## Apache Synapse

## Sample 1: Simple Content Based Routing (CBR) of Messages

<definitions xmlns="http://ws.apache.org/ns/synapse"> <sequence name="main"> <!-- filtering of messages with XPath and regex matches --> <filter source="get-property('To')" regex=".*/StockQuote.*"> <then> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> <drop/> </then> </filter> <send/> </sequence> </definitions>

### Objective

Introduction to simple content based routing - Shows how a message could be made to pass through Synapse using the dumb client mode, where Synapse acts as a gateway to accept all messages and then perform mediation and routing based on message properties or content.

### Pre-requisites

- Deploy the SimpleStockQuoteService in the sample Axis2 server and start Axis2
- Start Synapse using the configuration numbered 1 (repository/conf/sample/synapse_sample_1.xml)
  Unix/Linux: sh synapse.sh -sample 1
  Windows: synapse.bat -sample 1

### Executing the Client

Execute the sample client in the dumb client mode using the following command.

ant stockquote -Dtrpurl=http://localhost:8280/services/StockQuote

This time you will see Synapse receiving a message for which Synapse was set as the ultimate receiver. The filter mediator in the main sequence performs a regular expression match on the 'To' header (http://localhost:8280/services/StockQuote) to check whether it matches the expression ".*/StockQuote.*". Since the 'To' header matches this expression the child mediators of the filter mediator get executed. As a result, the message is sent to the Axis2 server. The drop mediator after the send mediator terminates the flow of the sequence. Axis2 server will print the following log when it receives the stock quote request from Synapse.

Sat Nov 18 21:01:23 IST 2006 SimpleStockQuoteService :: Generating quote for : IBM

During response processing, the filter condition fails, and thus the child mediators of the filter are skipped. The 'send' mediator at the end of the sequence forwards the response back to the client using the implicit 'To' address. The client will print a message similar to the following when it receives the response.

Standard :: Stock price = $95.26454380258552

Back to Catalog