Asynchronous Reply Routers
Asynchronous reply routers are used in request/response scenarios where message traffic is triggered by a request and the traffic needs to be consolidated before a response is given. The classic example of this is where a request is made and tasks are executed in parallel. Each task must finish executing and the results processed before a response can be sent back.
Asynchronous reply routers are only useful with services that use synchronous calls because there is no response when dispatching a message asynchronously. Mule provides aggregator routers that can be used in conjunction with a message splitter or recipient list router to aggregate messages before returning a response. For more information on these routers, see Outbound Routers.
Consider the inbound configuration and the asynchronous reply router in the LoanBroker configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <service name="LoanBroker"> <inbound> <vm:inbound-endpoint path="Loan.Requests"/> </inbound> <component class="org.mule.samples.loanbroker.SyncLoanBroker"> <outbound> <static-recipient-list-router> <reply-to address="jms://Loan.Quotes"/> <message-property-filter expression="recipients!=null"/> </static-recipient-list-router> </outbound-router> <async-reply> <jms:inbound-endpoint queue="Loan.Quotes"/> <custom-async-reply-router class="org.mule.samples.loanbroker.routers.BankQuotesResponseAggregator"/> </async-reply> </service>
This configuration specifies that the Loan Broker will receive requests from
vm://Loan.Requests and will dispatch multiple requests to different banks via the outbound router. The bank endpoints are defined in a List called 'recipients', which is a property on the outbound message. The important setting on the outbound router is the <`reply-to>` endpoint, which tells Mule to route all responses to the
jms://Loan.Quotes endpoint, which is the endpoint on which the async-reply router is listening. When all responses are received, the
BankQuotesResponseAggregator selects the cheapest quotes and returns it. Mule then handles returning this to the requester. The
<reply-to> endpoint is applied to the next service invoked. For example, if service1 dispatches to service2, and service1 has an outbound router with a reply-to endpoint, service2 will send the results of its invocation to the reply-to endpoint. For more information, see ReplyTo.
If you want to transform a response message without doing any other work on the response, you set the
transformers attribute on the response router without any other routing configuration.
1 <response-router transformers="OrderConfirmationToXml XmlToWebPage"/>
The timeout setting determines how long Mule should wait for replies before returning the result. The default value is determined by the value of the
defaultSynchronousEventTimeout attribute that has been configured for the Mule instance. (For more information, see Global Settings Configuration Reference.) You can also specify an independent timeout value for asynchronous replies for a given service using the optional
timeout attribute on the
failOnTimeout attribute determines whether to throw an exception if the router times out before all expected messages have been received. If set to false (the default), the current messages are returned for processing.
1 2 3 4 <async-reply failOnTimeout="false" timeout="2000"> <inbound-endpoint ref="replyEndpoint"/> <collection-async-reply-router/> </async-reply>
The rest of this page describes the asynchronous reply routers you can use. For detailed information on the elements you configure for asynchronous reply routers, see Asynchronous Reply Router Configuration Reference.
The Single Asynchronous Reply router configures a single response router. This router returns the first message it receives on a reply endpoint and discards the rest.
The Collection Asynchronous Reply router configures a collection response router. This router returns a MuleMessageCollection message type that contains all messages received for the current correlation.