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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Trending

  • Power BI Embedded Analytics — Part 2: Power BI Embedded Overview
  • Integrating Security as Code: A Necessity for DevSecOps
  • Understanding and Mitigating IP Spoofing Attacks
  • Enhancing Security With ZTNA in Hybrid and Multi-Cloud Deployments

Two Way Communication in JMS

By 
Łukasz Budnik user avatar
Łukasz Budnik
·
May. 29, 12 · Interview
Likes (1)
Comment
Save
Tweet
Share
26.2K 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, DZone MVB. 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.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: