Sample Code for Java Messaging Service (JMS)

Here is some sample code that shows how to use JMS. It is followed by the property files:

 import java.util.Enumeration;
import java.util.Hashtable;
 import javax.jms.JMSException;
 import javax.jms.MessageListener;
 import javax.jms.Session;
 import javax.jms.Topic;
 import javax.jms.TopicConnection;
 import javax.jms.TopicConnectionFactory;
 import javax.jms.TopicSession;
 import javax.jms.TopicSubscriber;
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
 /**
 * @author grahamkj
 *
 */
 public class JMSTopicClient implements Runnable {
 public final static String JNDI_FACTORY = "jrun.naming.JRunContextFactory";
 public final static String JMS_FACTORY = "jms/TopicConnectionFactory";
 private String groupTag;
 private boolean quit = false;
 private MessageListener listener;
 private TopicConnectionFactory topicConnFactory;
 private TopicConnection topicConn;
 private TopicSession topicSes;
 private TopicSubscriber topicSubscriber;
 private Topic topic;
 public JMSTopicClient() { 
   }
 public void run() {
   InitialContext jmsCtx = null;
 try {
   // Now set up a JMS topic that will be used to monitor the return results
   // from the blast
   // see if already initialized
   if (topicConn == null) {
   Hashtable env = new Hashtable();
   env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
   String serverURL = "10.1.204.30:2913";
   env.put(Context.PROVIDER_URL, serverURL);
   env.put(Context.SECURITY_PRINCIPAL, "admin");
   env.put(Context.SECURITY_CREDENTIALS, "admin");
   /**
   * @todo work out what this call does, was in the weblogic
   * example so stuck with it.
   */
   jmsCtx = new InitialContext(env);
   Object o = jmsCtx.lookup(JMS_FACTORY);
   System.out.println(
   "Found JMS_FACTORY: " + o.getClass().getName());
   topicConnFactory = (TopicConnectionFactory) jmsCtx.lookup(JMS_FACTORY);
   System.out.println("Finished looking up TopicConnectionFactory");
   topicConn = topicConnFactory.createTopicConnection();
   System.out.println("Finished calling createTopicConnection");
   
   // System.out.print(
   // "Waiting 5 seconds... (Check for odd port connections)..");
   // Thread.sleep(5000);
   // System.out.println("Done");
   /*Mike: This is the call that opens a socket on port 58804(random) */
   /*:BEGIN*/
   
   topicSes = topicConn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
   /*:END*/
   System.out.println("Finished calling topic session");
   // System.out.print(
   // "Waiting 5 seconds... (Check for odd port connections)..");
   // Thread.sleep(5000);
   // System.out.println("Done");
   String topicName = "jms/queue/TestClient1";
   // NOTE: Must convert the username to all lowercase to match what
   // the eRights security realm implementation does
   topicName = topicName.toLowerCase();
   subscribeToTopic(jmsCtx, topicName);
   System.out.println("We are now subscribe");
 } // end of getting new subscription to topic
 } catch (Exception ex) {
   ex.printStackTrace();
   }
 }
 protected void subscribeToTopic(InitialContext jmsCtx, String topicName)
 throws JMSException, NamingException {
   try {
     topic = (Topic) jmsCtx.lookup(topicName);
   } catch (NamingException ne) {
     topic = topicSes.createTopic(topicName);
     jmsCtx.bind(topicName, topic);
   }
   topicSubscriber = topicSes.createSubscriber(topic);
   topicSubscriber.setMessageListener(listener);
   topicConn.start();
 }
 public static void main(String[] args) {
   Thread t = new Thread(new JMSTopicClient());
   t.start();
 }
}