Contact Us 1-800-596-4880

Asynchronous Reply Routers

Overview

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.

Example

Consider the inbound configuration and the asynchronous reply router in the LoanBroker configuration:

<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.

Response Transformers

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.

<response-router transformers="OrderConfirmationToXml XmlToWebPage"/>

Time-outs

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 async-reply element.

The optional 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.

For example:

<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.

Single Asynchronous Reply

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.

<single-async-reply-router/>

Collection Asynchronous Reply

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.

<collection-async-reply-router/>

Custom Asynchronous Reply

The Custom Asynchronous Reply router allows you to configure a custom asynchronous reply router. To configure the custom router, set the class attribute to the custom router class.

<custom-async-reply-router class="org.mule.CustomAsyncReplyRouter"/>