Sunday 5 August 2018

JMS publish/subscribe model

In Publish-subscribe model, client send the message to the topic. All the subscribers subscribe to the topic to receive the messages.




a.   Client 1 publish the message to a topic.
b.   Client 2, Client 3…Client N subscribe to this topic to receive the messages.
c.   Topic deliver the messages to all the subscribed clients.

JMSProducer.java
package com.sample.producer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSProducer {
 private static final String JMS_PROVIDER_URL = "tcp://127.0.0.1:5050";
 private static final String TOPIC_NAME = "ORDER_TOPIC";

 public static void main(String[] args) throws JMSException, InterruptedException {
  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(JMS_PROVIDER_URL);
  Connection connection = connectionFactory.createConnection();
  connection.start();

  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  Topic topic = session.createTopic(TOPIC_NAME);
  MessageProducer producer = session.createProducer(topic);

  TextMessage textMesage = session.createTextMessage("Hello World");
  producer.send(textMesage);

  connection.close();
 }
}

JMSConsumerOne.java
package com.sample.consumer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSConsumerOne {
 private static final String JMS_PROVIDER_URL = "tcp://127.0.0.1:5050";
 private static final String TOPIC_NAME = "ORDER_TOPIC";

 public static void main(String args[]) throws JMSException {
  Connection connection = null;

  try {
   ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(JMS_PROVIDER_URL);
   connection = connectionFactory.createConnection();
   connection.start();

   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

   Topic topic = session.createTopic(TOPIC_NAME);
   MessageConsumer messageConsumer = session.createConsumer(topic);
   Message message = messageConsumer.receive();
   
   if(message instanceof TextMessage) {
    String text = ((TextMessage) message).getText();
    System.out.println("Consumer 1 received : " + text);
   }
   

  } finally {
   if (connection != null)
    connection.close();
  }

 }

}

JMSConsumerTwo.java
package com.sample.consumer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSConsumerTwo {
 private static final String JMS_PROVIDER_URL = "tcp://127.0.0.1:5050";
 private static final String TOPIC_NAME = "ORDER_TOPIC";

 public static void main(String args[]) throws JMSException {
  Connection connection = null;

  try {
   ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(JMS_PROVIDER_URL);
   connection = connectionFactory.createConnection();
   connection.start();

   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

   Topic topic = session.createTopic(TOPIC_NAME);
   MessageConsumer messageConsumer = session.createConsumer(topic);
   Message message = messageConsumer.receive();

   if (message instanceof TextMessage) {
    String text = ((TextMessage) message).getText();
    System.out.println("Consumer 2 received : " + text);
   }

  } finally {
   if (connection != null)
    connection.close();
  }

 }

}


Run JMSConsumer1.java

Run JMSConsumer2.java

Run JMSProducer.java

In JMSConsumer1 console, you will get below message.
Consumer 1 received : Hello World

In JMSConsumer2 console, you will get below message.
Consumer 2 received : Hello World



Previous                                                 Next                                                 Home

No comments:

Post a Comment