Using OpenJMS within Eclipse

OpenJMS allows you to use the Java Message Service (JMS) without a full-fledged Java Enterprise Edition container. It is an open source implementation of the JMS 1.1 specification.

We start by installing and starting OpenJMS. On Windows this is done by extracting the .zip file, going to the bin directory and calling startup.bat. On Linux this is done by extracting the .tar.gz file, going to the bin directory and calling startup.sh.

The next part is creating a Java project in Eclipse. Make sure to create the following directories:

  • bin, containing your own Java .class files
  • etc, containing the JNDI configuration file
  • lib, containing all required .jar library files
  • src, containing your own Java .java files

Start by copying all .jar files from the OpenJMS distribution to the lib directory and tell Eclipse to use these .jar files. Also add the etc directory to the build path. This ensures that the JNDI configuration file that we will create shortly is used at runtime.

Now create the JNDI configuration file that will tell our application where to find the running OpenJMS server. Create a file called jndi.properties with the following content:

java.naming.factory.initial=org.exolab.jms.jndi.InitialContextFactory
java.naming.provider.url=tcp://localhost:3035

Finally, we move on to the actual Java code that makes use of this groundwork. We create a message sender that sends a message to the topic topic1. This is a topic name that is available in OpenJMS by default.

MessageSender.java

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;

public class JMSSender {

   public void start() {
      Context context = null;
      Connection connection = null;
      MessageProducer sender = null;
      try {
         context = new InitialContext();
         ConnectionFactory factory = (ConnectionFactory) context.lookup("ConnectionFactory");
         connection = factory.createConnection();
         connection.start();

         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
         Destination destination = (Destination) context.lookup("topic1");
         sender = session.createProducer(destination);
         sender.send(session.createTextMessage("Hello World!"));
      } catch (Exception e) {
      } finally {
         try {
            if (sender != null) sender.close();
            if (connection != null) connection.close();
            if (context != null) context.close();
         } catch (Exception e) {
         }
      }
   }

   public static void main(String[] args) {
      JMSSender jmsSender = new JMSSender();
      jmsSender.start();
   }
}

MessageReceiver.java

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JMSReceiver {

   public void start() throws JMSException, NamingException {
      Context context = null;
      Connection connection = null;
      MessageConsumer receiver = null;
      try {
         context = new InitialContext();
         ConnectionFactory factory = (ConnectionFactory) context.lookup("ConnectionFactory");
         connection = factory.createConnection();
         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
         Destination destination = (Destination) context.lookup("topic1");

         receiver = session.createConsumer(destination);
         receiver.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
               TextMessage text = (TextMessage) message;
               try {
                  System.out.println("Received message: " + text.getText());
               } catch (JMSException e) {
               }
            }
         });

         connection.start();
      } catch (Exception e) {
      }
   }

   public static void main(String[] args) {
      JMSReceiver jmsReceiver = new JMSReceiver();
      jmsReceiver.start();
   }
}

Now start the JMSReceiver application and then the JMSSender application. If all goes well, the JMSReceiver application should print the text:

Received message: Hello World!
Advertisement
This entry was posted in programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s