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


AddThis Social Bookmark Button

Using OpenJMS withTomcat
Pages: 1, 2


Related Reading

Java Message ServiceJava Message Service
By Richard Monson-Haefel & David Chappell
Table of Contents
Sample Chapter
Full Description
Read Online -- Safari

Below is the servlet code for this example. It uses the information passed across by the user to retrieve a ConnectionFactory from the JNDI provider, connect to the OpenJMS server, create a publisher, and publish messages to it. Finally, it closes the connection and returns a response to the user. If a problem is encountered while this request is being processed, a ServletException is returned to the user.

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.jms.*;
import javax.naming.*;

public class SimplePublisher extends HttpServlet {
  public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

    String url = request.getParameter("url");
    String topic_name = request.getParameter("topic");
    String count_str = request.getParameter("count");
    int count = 0;
    try {
      count = Integer.parseInt(count_str);
    } catch (Exception exception) {
      throw new ServletException("Error in converting count " + exception);

    try {
      String factory_name = "JmsTopicConnectionFactory";
      Properties props = new Properties();

      if (url.startsWith("rmi")) {
      } else {
      props.put(Context.PROVIDER_URL, url);
      // lookup the connection factory from the context
      Context context = new InitialContext(props);
      TopicConnectionFactory factory = (TopicConnectionFactory)

      // if we can't find the factory then throw an exception
      if (factory != null) {
        TopicConnection connection = factory.createTopicConnection();
        TopicSession session = connection.createTopicSession(
       false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(topic_name);

        if (topic != null) {
          TopicPublisher publisher = session.createPublisher(topic);
          if (publisher != null) {
            for (int index = 0; index < count; index++) {
                "publish message " + count));

        // close the connction
    } catch (Exception exception) {
      throw new ServletException("Exception in processign request " + exception);

    // formulate response
    PrintWriter out = response.getWriter();
    out.println("<title>OpenJMS Response</title>");
    out.println("Published " + count + " messages on topic " + topic_name +
          " to url " + url + ".");

Compiling and Packaging

To compile the servlet, you need the following libraries:

Library Description
openjms-client-0.7.jar The core OpenJMS client library
openjms-rmi-0.7.jar Additional classes for RMI-based OpenJMS server
exolabcore-0.3.1.jar Core Exolab library
jms_1.0.2a.jar Java Message Service (JMS) API

In addition, before we package the Web application, we must create the web.xml descriptor file, which identifies the servlet and its corresponding URL mapping.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
<display-name>A Simple Publisher Application</display-name>

Finally, we must package all the above components in a Web Application Archive (war), which is identical, in format, to JAR files. The content of openjms.war, which is shown below, complies with the structure identified in the Servlet specifications.

     0 Wed Oct 03 16:18:36 GMT+11:00 2001 META-INF/
     0 Wed Oct 03 16:18:36 GMT+11:00 2001 WEB-INF/
     0 Wed Oct 03 16:18:36 GMT+11:00 2001 WEB-INF/classes/
     0 Wed Oct 03 16:18:38 GMT+11:00 2001 WEB-INF/lib/
     0 Wed Oct 03 16:18:36 GMT+11:00 2001 servlets/
     0 Wed Oct 03 16:18:36 GMT+11:00 2001 build/
     0 Wed Oct 03 16:18:36 GMT+11:00 2001 dist/
   667 Wed Oct 03 16:18:36 GMT+11:00 2001 WEB-INF/web.xml
  3752 Wed Oct 03 16:18:36 GMT+11:00 2001 WEB-INF/classes/SimplePublisher.java
  4279 Wed Oct 03 16:18:38 GMT+11:00 2001 WEB-INF/classes/SimplePublisher.class
268061 Wed Oct 03 16:18:38 GMT+11:00 2001 WEB-INF/lib/openjms-client-0.7.jar
 44025 Wed Oct 03 16:18:38 GMT+11:00 2001 WEB-INF/lib/openjms-rmi-0.7.jar
124524 Wed Oct 03 16:18:38 GMT+11:00 2001 WEB-INF/lib/exolabcore-0.3.1.jar
 27724 Wed Oct 03 16:18:38 GMT+11:00 2001 WEB-INF/lib/jms_1.0.2a.jar
   757 Wed Oct 03 16:18:36 GMT+11:00 2001 servlets/SimplePublisher.html
    51 Wed Oct 03 16:18:36 GMT+11:00 2001 META-INF/MANIFEST.MF

The archive contains all the libraries, the servlet, and associated HTML files.

Deploying the OpenJMS Web Application

We must follow the following steps to deploy the archive, generated above, in Tomcat.

  1. Copy and unpack the archive in Tomcat's webapps directory.
  2. Modify Tomcat's server.xml file and add a new Context.
  3. Restart Tomcat.
  4. Start OpenJMS server.

Unpack the Web Application

Locate the webapps directory in your Tomcat installation, create the openjms subdirectory, and unpack the archive.

The directory structure should be similar to this:


Modify the server.xml Configuration File

In Tomcat's server configuration file, add a new context that defines the OpenJMS Web application. The configuration redirects URLs in the form of http://localhost:2222/openjms/ to the Web application we have just deployed.

<Server port="2220" shutdown="SHUTDOWN" debug="0">
  <Service name="Tomcat-Standalone">
    <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
    <Connector className="org.apache.catalina.connector.http.HttpConnector"
               port="2222" minProcessors="5" maxProcessors="75"
               enableLookups="true" redirectPort="2223"
               acceptCount="10" debug="0" connectionTimeout="60000"/>
    <Engine name="Standalone" defaultHost="localhost" debug="0">
      <Logger className="org.apache.catalina.logger.FileLogger"
              prefix="catalina_log." suffix=".txt" timestamp="true"/>
      <Realm className="org.apache.catalina.realm.MemoryRealm" />
      <Host name="localhost" debug="0" appBase="webapps" unpackWARs="true">
        <!-- Tomcat Root Context -->
        <Context path="" docBase="ROOT" debug="0"/>
        <!-- SimplePublisher examples -->
        <Context path="openjms" docBase="openjms" debug="0"
          <Logger className="org.apache.catalina.logger.FileLogger"
                     prefix="localhost_examples_log." suffix=".txt"

Restarting Tomcat

Restart Tomcat, using either catalina or tomcat scripts files located in the bin directory. If the server comes up without any problems, the following will be displayed on the console:

Starting service Tomcat-Standalone
Apache Tomcat/4.0-b8-dev

Starting the OpenJMS Server

For simplicity, use the out-of-the-box RMI/JDBM configuration, rmi_jms.xml, for the server. Change to the bin directory of the OpenJMS installation and enter the following command line: startjms.bat -config ..\config\rmi_jms.xml

If the server has successfully started, the following output should appear in the console:

OpenJMS 0.7 (build 6)
Exolab Inc. (C) 1999-2001. All rights reserved. www.openjms.org
16:57:59.564 INFO [main] - Instantiated the RmiRegistryService service on port 1099
16:57:59.564 INFO [main] - Embedded RMI Registry running on port 1099
16:57:59.574 INFO [main] - Creating the RMI-based JMS Server
16:57:59.574 INFO [main] - Creating JNDI Server org.exolab.jms.jndi.rmi.RmiJndiServer
16:58:02.048 INFO [main] - Registered the service JndiServer with the registry.
16:58:02.048 INFO [main] - Started service [RmiRegistryService]
16:58:02.048 INFO [main] - Started service [ThreadPoolManager]
16:58:02.048 INFO [RmiRegistryService] - Started RmiRegistryService
16:58:12.052 INFO [main] - Started service [EventManagerThread]
16:58:12.252 INFO [main] - Removed expired messages.
16:58:12.252 INFO [main] - Started service [DatabaseService]
16:58:12.262 INFO [main] - Started service [Scheduler]
16:58:12.262 INFO [main] - Started service [LeaseManagerReaper]
16:58:12.262 INFO [main] - Started service [TransactionService]
16:58:12.262 INFO [main] - Started service [GCCollectionService]
16:58:12.392 INFO [main] - Started service [MessageManager]
16:58:12.402 INFO [main] - JMS Server is bound to //localhost:1099/OpenJmsServer
16:58:12.433 INFO [main] - JMS Admin Server is bound to //localhost:1099/JmsAdminServer
16:58:14.285 INFO [main] - Bound connection factory JmsQueueConnectionFactory
16:58:14.295 INFO [main] - Bound connection factory JmsTopicConnectionFactory

The important thing to note is that the server uses RMI with an embedded JNDI provider, called JndiServer, running on the localhost at port 1099. This information will be required by the user to formulate the URL.

Using the OpenJMS Web Application

Once the application has been deployed and both Tomcat and OpenJMS have been started, use the following URL to interact with the application: http://localhost:2222/openjms/servlets/SimplePublisher.html

The following page, excluding the information, will be rendered in the browser.

Dialogue box.
Figure 4.

Fill in the details, as specified above, and press Submit Query. This request will publish 20 messages under the topic jima.



Jakarta Tomcat

Jim Alateras is an independent consultant specializing in open source and emerging technologies.

Return to ONJava.com.