Monday 6 August 2018

JMS: Temporary Topics

Temporary topic is unique to a connection. A temporary topic is created for the duration of a JMSContext, Connection or TopicConnection.

One important thing to note here is that, “It is a system defined Topic whose messages may be consumed only by the connection that created it”.

How to create temporary topic?
javax.jms.Session interface provides 'createTemporaryTopic' method to create an instance of temporary topic.
TemporaryTopic createTemporaryTopic() throws JMSException;

Limitations of temporary destinations
a.   Temporary destinations are only consumed by the connection that created them.
b.   Temporary destination scope limited to the connection that created it, and these temporary destinations are closed whenever the connection is closed. You will lose all the data in the temporary queue, when the connection is closed.

Where can I use temporary topics?
One typical use for a temporary destination is, as the JMSReplyTo destination for service requests, Consumers send the acknowledgement to the destination specified in JMSReplyTo header.

JMSProducer.java
package com.sample.producer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TemporaryTopic;
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 = "MSG_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 textMessage = session.createTextMessage("Hello World");
  TemporaryTopic temporaryTopic = session.createTemporaryTopic();
  textMessage.setJMSReplyTo(temporaryTopic);

  producer.send(textMessage);

  MessageConsumer messageConsumer = session.createConsumer(temporaryTopic);
  Message message = messageConsumer.receive();

  if (message instanceof TextMessage) {
   String text = ((TextMessage) message).getText();
   System.out.println(text);
  }

  connection.close();
 }
}

JMSConsumerOne.java
package com.sample.consumer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
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 = "MSG_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();

    Destination replyBackDest = message.getJMSReplyTo();
    MessageProducer messageProducer = session.createProducer(replyBackDest);

    TextMessage textMessage = session.createTextMessage("JMSConsumerOne received : \'" + text + "\'");
    messageProducer.send(textMessage);
   }

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

 }

}


Run JMSConsumerOne.java followed by JMSProducer.java, you can able to see below output in JMSProducer console.

JMSConsumerOne received : 'Hello World'






Previous                                                 Next                                                 Home

No comments:

Post a Comment