The simplest JMS request/reply I could come up with for ActiveMQ and JBoss A-MQ

In case you find yourself needing to set up some JMS request/reply code, I hope this helps (the whole example, including writing this post took less than one hour for me). It’s the simplest example I could come up with for ActiveMQ (and JBoss A-MQ, of course). By simple  I mean it focuses as much on the simple task as possible without adding any other frameworks, magic tricks, or whatever — just pure JMS API.

First, create the request message

Here’s the code:

You’ll notice that, compared to a simple send method, the request/reply is a little more complicated in the following ways: you need to create a temporary reply queue (goes in the JMSReplyTo header), a correlation ID (JMSCorrelationID), and then start listening for a reply message.

When you run this, you should see the following if you log into JBoss A-MQ and browse the messages (ActiveMQ lets you do something similar through its web interface too):

jms-request-reply-example

Notice the JMSCorrelationID and the JMSReplyTo headers are set.

Next, subscribe and reply

The code to respond to the message mirrors the sender’s code:

Assuming that the sender code is running, if you run the code above, you should then see this in your IDE or console:

jms-request-reply-example-2

The full code is available on Github. Good luck out there, and let me know how this demo goes for you.

Leave a Reply

Your email address will not be published. Required fields are marked *