Tuesday, May 10, 2016

Task scheduling through WSO2 ESB 4.9.0

Introduction

WSO2 ESB is capable of scheduling and executing tasks periodically. A task can be scheduled to run n number of times in a given t time duration. Also we can schedule a task to run just once after WSO2 ESB starts. If we want to get more control in task scheduling then we can use cron-expression. Let's say as an example we can schedule a task to run everyday at 5 PM or on 25th at 5 PM every month, likewise.

Example

As a first step we have to create a sample back-end service. Here I use a sample service which exists in WSO2 ESB samples folder.

step 1


If you go to  <ESB_HOME>/samples/axis2Server/src  directory,  you will see several back-end samples are available there. 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.  Here I will choose SimpleStockQuoteService service.


Step 2 


As a next step, go to <ESB_HOME>/samples/axis2Server directory and execute axis2server.sh (for Linux) to start the Axis 2 server. This starts the Axis2 server with the HTTP transport listener on port 9000 and HTTPS on 9002 respectively.

Step 3 


Now add a sample sequence in the WSO2 ESB as below.
Click on Sequences under Service Bus in Manage menu and click Add Sequences



Then switch to the source view by clicking on  switch to source view and replace the following content and click save and close button to save the content.

<?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>



Step 4 


As a next step, add a Scheduled Task by clicking on Scheduled Tasks under Service Bus in Manage menu. And set its configuration as below.


Step 5


As final step click on Schedule button and task will start execution according to the Interval. Here in this example task will run 50 times and start in 10 seconds. 

No comments:

Post a Comment