Friday, January 3, 2014

Working with WSO2 ESB Tasks with 'injectTo' property

WSO2 ESB Tasks allows to run scheduled jobs at specified intervals which is triggered by a timer. In this blog post I discuss about how to use 'injectTo' property with tasks.

Injecting the message to a proxy service:

Basic structure as follows:
<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="proxyName"
value="SampleProxy"/>

  1. Use the configuration below
  2. Start inbuilt backend service 'axis2serever' at $ESB_HOME/samples/axis2Server
  3. Here I use SimplestockQuoteService and you need to built it by running ant command at $ESB_HOME/samples/axis2Server/src/SimpleStockQuoteService
  4. Retart ESB and see task triggered 2 times withing 5ms as per below example:
    <trigger count="2" interval="5"/> 

 <?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="SampleProxy"  
      transports="https http"  
      startOnLoad="true"  
      trace="disable">  
    <description/>  
    <target endpoint="A">  
      <inSequence>  
       <log>  
         <property name="LOG"  
              value="======================================================="/>  
       </log>  
      </inSequence>  
      <outSequence>  
       <log>  
         <property name="LOG" value="******************************************"/>  
       </log>  
      </outSequence>  
    </target>  
   </proxy>  
   <endpoint name="A">  
    <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>  
   </endpoint>  
   <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="SampleInjectToSequenceTask"  
      class="org.apache.synapse.startup.tasks.MessageInjector"  
      group="synapse.simple.quartz">  
    <trigger count="2" interval="5"/>  
    <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="SampleProxy"/>  
    <property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"  
         name="injectTo"  
         value="proxy"/>  
   </task>  
 </definitions>  




Injecting the message to a proxy service:


Basic structure as follows:
<property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
name="injectTo"
value="sequence"/>
.......................
<property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
name="sequenceName"
value="SampleSequence"/>

  1. Use the configuration below.
  2. Same axis2service and SimpleStokQuote service can be used as per above
  3. Retart ESB and see task triggered 3 times withing 10ms as per below example:
    <trigger count="3" interval="10"/>
 <?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="SampleProxy"  
      transports="https http"  
      startOnLoad="true"  
      trace="disable">  
    <description/>  
    <target endpoint="A">  
      <inSequence>  
       <log>  
         <property name="LOG"  
              value="======================================================="/>  
       </log>  
      </inSequence>  
      <outSequence>  
       <log>  
         <property name="LOG" value="******************************************"/>  
       </log>  
      </outSequence>  
    </target>  
   </proxy>  
   <endpoint name="A">  
    <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>  
   </endpoint>  
   <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="recSeq">  
    <log level="full">  
      <property name="MSG" value="===== RESPONSE ====="/>  
    </log>  
    <drop/>  
   </sequence>  
   <sequence name="SampleSequence">  
    <send receive="recSeq">  
      <endpoint>  
       <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>  
      </endpoint>  
    </send>  
   </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="SampleInjectToSequenceTask"  
      class="org.apache.synapse.startup.tasks.MessageInjector"  
      group="synapse.simple.quartz">  
    <trigger count="3" interval="10"/>  
    <property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"  
         name="injectTo"  
         value="sequence"/>  
    <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="sequenceName"  
         value="SampleSequence"/>  
   </task>  
 </definitions>  

No comments:

Post a Comment