September 7, 2011

Java Message Service (JMS) Topic- Hello World Example with MessageListener

1) Configure the JMS Topic in the JBoss 4.2.2GA Server, in the file ${JBOSS_SERVER}/server/default/deploy/jms/jbossmq-destinations-service.xml

<mbean code="org.jboss.mq.server.jmx.Topic"
  name="jboss.mq.destination:service=Topic,name=myTopic1">
  <depends optional-attribute-
  name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>

Ensure that the name myTopic1 is unique within the XML file

2) The ConnectionFactory administered object is pre-configured with the default JNDI name 'ConnectionFactory' in JBoss

Note: Add the following jar file to the classpath / build path of the Java Project / Application:

%JBOSS_HOME%\client\jbossall-client.jar

3) Create the TopicPublisherClient.java

package info.icontraining.topic;

import javax.jms.*;
import javax.naming.*;
import java.util.*;

public class TopicPublisherClient {

   public static void main(String[] args) {

      TopicConnection conn = null;
      TopicSession session = null;
      Topic topic = null;
   
      try {
         Properties props = new Properties();
         props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory");
         props.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
         props.put(Context.PROVIDER_URL, "localhost:1099");
         props.put("java.naming.rmi.security.manager", "yes");

         InitialContext iniCtx = new InitialContext(props);

         Object cf = iniCtx.lookup("ConnectionFactory");
         TopicConnectionFactory tcf = (TopicConnectionFactory) cf;
    
         conn = tcf.createTopicConnection();

         topic = (Topic) iniCtx.lookup("topic/myTopic1");
    
         session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
         conn.start();

         TopicPublisher topicPublisher = session.createPublisher(topic);

         TextMessage message = session.createTextMessage();
         message.setText("this is a test message");
         topicPublisher.publish(message);
         System.out.println("Message published.");

         topicPublisher.close();

      } catch (NamingException e) {   
         System.out.println(e.toString());
      } catch (JMSException e) {
         System.out.println("JMS Exception");
      } finally {
         if (conn != null) {
            try {
               conn.close();
            } catch (JMSException e) {}
         }
      }
   }
}

4) Create the TopicSubscriberClient.java

package info.icontraining.topic;

import javax.jms.*;
import javax.naming.*;
import java.util.*;

public class TopicSubscriberClient
{
   public static void main(String[] args) {
      TopicConnection topicConnection = null;
      char answer = '\0';
  
      try 
      {
         Properties props = new Properties();

          props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory");
          props.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
          props.put(Context.PROVIDER_URL, "localhost:1099");
          props.put("java.naming.rmi.security.manager", "yes");

          Context context = new InitialContext(props);
          TopicConnectionFactory tcf= (TopicConnectionFactory) context.lookup("ConnectionFactory");
          topicConnection = tcf.createTopicConnection();

          String topicName = "topic/myTopic1";
          Topic topic = (Topic) context.lookup(topicName);
   
          TopicSession topicSession = topicConnection.createTopicSession(false,  TopicSession.AUTO_ACKNOWLEDGE);
          topicConnection.start();

          TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
   
          topicSubscriber.setMessageListener(new MyMessageListener());

          for(;;) {}
      }
      catch (JMSException e) {
         System.out.println("JMS Exception");
      } catch (Exception e) {
         System.out.println(e.toString());
      } finally {
         if (topicConnection != null) {
            try {
               topicConnection.close();
            } catch (JMSException e) {}
         }
      }
   }

   static class MyMessageListener implements MessageListener { 

      public void onMessage(Message message) {

         TextMessage tm = (TextMessage) message;

         try {
            System.out.println("onMessage==>"+ tm.getText());
         } catch(Throwable t) {
            t.printStackTrace();
         }
      }
   }
}

5) Run multiple instances of TopicSubscriberClient in multiple heaps. Next, run TopicPublisherClient to send the message to the JMS Topic.

No comments:

Post a Comment