JMS Publish/Subscribe Message Example

In JMS tutorial, you read about JMS messaging domains Point to Point Domain and Publish Subscribe Domain. In this example, we will go through one such example of Publish/Subscribe messaging domain. The publish/subscribe messaging domain is a one-to-many model where one publisher sends the message through a topic to all the subscribers who are active and they receive the message through topic.

Publish Subscribe JMS Messaging
Publish Subscribe JMS Messaging

Publish/Subscribe Messaging Publisher Application Flow

A typical publish/subscribe messaging example for the sender application is given below. The following steps for the application are –

  1. Firstly, we will obtain the Initial Context object for the JMS server.
  2. After that use the initial context object for lookup a topic object.
  3. Again we will use the initial context object for lookup the topic connection factory.
  4. Then use the topic connection factory to create the topic connection as it represents the physical connection of the JMS server.
  5. After creating the topic connection factory we will create the topic session where the first parameter will decide whether the session is transacted or not. But we will use a non-transacted session and the second parameter decides the delivery mode.
  6. After this we will create a topic publisher for topic object and then create a message.
  7. Then send the message such as “Hello” to the topic object.
  8. After that close the topic connection as you close the topic connection it will automatically close the session and topic publisher.

Let’s look at example below:

package pubSub;     

import javax.naming.InitialContext;                                                                           
import javax.jms.Topic;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.TopicPublisher;
import javax.jms.DeliveryMode;
import javax.jms.TopicSession;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
                                                                           
public class Publisher
{
    public static void main(String[] args) throws Exception
    {
       // get the initial context
       InitialContext ctx = new InitialContext();
                                                                          
       // lookup the topic object
       Topic topic = (Topic) ctx.lookup("topic/topic0");
                                                                          
       // lookup the topic connection factory
       TopicConnectionFactory connFactory = (TopicConnectionFactory) ctx.
           lookup("topic/connectionFactory");
                                                                          
       // create a topic connection
       TopicConnection topicConn = connFactory.createTopicConnection();
                                                                          
       // create a topic session
       TopicSession topicSession = topicConn.createTopicSession(false, 
           Session.AUTO_ACKNOWLEDGE);
                                                                          
       // create a topic publisher
       TopicPublisher topicPublisher = topicSession.createPublisher(topic);
       topicPublisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
                                                                          
       // create the "Hello World" message
       TextMessage message = topicSession.createTextMessage();
       message.setText("Hello World");
                                                                          
       // publish the messages
       topicPublisher.publish(message);
                                                                          
       // print what we did
       System.out.println("Message published: " + message.getText());
                                                                          
       // close the topic connection
       topicConn.close();
    }
}

Publish/Subscribe Messaging Subscriber Application Flow

Most of the steps for the receiver side are same as for the sender application – except it will listen the message rather than sending the JMS message.

package pubSub;      

import javax.naming.InitialContext;                                                                           
import javax.jms.Topic;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
                                                                           
public class Subscriber
{
    public static void main(String[] args) throws Exception
    {
       // get the initial context
       InitialContext ctx = new InitialContext();
                                                                          
       // lookup the topic object
       Topic topic = (Topic) ctx.lookup("topic/topic0");
                                                                          
       // lookup the topic connection factory
       TopicConnectionFactory connFactory = (TopicConnectionFactory) ctx.
           lookup("topic/connectionFactory");
                                                                          
       // create a topic connection
       TopicConnection topicConn = connFactory.createTopicConnection();
                                                                          
       // create a topic session
       TopicSession topicSession = topicConn.createTopicSession(false,
           Session.AUTO_ACKNOWLEDGE);
                                                                          
       // create a topic subscriber
       TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
                                                                          
       // start the connection
       topicConn.start();
                                                                          
       // receive the message
       TextMessage message = (TextMessage) topicSubscriber.receive();
                                                                          
       // print the message
       System.out.println("Message received: " + message.getText());
                                                                          
       // close the topic connection
       topicConn.close();
    }
}

In the above code the receiver will receive the message from the sender and print it i.e. Hello World.

Happy Learning !!

Comments

Subscribe
Notify of
guest
5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode