Sample 156: Service Integration with Specifying the Receiving Sequence

<definitions xmlns="http://ws.apache.org/ns/synapse"> <localEntry key="sec_policy" src="file:repository/conf/sample/resources/policy/policy_3.xml"/> <proxy name="StockQuoteProxy"> <target> <inSequence> <enrich> <source type="body"/> <target type="property" property="REQUEST"/> </enrich> <send receive="SimpleServiceSeq"> <endpoint name="secure"> <address uri="http://localhost:9000/services/SecureStockQuoteService"> <enableSec policy="sec_policy"/> </address> </endpoint> </send> </inSequence> <outSequence> <drop/> </outSequence> </target> </proxy> <sequence name="SimpleServiceSeq"> <property name="SECURE_SER_AMT" expression="//ns:getQuoteResponse/ns:return/ns:last" xmlns:ns="http://services.samples"/> <log level="custom"> <property name="SecureStockQuoteService-Amount" expression="get-property('SECURE_SER_AMT')"/> </log> <enrich> <source type="body"/> <target type="property" property="SecureService_Res"/> </enrich> <enrich> <source type="property" property="REQUEST"/> <target type="body"/> </enrich> <send receive="ClientOutSeq"> <endpoint name="SimpleStockQuoteService"> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> </sequence> <sequence name="ClientOutSeq"> <property name="SIMPLE_SER_AMT" expression="//ns:getQuoteResponse/ns:return/ns:last" xmlns:ns="http://services.samples"/> <log level="custom"> <property name="SimpleStockQuoteService-Amount" expression="get-property('SIMPLE_SER_AMT')"/> </log> <enrich> <source type="body"/> <target type="property" property="SimpleService_Res"/> </enrich> <filter xpath="fn:number(get-property('SIMPLE_SER_AMT')) > fn:number(get-property('SECURE_SER_AMT'))"> <then> <log> <property name="StockQuote" value="SecureStockQuoteService"/> </log> <enrich> <source type="property" property="SecureService_Res"/> <target type="body"/> </enrich> </then> <else> <log> <property name="StockQuote" value="SimpleStockQuoteService"/> </log> </else> </filter> <send/> </sequence> </definitions>

Objective

Synapse is capable of mediating requests among multiple services and managing complex message flows thereby acting as a lightweight orchestration engine. This sample demonstrates how to easily integrate multiple services with Synapse using the 'receiving sequence' feature of Synapse.

Pre-requisites

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

Executing the Client

This sample includes a proxy service which first forwards the client request to the SecureStockQuoteService. Once a response has been received from this service, Synapse will turn around and invoke the SimpleStockQuoteService. To do this proxy service must hold on to the original request in memory. This is done using an enrich mediator. Once Synapse has received a response from the SimpleStockQuoteService it will compare the two responses received from the two services and select the one with the lower stock quote value. This response will be then sent to the client.

The important feature to note here is the 'receive' attribute set on the 'send' mediators. This tells Synapse that responses of those send operations should be directed to the sequences referred by the 'receive' attribute. Therefore the response from the SecureStockQuoteService is directed to the sequence named 'SimpleServiceSeq'. Similarly the response from the SimpleStockQuoteService will be handled by the sequence named 'ClientOutSeq'.

To try this out, execute the stock quote client as follows:

ant stockquote -Daddurl=http://localhost:8280/services/StockQuoteProxy

You can confirm that Synapse invokes both services by going through the console output of the Axis2 server. However Axis2 client will receive only one response back. As far as the client is concerned, only one HTTP transaction is taking place. But Synapse does multiple service invocations with the back-end to make the whole integration scenario tick.

Back to Catalog