Sample 157: Conditional Router Mediator for Implementing Complex Routing Scenarios

<definitions xmlns=""> <proxy name="StockQuoteProxy" transports="https http" startOnLoad="true" trace="disable"> <target> <inSequence> <conditionalRouter continueAfter="false"> <conditionalRoute breakRoute="false"> <condition> <match xmlns="" type="header" source="foo" regex="bar.*"/> </condition> <target sequence="cnd1_seq"/> </conditionalRoute> <conditionalRoute breakRoute="false"> <condition> <and xmlns=""> <match type="header" source="my_custom_header1" regex="foo.*"/> <match type="url" regex="/services/StockQuoteProxy.*"/> </and> </condition> <target sequence="cnd2_seq"/> </conditionalRoute> <conditionalRoute breakRoute="false"> <condition> <and xmlns=""> <match type="header" source="my_custom_header2" regex="bar.*"/> <equal type="param" source="qparam1" value="qpv_foo"/> <or> <match type="url" regex="/services/StockQuoteProxy.*"/> <match type="header" source="my_custom_header3" regex="foo.*"/> </or> <not> <equal type="param" source="qparam2" value="qpv_bar"/> </not> </and> </condition> <target sequence="cnd3_seq"/> </conditionalRoute> </conditionalRouter> </inSequence> <outSequence> <send/> </outSequence> </target> </proxy> <sequence name="cnd1_seq"> <log level="custom"> <property name="MSG_FLOW" value="Condition (I) Satisfied"/> </log> <sequence key="send_seq"/> </sequence> <sequence name="cnd2_seq"> <log level="custom"> <property name="MSG_FLOW" value="Condition (II) Satisfied"/> </log> <sequence key="send_seq"/> </sequence> <sequence name="cnd3_seq"> <log level="custom"> <property name="MSG_FLOW" value="Condition (III) Satisfied"/> </log> <sequence key="send_seq"/> </sequence> <sequence name="send_seq"> <log level="custom"> <property name="DEBUG" value="Condition Satisfied"/> </log> <send> <endpoint name="simple"> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> </sequence> </definitions>


Conditional router mediator can be used to implement complex routing rules in Synapse. It can route messages to various endpoints based on URLs, query parameters and transport headers. This sample demonstrates how to use the conditional router mediator within a proxy service to build a smart routing proxy.


  • Deploy the SimpleStockQuoteService in the sample Axis2 server a d start Axis2 server.
  • Start Synapse using the configuration numbered 157 (repository/conf/sample/synapse_sample_157.xml)
    Unix/Linux: sh -sample 157
    Windows: synapse.bat -sample 157

Executing the Client

We will be using 'curl' as the client in this scenario. Curl is a neat little command line tool that can be used to generate various types of HTTP requests (among other things).

First create a sample input file named stockQuoteReq.xml with the following content.

<soap:Envelope xmlns:soap="" xmlns:ser="http://services.samples"> <soap:Header/> <soap:Body> <ser:getQuote> <ser:request> <ser:symbol>IBM</ser:symbol> </ser:request> </ser:getQuote> </soap:Body> </soap:Envelope>

Invoke curl as follows to see header based routing feature in action.

curl -d @stockQuoteReq.xml -H "Content-Type: application/soap+xml;charset=UTF-8" -H "foo:bar" "http://localhost:8280/services/StockQuoteProxy"

This sends a HTTP request with a custom header named 'foo'. Proxy service will detect this header and print a custom log message confirming the receipt of the request.

Now invoke curl as follows to test a combination header and URL based routing.

curl -d @stockQuoteReq.xml -H "Content-Type: application/soap+xml;charset=UTF-8" -H "my_custom_header1:foo1" "http://localhost:8280/services/StockQuoteProxy"

Finally invoke curl as follows to test routing based on complex conditions.

curl -d @stockQuoteReq.xml -H "Content-Type: application/soap+xml;charset=UTF-8" -H "my_custom_header2:bar" -H "my_custom_header3:foo" "http://localhost:8280/services/StockQuoteProxy?qparam1=qpv_foo&qparam2=qpv_foo2"

In each case Synapse will log a different log entry because the conditional router mediator uses different sequences to process the three messages.

Back to Catalog