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 Messaging Publisher Application Flow
A typical publish/subscribe messaging example for the sender application is given below. The following steps for the application are –
- Firstly, we will obtain the Initial Context object for the JMS server.
- After that use the initial context object for lookup a topic object.
- Again we will use the initial context object for lookup the topic connection factory.
- Then use the topic connection factory to create the topic connection as it represents the physical connection of the JMS server.
- 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.
- After this we will create a topic publisher for topic object and then create a message.
- Then send the message such as “Hello” to the topic object.
- 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