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 https://docs.wso2.com/display/ESB480/Writing+Tasks. In this example, I will be using the default task MessageInjector, that is available with ESB pack.  Following is the task configuration.


<task xmlns="http://ws.apache.org/ns/synapse" name="CheckQuote" class="org.apache.synapse.startup.tasks.MessageInjector" group="synapse.simple.quartz">
    <trigger interval="60"/>
    <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:request>
                <m0:symbol>MSTF</m0:symbol>
            </m0:request>
            <m0:request>
                <m0:symbol>WSO2</m0:symbol>
            </m0:request>
        </m0:getQuote>
    </property>
    <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="iterateSequence"/>
</task>

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="http://ws.apache.org/ns/synapse" name="iterateSequence">
    <iterate xmlns:m0="http://services.samples" preservePayload="true" attachPath="//m0:getQuote" expression="//m0:getQuote/m0:request">
        <target>
            <sequence>
                <call>
                    <endpoint>
                        <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                    </endpoint>
                </call>
                <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()"/>
                </log>
            </sequence>
        </target>
    </iterate>
</sequence>

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