Contact Us 1-800-596-4880

Listen For A Reply - Mule 4

Request and Reply is a very common pattern between applications. AMQP Connector provides an operation specially designed for this, called publish-consume. The publish-consume operation enables you to publish a message to any exchange, and then wait for a reply on a different destination.

When using this operation, the replyTo property of the outgoing message will contain the ID of the queue at which your app will be waiting for a response. This destination may be a well-known destination, or it may be a temporary queue 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.

Wait 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:

<amqp:publish-consume
	config-ref="config"
	exchangeName="targetExchange"
	maximumWait="20"
	maximumWaitUnit="SECONDS"/>

In this example, the operation will publish a message with the current payload as its body and the replyTo property of the message configured to a temporary destination, to the targetExchange. 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 AMQP:TIMEOUT error).

Receive 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 configure the reply-to parameter on the outgoing Message:

<amqp:publish-consume config-ref="config" exchangeName="targetExchange">
    <amqp:message>
    		<amqp:properties reply-to="replyToQueue" />
    </amqp:message>
</amqp:publish-consume>

Now the operation will publish a message to the targetExchange with the current payload as its body and the replyTo property configured with the replyToQueue. Then, it will automatically try to consume the response from the replyToQueue queue until a message is consumed or the maximumWait is reached (in which case it will fail with a AMQP:TIMEOUT error).

See Also

View on GitHub