DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • Spring AI Advisors: Chat Memory, Token Tracking, and Message Logging
  • Securing the AI Host: Spring AI MCP Server Communication With API Keys
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  • When One MVP Is Really Four Systems: A Better Way to Plan Multi-Role Apps

Two Way Communication in JMS

By 
Łukasz Budnik user avatar
Łukasz Budnik
·
May. 29, 12 · Interview
Likes (1)
Comment
Save
Tweet
Share
26.5K Views

Join the DZone community and get the full member experience.

Join For Free
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
Two-way communication

Published at DZone with permission of Łukasz Budnik. See the original article here.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook