Thursday, July 14, 2011

Mule - Single Async Reply router

Single Async Reply Routers is one of the Async reply routers supported by Mule. Async reply routers are used in scenarios where the message traffic is triggered with a request and the response has to be collected from different endpoints before it is returned. Single Async Reply Router returns the first message it receives on its inbound endpoint and discards the rest.

The following example shows the usage of the singe-async-reply-router using an HTTP request.

mule-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2"
    xmlns:file="http://www.mulesource.org/schema/mule/file/2.2"
    xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2" xmlns:smtp="http://www.mulesource.org/schema/mule/smtp/2.2"
    xmlns:http="http://www.mulesource.org/schema/mule/http/2.2"
    xmlns:spring="www.springframework.org/schema/beans"
    xsi:schemaLocation="
       http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
       http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd
       http://www.mulesource.org/schema/mule/file/2.2 http://www.mulesource.org/schema/mule/file/2.2/mule-file.xsd
       http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd
       http://www.mulesource.org/schema/mule/smtp/2.2 http://www.mulesource.org/schema/mule/smtp/2.2/mule-smtp.xsd
       http://www.mulesource.org/schema/mule/http/2.2 http://www.mulesource.org/schema/mule/http/2.2/mule-http.xsd
       www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans-2.5.xsd">


    <model>
        <service name="InputService">
            <inbound>
                <!-- The inbound endpoint is still synchronous -->
                <http:inbound-endpoint host="localhost"
                    port="8000" path="getMessage" synchronous="true" />
            </inbound>
            <outbound>
                <pass-through-router>
                    <!-- The outbound endpoint is asynchronous -->
                    <vm:outbound-endpoint path="processMessage"
                        synchronous="false" />
                </pass-through-router>
            </outbound>
            <!-- The Asynchronous response is processed by this router -->   
            <async-reply>
                <vm:inbound-endpoint path="printData" />
                <single-async-reply-router />
            </async-reply>
        </service>
        <service name="ProcessService">
            <inbound>
                <vm:inbound-endpoint path="processMessage"
                    synchronous="false" />
            </inbound>
            <component class="org.mybusiness.components.ProcessInputComponent" />
            <outbound>
                <pass-through-router>
                    <!-- Returning response to Asynchronous router endpoint --> 
                    <vm:outbound-endpoint path="printData" />
                </pass-through-router>
            </outbound>
        </service>
    </model>
</mule>

Component Class



package org.mybusiness.components;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mybusiness.dtos.Name;


public class ProcessInputComponent {
Log LOG = LogFactory.getLog(ProcessInputComponent.class);


public String processData(String data) {
StringBuffer sb = new StringBuffer();
sb.append("The data entered is : ").append(data);
sb.append("\n");
sb.append("The length of the data is : ").append(data.length());
return sb.toString();
}
}

Sample Response

1 comment: