Content Based Routing

Sample Number

119

Level

Introductory

Description

This sample demonstrates Content Based Routing using UltraESB

Use Case

I want to intermediate and examines the message content and route it to a specific recipient based on data contained in the message. The routing can be based on a number of criteria such as existence of fields, specific field values, message headers etc..

CBR

Sample Configurations

XML Content based routing

In the following configuration, proxy service which is exposed on the HTTP/S transports, mediate the incoming message ,examine the message content and checks for the first order symbol.  Based on the content it forwards the message to the specific destination.

Proxy service configuration for the restful proxy service

 1<u:proxy id="CBRProxy">
 2    <u:transport id="http-8280"/>
 3    <u:target>
 4        <u:inSequence>
 5            <u:java><![CDATA[
 6                    if (mediation.getXMLSupport().filter(msg, "//order[1]/symbol", "IBM")) {
 7                        mediation.sendToEndpoint(msg, "echoService1");
 8                    } else if (mediation.getXMLSupport().filter(msg, "//order[1]/symbol", "YAHO")) {
 9                        mediation.sendToEndpoint(msg, "echoService2");
10                    } else {
11                        mediation.getSOAPSupport().setPayloadToSOAP11Fault(msg, null, "First order must be for the symbol IBM or YAHO", null);
12                        mediation.sendResponse(msg, 500);
13                    }
14                ]]></u:java>
15        </u:inSequence>
16        <u:outDestination>
17            <u:address type="response"/>
18        </u:outDestination>
19    </u:target>
20</u:proxy>
SOAP header based routing

Similar to the above you can route messages based on the SOAP header.

Proxy service configuration for the restful proxy service

 1<u:proxy id="CBRSOAPHeaderProxy">
 2     <u:transport id="http-8280"/>
 3     <u:target>
 4         <u:inSequence>
 5             <u:java><![CDATA[
 6                     if ("xadmin;server1;community#1.0##".equals(
 7                             mediation.getSOAPSupport().getSoapHeaderAsString(msg, "http://someuri", "routing"))) {
 8                         mediation.sendToEndpoint(msg, "echoService1");
 9                     } else if ("xadmin;server2;community#2.0##".equals(
10                             mediation.getSOAPSupport().getSoapHeaderAsString(msg, "http://someuri", "routing"))) {
11                         mediation.sendToEndpoint(msg, "echoService2");
12                     } else {
13                         mediation.getSOAPSupport().setPayloadToSOAP11Fault(msg, null, "Invalid routing header", null);
14                         mediation.sendResponse(msg, 500);
15                     }
16                 ]]></u:java>
17         </u:inSequence>
18         <u:outDestination>
19             <u:address type="response"/>
20         </u:outDestination>
21     </u:target>
22 </u:proxy>
Transport header based routing

You also can route the message to a specific destination based on the transport header. In the following proxy configuration it checks the transport header 'routing' and based on the header message is forwarded to the particular destination.

Proxy service configuration for the restful proxy service

 1<u:proxy id="CBRTransportHeaderProxy">
 2     <u:transport id="http-8280"/>
 3     <u:target>
 4         <u:inSequence>
 5             <u:java><![CDATA[
 6                     if ("xadmin;server1;community#1.0##".equals(msg.getFirstTransportHeader("routing"))) {
 7                         mediation.sendToEndpoint(msg, "echoService1");
 8                     } else if ("xadmin;server2;community#2.0##".equals(msg.getFirstTransportHeader("routing"))) {
 9                         mediation.sendToEndpoint(msg, "echoService2");
10                     } else {
11                         mediation.getSOAPSupport().setPayloadToSOAP11Fault(msg, null, "Invalid routing header", null);
12                         mediation.sendResponse(msg, 500);
13                     }
14                 ]]></u:java>
15         </u:inSequence>
16         <u:outDestination>
17             <u:address type="response"/>
18         </u:outDestination>
19     </u:target>
20 </u:proxy>

In Action

To run this sample, start the sample configuration number 119 from the command line as follows.

Running the sample from startup script

$ cd /opt/ultraesb-2.6.1/bin
$ ./ultraesb.sh -sample 119

Now start the SOA Toolbox, and fire-up the sample Jetty servers on port 9000 and port 9001 that holds sample echo services for testing. Now you have two test echo services with service URLs http://localhost:9000/service/EchoService and http://localhost:9001/service/EchoService.

To test the 'CBRProxy', select file '/resources/samples/resources/requests/1K_buyStocks.xml' as the message body and send a 'POST' request to 'http://localhost:8280/service/CBRProxy'. Since the 1st order symbol of this message is 'IBM' it will be routed to EchoService running on port 9000 and send the echo response. When you select the file '/resources/samples/resources/requests/1K_buyStocks_order.xml' , since the 1st order symbol of this message is 'YAHO' it will be routed to  EchoService running on port 9001.

To test the 'CBRSOAPHeaderProxy', select file '/resources/samples/resources/requests/1K_buyStocks.xml' as the message body and send a 'POST' request to 'http://localhost:8280/service/CBRSOAPHeaderProxy'. Since the SOAP header 'routing'  of this message is 'xadmin;server1;community#1.0##' it will be routed to EchoService running on port 9000 and send the echo response. When you select the file '/resources/samples/resources/requests/1K_buyStocks_order.xml' , since the SOAP header 'routing'  of this message is 'xadmin;server2;community#2.0##' it will be routed to  EchoService running on port 9001.

To test the 'CBRTransportHeaderProxy', select file '/resources/samples/resources/requests/1K_buyStocks.xml' as the message body and send a 'POST' request to 'http://localhost:8280/service/CBRTransportHeaderProxy' with transport header ’routing: xadmin;server1;community#1.0##'. This will be routed to  EchoService running on port 9000 and send the echo response. When you send a message with header 'routing: xadmin;server2;community#2.0##' message will be routed to  EchoService running on port 9001.

In this topic
In this topic
Contact Us