Enterprise Integration Zone is brought to you in partnership with:

Łukasz works as a Technical Architect for an international IT company and is responsible for delivering applications written in Java EE, Spring, and .NET. He has been involved in many various projects ranging from online insurance systems, voice and video solutions, mobile systems (both native and HTML5-based), medical systems, and large system integration projects. Łukasz is an expert in distributed systems, SOA, and cloud. Łukasz holds PhD in Computer Science. Łukasz is a DZone MVB and is not an employee of DZone and has posted 20 posts at DZone. You can read more from them at their website. View Full User Profile

Two Way Communication in JMS

05.28.2012
| 12520 views |
  • submit to reddit
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 Łukasz Budnik, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Daniel Slazer replied on Tue, 2012/06/12 - 12:30pm

In which case we can just use Collections.unmodifiableList(). It's all well and good to ask them not to call the method, but better still to enforce it. If they ignore our docs, it's better to throw an exception than to corrupt the data.

Hani Jee replied on Sat, 2012/08/04 - 10:00pm

There is no question that instantaneous water heaters are good options to trust in terms of their water heating services. However, you need to know that not all kinds of this water heater will work best for you. computer technician resume

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.