How to Schedule a Task Using WSO2 ESB 4.9.0

Schedule task is one of the very useful hidden features that comes with WSO2 ESB 4.9.0. This is a much improved and reliable version of schedule task comparing to previous versions of ESB. This Task Scheduler can be worked with clustered environments such as 1 manager and 2 worker..etc. Let's start hacking the WSO2 ESB

First We have to create a Sample Back-End Services. The back-end sample services come with a pre-configured Axis2 server, and demonstrates in-only and in-out SOAP/REST or POX messaging over HTTP/HTTPS and JMS transports using WS-Addressing, WS-Security, and WS-Reliable Messaging. They also handle binary content using MTOM and SwA.
1. Each back-end sample service can be found in a separate folder in <ESB_HOME>/samples/axis2Server/src directory. They can be built and deployed using Ant from each service directory. You can do this by typing "ant" without quotes on a console from a selected sample directory. For example,
user@host:/tmp/wso2esb-2.0/samples/axis2Server/src/SimpleStockQuoteService$ ant
Buildfile: build.xml
 ...
build-service:
   ....
      [jar] Building jar: /tmp/wso2esb-2.0/samples/axis2Server/repository/services/SimpleStockQuoteService.aar
BUILD SUCCESSFUL
Total time: 3 seconds
2. Next, start the Axis2 server. Go to <ESB_HOME>/samples/axis2Server directory and execute either axis2server.sh (for Linux) or axis2server.bat(for Windows) script. For example,
C:/wso2/wso2esb/samples/axis2Server>axis2server.bat
./axis2server.sh
This starts the Axis2 server with the HTTP transport listener on port 9000 and HTTPS on 9002 respectively.
3. Now add the sample sequence, Please follow below steps.
i.Click on Sequences Under Service Bus from left pane.
ii.Click on Add Sequences.
iii.Then click on "switch to source view" from the tab

iv.And delete everything in that box and add below sequence.
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="iterateSequence" xmlns="http://ws.apache.org/ns/synapse">
    <iterate attachPath="//m0:getQuote"
        expression="//m0:getQuote/m0:request" preservePayload="true"
        xmlns:m0="http://services.samples"
        xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd">
        <target>
            <sequence>
                <call>
                    <endpoint>
                        <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                    </endpoint>
                </call>
                <log level="custom">
                    <property
                        expression="//ns:return/ax21:lastTradeTimestamp/child::text()"
                        name="Stock_Quote_on" xmlns:ax21="http://services.samples/xsd"/>
                    <property
                        expression="//ns:return/ax21:name/child::text()"
                        name="For_the_organization" xmlns:ax21="http://services.samples/xsd"/>
                    <property
                        expression="//ns:return/ax21:last/child::text()"
                        name="Last_Value" xmlns:ax21="http://services.samples/xsd"/>
                </log>
            </sequence>
        </target>
    </iterate>
</sequence>

Note: you have to change the endpoint address if your running SimpleStockQuoteService in some other endpoint.

v. Save & Close the view.

4. Let's add the scheduling task.
Click on Scheduled Tasks Under Service Bus from the left pane. Select add task and fill the fields like below.


i. Task Name - CheckQuote
ii. Task Group - synapse.simple.quartz
iii. Task Implementation - org.apache.synapse.startup.tasks.MessageInjector
iv. Set below properties 
sequenceName - Literal - iterateSequence
injectTo - Literal - sequence
message - XML - 
<m0:getQuote xmlns:m0="http://services.samples">
         <m0:request>
            <m0:symbol>IBM</m0:symbol>
         </m0:request>
         <m0:request>
            <m0:symbol>MSTF</m0:symbol>
         </m0:request>
         <m0:request>
            <m0:symbol>WSO2</m0:symbol>
         </m0:request>
      </m0:getQuote>

v. Trigger type - Single
vi. Count - 100 
Note: This means the task will run 100 times. If you want to set an infinite tasks simply set the count to -1.
vii. Interval (in seconds) - 10


Thant's it! Now click on Schedule button and the task will start execution according to the Interval. In this example, task will start in 10 seconds. 

Kindly find the schedule task xml code from below:

<task class="org.apache.synapse.startup.tasks.MessageInjector"
        group="synapse.simple.quartz" name="CheckQuote">
        <trigger count="100" interval="10"/>
        <property name="sequenceName" value="iterateSequence" xmlns:task="http://www.wso2.org/products/wso2commons/tasks"/>
        <property name="message" xmlns:task="http://www.wso2.org/products/wso2commons/tasks">
            <m0:getQuote xmlns:m0="http://services.samples">
                <m0:request>
                    <m0:symbol>IBM</m0:symbol>
                </m0:request>
                <m0:request>
                    <m0:symbol>MSTF</m0:symbol>
                </m0:request>
                <m0:request>
                    <m0:symbol>WSO2</m0:symbol>
                </m0:request>
            </m0:getQuote>
        </property>
        <property name="injectTo" value="sequence" xmlns:task="http://www.wso2.org/products/wso2commons/tasks"/>
    </task>
                        

Reference 
https://docs.wso2.com/display/ESB481/Adding+and+Scheduling+Tasks
https://docs.wso2.com/display/ESB480/ESB+Samples+Setup
http://himashag.blogspot.com/2015/02/writing-simple-scheduling-task-with.html


Comments