Sunday 5 August 2018

JMS: Temporary Queue

A TemporaryQueue is a unique Queue object created for the duration of a connection. You can create a temporary destination using session object. Temporary destination has a scope limited to the connection that created it, and these temporary destinations are closed whenever the connection is closed.

javax.jms.Session interface provides below methods to create temporary queue and topic.

TemporaryQueue createTemporaryQueue() throws JMSException;
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 queues?
One typical use of a temporary destination is, as the JMSReplyTo destination for service requests, Consumers send the reply back to the destination specified in JMSReplyTo header.

Demo Application
I am going to use temporary queue to receive acknowledgement of the messages received by jms consumer.



a.   JMS Producer send message to ‘ORDER_QUEUE’.
b.   ‘ORDER_QUEUE’ send the message to ‘JMS Consumer’.
c.   After receiving the message from ‘ORDER_QUEUE’, JMS Consumer extract the order id and send it to temporary queue.
d.   JMS Producer reads the acknowledgement from Temp Queue

How to send the temporary queue details to JMS Consumer?
Use Message ‘setJMSReplyTo’ API to inform temporary queue details to JMS Consumer.

TextMessage message = session.createTextMessage("MessageId:12345 HelloWorld");
TemporaryQueue tempQueue = session.createTemporaryQueue();
message.setJMSReplyTo(tempQueue);

Find below working application.

JMSProducer.java
package com.sample.producer;

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.TemporaryQueue;
import javax.jms.TextMessage;

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 QUEUE = "ORDER_QUEUE";

 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);

  Destination temporaryQueue = session.createQueue(QUEUE);

  MessageProducer producer = session.createProducer(temporaryQueue);

  TextMessage message = session.createTextMessage("OrderId:12345 item1:100");
  
  TemporaryQueue tempQueue = session.createTemporaryQueue();
  message.setJMSReplyTo(tempQueue);

  producer.send(message);

  MessageConsumer consumer = session.createConsumer(tempQueue);

  Message msg = consumer.receive();

  if (msg instanceof TextMessage) {
   TextMessage textMessage = (TextMessage) msg;
   System.out.println(textMessage.getText());
  }
  connection.close();
 }
}

JMSConsumer.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 org.apache.activemq.ActiveMQConnectionFactory;

public class JMSConsumer {
 private static final String JMS_PROVIDER_URL = "tcp://127.0.0.1:5050";
 private static final String QUEUE = "DEMO_QUEUE";

 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);

   Destination destination = session.createQueue(QUEUE);

   MessageConsumer consumer = session.createConsumer(destination);

   Message message = consumer.receive();

   if (message instanceof TextMessage) {
    TextMessage textMessage = (TextMessage) message;

    String msgId = textMessage.getText().split(" ")[0];
    
    Destination replyBackQueue = textMessage.getJMSReplyTo();
    MessageProducer producer = session.createProducer(replyBackQueue);

    TextMessage msg = session.createTextMessage("Message Received : " + msgId);
    producer.send(msg);
   }

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

 }

}


Run JMSConsumer.java

Run JMSProvider.java

You can able to see below message in the console.

Message Received : OrderId:12345




Previous                                                 Next                                                 Home

No comments:

Post a Comment