Friday, February 27, 2015

Writing a simple scheduling task with WSO2 ESB 4.8.0

WSO2 ESB tasks let you run scheduled jobs at given time intervals. You can create custom tasks as well which is explain in In this example, I will be using the default task MessageInjector, that is available with ESB pack.  Following is the task configuration.

<task xmlns="" name="CheckQuote" class="org.apache.synapse.startup.tasks.MessageInjector" group="synapse.simple.quartz">
    <trigger interval="60"/>
    <property xmlns:task="" name="message">
        <m0:getQuote xmlns:m0="http://services.samples">
    <property xmlns:task="" name="injectTo" value="sequence"/>
    <property xmlns:task="" name="sequenceName" value="iterateSequence"/>

trigger interval property will allow you to configure the time interval of the task to be scheduled. In this sample I have included 60s as the time interval, so that the task will run every minute. Under message property, I am passing a default get quote payload of the sample SimpleStockQuote service.
injectTo property will define that I need to inject this constructed message to a sequence named iterateSequence.

Following is the iterateSequence I have defined.

<sequence xmlns="" name="iterateSequence">
    <iterate xmlns:m0="http://services.samples" preservePayload="true" attachPath="//m0:getQuote" expression="//m0:getQuote/m0:request">
                        <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                <log level="custom">
                    <property xmlns:ax21="http://services.samples/xsd" xmlns:ns="http://services.samples" name="Stock_Quote_on" expression="//ns:return/ax21:lastTradeTimestamp/child::text()"/>
                    <property xmlns:ax21="http://services.samples/xsd" xmlns:ns="http://services.samples" name="For_the_organization" expression="//ns:return/ax21:name/child::text()"/>
                    <property xmlns:ax21="http://services.samples/xsd" xmlns:ns="http://services.samples" name="Last_Value" expression="//ns:return/ax21:last/child::text()"/>

Within the iterate sequence, I am using the call mediator to invoke the given endpoint with the message payload that is passed. To run this sample make sure that the SimpleStockQuoteService is running. When this task is scheduled to run, you can see that quotes for IBM,WSO2 and MSTF is printed every minute.

No comments:

Post a Comment