Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

To Listen For A Reply

Request/Reply is a very common pattern between applications, thus the new JMS Connector provides an operation specially designed for that, called publish-consume. The publish-consume operation allows you to publish a Message to any destination, and then wait for a reply on a different Destination.

When using this operation, the JMSReplyTo header of the outgoing Message will contain the id of the Destination on which our applicaiton will be waiting for a response. This destination may be a well-known destination, or it may be a temporary destination created on the fly with the only purpose of receiving a single response message.

Finally, the result of the operation will be the Message received as a response, with exactly the same structure as if a consume operation was invoked.

Waiting For A Reply On A Temporary Queue

Using a temporary Queue to wait for a response is very simple, just configure the message being sent and the operation will automatically wait for a response in a temporary Queue:


         
      
1
<jms:publish-consume config-ref="config" destination="targetDestination" maximumWait="20" maximumWaitUnit="SECONDS"/>

In this example, the operation will publish a Message with the current payload as its body and the JMSReplyTo header configured to a temporary destination, to the targetDestination. Then, it will automatically try to consume the response from that temporary Queue until a Message is consumed or the maximumWait is reached (in which case it will fail with a JMS:TIMEOUT error).

Receiving The Reply On A Known Queue

In many cases you don’t want reply queues to be created per-request basis, but instead a well-known Queue is used for replies. For this cases, all you have to do is to configure the reply-to parameter on the outgoing Message:


         
      
1
2
3
4
5
<jms:publish-consume config-ref="config" destination="targetDestination">
    <jms:message>
        <jms:reply-to destination="replyToDestination"/>
    </jms:message>
</jms:publish-consume>

Now the operation will publish a Message to the targetDestination with the current payload as its body and the JMSReplyTo header configured with the replyToDestination. Then, it will automatically try to consume the response from the replyToDestination Queue until a Message is consumed or the maximumWait is reached (in which case it will fail with a JMS:TIMEOUT error).