Using OpenJMS withTomcatby Jim Alateras
The article assumes that the reader is familiar with both OpenJMS and Tomcat and does not describe how to install or use either product. Therefore, to allow you to work through this note we recommend that you install and familiarize yourself with the products by visiting their respective web sites.
The examples in this article have been tested with:
- OpenJMS v0.7, which conforms to v1.0.2 of the specification and is available at http://www.openjms.org/download.html.
- Jakarta Tomcat v4.0-b8, which conforms to v2.3 of the Servlet specification and v1.2 of the Java Server Pages (JSP) specification and is available at http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release/v4.0-b8
Figure 1 illustrates the relationship between the various Tomcat and OpenJMS components. The OpenJMS Web Application consists of JMS client code talking to the OpenJMS Client Library. The application is deployed within the Tomcat container. In this configuration, the OpenJMS server is executing outside the Tomcat container, and may even be executing on another machine on a different network.
The user can access the OpenJMS application by entering the corresponding URL in the browser or application. Tomcat will resolve the URL and redirect it to the corresponding servlet, which will execute and then return a response back to the user. In the example that follows, Tomcat is used to serve both dynamic and static Web pages, therefore functioning as a typical Web server in addition to a servlet and JSP container.
Writing an OpenJMS Web Application
This section describes how to write and package a trivial OpenJMS Web application, which simply connects to the OpenJMS Server and publishes messages. The interaction diagram, shown in Figure 2, depicts the sequence of events between the various components.
The table below provides a brief description of each of the components in the interaction diagram. Further details are provided in subsequent sections.
|Browser||Represents the client browser making the requests to Tomcat.|
|Tomcat Container||The Servlet/JSP container|
||A static HTML page containing a form, which gathers information to publish messages to the OpenJMS Server|
||A servlet class that interacts with the OpenJMS server|
|JNDI Provider||Used to bind and lookup OpenJMS connection factories|
|OpenJMS Server||A JMS-v1.0.2-compliant server|
The sequence of events is as follows.
The user will enter a URL to access the.
Once the page is displayed in the browser, the user will complete the url, topic, and count fields.
Field Description url This denotes the URL of the JNDI Server, which holds the OpenJMS
ConnectionFactoriesare used to bootstrap a connection to the OpenJMS Server. The form of the URL depends on the configuration of the OpenJMS Server.
topic This is the name of the destination that messages will be published under. The messages are in the form
publish message X, where
Xis the message number.
count The number of messages to publish.
3. When the user presses the Submit Query button, the information in the form will be sent across to Tomcat and used to invoke the
The servlet will connect to the OpenJMS server, create a
TopicPublisherusing the topic form parameter, publish count messages, and then close the connection. On successful completion, the user should see the following response in their browser.
Published 20 messages on topic jima to url rmi://localhost:1099/JndiServer.
The application itself is not useful, but it does provide all the information necessary to integrate the two components.
The HTML code includes the form, which gathers the user information and submits a request to the SimplePublisher servlet.
<form method="get" action="../servlet/SimplePublisher">
<table cols="2" cellspacing="10">
<td><input type="text" name="url" size="80"></td>
<td><input type="test" name="topic" size="30"></td>
<td><input type="text" name="count" size="5"></td>
Pages: 1, 2