Pages

Publish and Subscribe in JMS

Java Messaging Service as two models of messaging.
1.point-to-point (tutorial here)
2.Publish and Subscribe.

Here we are going to look publish and subscribe example.

Requirement
Application server : JBoss Server 5.0 GA
Java : jdk 1.5 and above
IDE(optional) : Eclipse Indigo/ Juno


Step 1:Make sure the topic as created with name(here queue name is topic) in the path c:/dummypath/jboss 5.1 GA/server/default/deploy/messaging and file is destinations-service.xml

<mbean code="org.jboss.jms.server.destination.TopicService" name="jboss.messaging.destination:service=Topic,name=topic" xmbean-dd="xmdesc/Topic-xmbean.xml">
    <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
    <depends>jboss.messaging:service=PostOffice</depends> 
    </mbean>

Step 2:Create a Publisher to put a message in Topic.
Publisher
import java.util.Hashtable;
import javax.jms.JMSException;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TopicPublisher {

 public final static String JNDI_FACTORY = "org.jnp.interfaces.NamingContextFactory";
 // *************** Connection Factory JNDI name *************************
 public final static String JMS_FACTORY = "/ConnectionFactory";
 String topicName = "topic";
  
 private TopicConnectionFactory tconFactory;
 private TopicConnection tcon;
 private TopicSession tsession;
 private Topic topic;
 private TextMessage msg;

 public static void main(String[] args) throws Exception {
  InitialContext ic = getInitialContext();
  TopicPublisher tp = new TopicPublisher();
  tp.init(ic);
  tp.readAndSend(tp);
  tp.close();
 }

 private static InitialContext getInitialContext() throws NamingException {
  Hashtable env = new Hashtable();
  env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
  env.put(Context.PROVIDER_URL, "localhost:1099");
  return new InitialContext(env);
 }

 public void init(Context ctx) throws NamingException, JMSException {
  tconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY);
  tcon = tconFactory.createTopicConnection();
  tcon.start();

  tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
  
  topic = tsession.createTopic(topicName);
  msg = tsession.createTextMessage("Hello World");
 }

 private void readAndSend(TopicPublisher qs) throws JMSException {
  javax.jms.TopicPublisher topicPub=tsession.createPublisher(topic);
  topicPub.publish(msg);
  System.err.println("Topic published");
 }

 public void close() throws JMSException {
  tsession.close();
  tcon.close();
 }
}

Step 3:Create a Subscribe to receive a message in Topic.
Subscribe
import java.util.Hashtable;
import javax.jms.JMSException;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.Session;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TopicSubscriber {

 public final static String JNDI_FACTORY = "org.jnp.interfaces.NamingContextFactory";
 // *************** Connection Factory JNDI name *************************
 public final static String JMS_FACTORY = "/ConnectionFactory";
 String topicName = "topic";
  
 private TopicConnectionFactory tconFactory;
 private TopicConnection tcon;
 private TopicSession tsession;
 private Topic topic;
 private Message msg;

 public static void main(String[] args) throws Exception {
  InitialContext ic = getInitialContext();
  TopicSubscriber ts = new TopicSubscriber();
  ts.init(ic);
  ts.readAndSend(ts);
  ts.close();
 }

 private static InitialContext getInitialContext() throws NamingException {
  Hashtable env = new Hashtable();
  env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
  env.put(Context.PROVIDER_URL, "localhost:1099");
  return new InitialContext(env);
 }

 public void init(Context ctx) throws NamingException, JMSException {
  tconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY);
  tcon = tconFactory.createTopicConnection();
  tcon.start();

  tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
  
  topic = tsession.createTopic(topicName);
 }

 private void readAndSend(TopicSubscriber qs) throws JMSException {
  
  javax.jms.TopicSubscriber topicSub=tsession.createSubscriber(topic);  
  msg=topicSub.receive();
  TextMessage txtMsg=(TextMessage)msg;
  System.out.println("Topic Subscriber --> " + txtMsg.getText());
 }

 public void close() throws JMSException {
  tsession.close();
  tcon.close();
 }
}
Step 4: Run the subscribe first(listen the topic) and run the publisher.Note you can run one or subscribe at a time. Message will be reached all subscribe console.

Output:
Subscribe Console : 
Topic Subscriber --> Hello World

Publish Console :
Topic Published