ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Using OpenJMS withTomcat

by Jim Alateras
12/12/2001

This article illustrates how to integrate OpenJMS with Jakarta Tomcat. It deals exclusively with Tomcat v4 (or Catalina), which is the next-generation servlet/JSP container, and OpenJMS v.0.7.

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:

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.

Diagram.
Figure 1.

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.

Diagram.
Figure 2.

The table below provides a brief description of each of the components in the interaction diagram. Further details are provided in subsequent sections.

Component Description
Browser Represents the client browser making the requests to Tomcat.
Tomcat Container The Servlet/JSP container
SimplePublisher.html A static HTML page containing a form, which gathers information to publish messages to the OpenJMS Server
SimplePublisher.class 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.

  1. The user will enter a URL to access the SimplePublisher.html page

    .
  2. Once the page is displayed in the browser, the user will complete the url, topic, and count fields.

    Dialogue box.
    Figure 3.

    Field Description
    url This denotes the URL of the JNDI Server, which holds the OpenJMS ConnectionFactories. The ConnectionFactories are 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 X is the message number.
    count The number of messages to publish.
  3. 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 SimplePublisher servlet.

    The servlet will connect to the OpenJMS server, create a TopicPublisher using 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.

SimplePublisher.html

The HTML code includes the form, which gathers the user information and submits a request to the SimplePublisher servlet.

<html>
  <title>
    Simple Publisher
  </title>
  
  <body>
    <form method="get" action="../servlet/SimplePublisher">
      <table cols="2" cellspacing="10">
        <tr>
          <td>url</td>
          <td><input type="text" name="url" size="80"></td>
        </tr>            
      
        <tr>
          <td>topic</td>
          <td><input type="test" name="topic" size="30"></td>
        </tr>
      
        <tr>
          <td>count</td>
          <td><input type="text" name="count" size="5"></td>
        </tr>
      </table>
      <input type="submit">
    </form>
  </body>
</html>

Pages: 1, 2

Next Pagearrow