Platinum Partner
java,enterprise-integration,tutorial,jms,integration

Two Way Communication in JMS

Today I will debunk quite a popular myth about one way only communication in JMS. There is no classic request-response equivalent of course (just like there is in AMQP), but a message may convey a reply to header containing a reference to a temporary queue. This way we can achieve two way communication in JMS.

Below I give you a simple JUnit test which illustrates the idea.

Example

In the following example I use a temporary queue and set it as JMS reply to header. The temporary queue exists as long as its creator's session is active. The code is pretty simple and self commenting. In testA I create a message and a temporary queue which I expect to recieve a response from the consumer. Comsumer in testB consumes the message, retrieves the reply to destination and sends a text message to it. testC consumes the message from temporary queue. Once the session is closed, temporary queue is disposed and removed.
public class AppTest {
 private static Connection connection;
 private static Session session;
 private static Destination destination;
 private static Destination replyToDestination;
 @BeforeClass
 public static void setup() throws JMSException {
  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
    "tcp://localhost:62626");
  connection = connectionFactory.createConnection();
  connection.start();
  session = connection.createSession(true, Session.SESSION_TRANSACTED);
  destination = session.createQueue("Java.ActiveMQ.Test.Queue");
  replyToDestination = session.createTemporaryQueue();
 }
 @AfterClass
 public static void cleanup() throws JMSException {
  session.close();
  connection.stop();
  connection.close();
 }
 @Test
 public void testA() throws JMSException {
  MessageProducer producer = session.createProducer(destination);
  Person person = new Person();
  person.setFirstName("Lukasz");
  person.setLastName("Budnik");
  Message message = session.createObjectMessage(person);
  message.setJMSReplyTo(replyToDestination);
  producer.send(message);
  session.commit();
 }
 @Test
 public void testB() throws JMSException, InterruptedException {
  Person person = null;
  MessageConsumer consumer = session.createConsumer(destination);
  Message message;
  while ((message = consumer.receive(2000l)) != null) {
   if (message instanceof ObjectMessage) {
    ObjectMessage objectMessage = (ObjectMessage) message;
    Object object = objectMessage.getObject();
    if (object instanceof Person) {
     person = (Person) object;
     Assert.assertEquals("Lukasz", person.getFirstName());
     Assert.assertEquals("Budnik", person.getLastName());
     Destination replyToDestination = message.getJMSReplyTo();     
     MessageProducer replyToMessageProducer = session.createProducer(replyToDestination);
     Message replyMessage = session.createTextMessage("OK");
     replyToMessageProducer.send(replyMessage);     
     session.commit();
    }
   }
  }
  if (person == null) {
   Assert.fail("Person is null");
  }
 }
 @Test
 public void testC() throws JMSException, InterruptedException {
  String text = null;
  MessageConsumer consumer = session.createConsumer(replyToDestination);
  Message message;
  while ((message = consumer.receive(2000l)) != null) {
   if (message instanceof TextMessage) {
    TextMessage textMessage = (TextMessage) message;
    text = textMessage.getText();
    Assert.assertEquals("OK", text);     
    session.commit();
   }
  }
  if (text == null) {
   Assert.fail("Text is null");
  }
 }
}
Summary

Simple, isn't it? This mechanism is used heavily in Apache Camel and WS frameworks which use transacted JMS under the hood for WS-RM implementations. Using those frameworks, temporary queues and reply to headers are being taken care of transparently so I may even not know that you use them :)

cheers,
Łukasz

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}