Wednesday, March 4, 2015

Service chaining with WSO2 ESB

There might be scenarios, where you need to process a BE(backend) call, get that response and pass it to another BE call. Usually when you use send mediator in your insequence,  your response can be retrieved from your out sequence. But if you need to change this behavior you need to use service chaining. 

This can easily be achieved with 'send receive' of WSO2 ESB. For an example, let us look at below synapse configuration. In this, I'm using a scheduled task, to send a quote message to a proxy every 5 seconds. In the insequence I'm sending this injected message to StockQuoteEpr endpoint which will give a response. This response will be sent to CustomSequence instead of the out sequence. For more information on this, you can refer https://docs.wso2.com/display/ESB460/Send+Mediator

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://ws.apache.org/ns/synapse">
   <registry provider="org.wso2.carbon.mediation.registry.WSO2Registry">
      <parameter name="cachableDuration">15000</parameter>
   </registry>

   <proxy name="testProxy"
          transports="https http"
          startOnLoad="true"
          trace="disable">
      <description/>
      <target>
         <inSequence>
          <send receive="CustomSequence">
            <endpoint key="StockQuoteEpr"/> 
          </send>
         </inSequence>
      </target> 
   </proxy>

   <endpoint name="StockQuoteEpr">
     <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
   </endpoint>
   
   <sequence name="CustomSequence">
      <log level="full">
         <property name="MESSAGE" value="============MSG========="/>
      </log>
   </sequence>

   <sequence name="fault">
      <log level="full">
         <property name="MESSAGE" value="Executing default 'fault' sequence"/>
         <property name="ERROR_CODE" expression="get-property('ERROR_CODE')"/>
         <property name="ERROR_MESSAGE" expression="get-property('ERROR_MESSAGE')"/>
      </log>
      <drop/>
   </sequence>

   <sequence name="main">
      <in>
         <log level="full"/>
         <filter source="get-property('To')" regex="http://localhost:9000.*">
            <send/>
         </filter>
      </in>
      <out>
         <send/>
      </out>
      <description>The main sequence for the message mediation</description>
   </sequence>

   <task name="InjectToProxyTask"
         class="org.apache.synapse.startup.tasks.MessageInjector"
         group="synapse.simple.quartz">
      <trigger count="5" interval="5"/>
      <property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
                name="soapAction"
                value="getQuote"/>
      <property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
                name="format"
                value="soap11"/>
      <property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
                name="injectTo"
                value="proxy"/>
      <property xmlns:task="http://www.wso2.org/products/wso2commons/tasks" name="message">
         <m0:getQuote xmlns:m0="http://services.samples">
            <m0:request>
               <m0:symbol>IBM</m0:symbol>
            </m0:request>
         </m0:getQuote>
      </property>
      <property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
                name="proxyName"
                value="testProxy"/>
   </task>
   
</definitions>

No comments:

Post a Comment