Search code examples
jakarta-eejms

JMS ReplyTo - How does it work?


The JMS API allows messages to declare a replyTo Destination instance. (i.e. the superclass of Queue, Topic). A service could then send a reply message to the sender using this queue.

Are there any restrictions on what Destination can be set as a ReplyTo value? That seems unlikely to work, for the service may not even have any network route to the defined Destination and therefore could not return any message. Does JMS somehow assert the validity (reachability) of a provided Destination? Or is it simply up to the service to try to respond on the given Destination and fail if necessary.


Solution

  • Scenario 1

    In this scenario the Destination is pre-configured and hence proven to work. There is not much value to set this destination as a value for JMSReplyTo header as the receiver might already know about the existence of this pre-configured destination.

    Scenario 2

    In this scenario the sender creates a temporary Destination and receiver will know about this only by calling getJMSReplyTo() method on the received Message. This kind of establishes a private channel between sender and receiver. Read this nice article on pros and cons of temporary destinations.

    Sample code

    Queue tempQueue = qSession.createTemporaryQueue();
    TextMessage request = qSession.createTextMessage();
    request.setJMSReplyTo(tempQueue);
    QueueReceiver qReceiver = qSession.createReceiver(tmpQueue);
    Message response = qReceiver.receive();
    

    edit: Add a web archive link for the original http://onjava.com/onjava/2007/04/10/designing-messaging-applications-with-temporary-queues.html