Friday, February 27, 2015

No more manic mondays!

It is quite a surprise to myself that I don't have any more manic mondays in my calendar. Before I joined WSO2, mondays have always been blue and depressing. I guess that's how we all feel after a good weekend. Having to wake up everyday and cram yourself with work/school is hectic for most.

To be honest, Few years back it was the same for me.


After joining  WSO2, this thought has changed evidently. When joining I just had a very average interest in coding. There was not much of hyped up passion about coding. Within few months I realized that this has deliberately changed. Growing passion to learn all the new technologies, to try out research on content, to have that 'special interest' in what you do. It's quite inexplainable on how or why/when it all brought a different viewpoint.

I guess it's the responsibility, the personal growth that is allowed within WSO2. The culture ,personal branding , encouragement to explore more all this together have changed things. Because once you are passionate about the work you do, it's no longer called working.As dr.Sanjiva once said, "If you wake up every monday thinking, oh god have to go to work, you are certainly not doing the right job."  It's not that the work load is less or it's all easy, BUT the attitude I had before has changed positively. I love my job, I love the work that I am entitled with!

Passion is the key to everything.It will change your attitude towards things and it will help you out in your life. :)

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.




Tuesday, February 10, 2015

Using xslt mediator to update file transported with VFS

There can be situations where you need to update xml files when mediating through an ESB. Wso2 ESB supports this ,with the use of xslt mediator where you can add your own xslt definition and perform the transformations.

In this example, I will be looking at a sample where you need to transform a file located in your local file system. First of all, you need to download a fresh binary pack of ESB( http://wso2.com/products/enterprise-service-bus/).  
First of all, we need to enable vfs transport in esb. After unzipping the downloded esb pack following configurations need to be made .
In <ESB_HOME>/repository/conf/axis2/axis2.xml uncomment below lines. 



<transportreceiver name="vfs" class="org.apache.synapse.transport.vfs.VFSTransportListener"/>
...
<transportSender name="vfs" class="org.apache.synapse.transport.vfs.VFSTransportSender"/>

Next step is to write your xslt mediator as below.  
<definitions xmlns="http://ws.apache.org/ns/synapse">
<registry provider="org.wso2.carbon.mediation.registry.ESBRegistry">
<parameter name="root">file:./repository/samples/resources/</parameter>
<parameter name="cachableDuration">15000</parameter>
</registry>
<localEntry key="xslt-key-req" src="file:repository/samples/resources/update.xslt"/>
<proxy name="FileProxy" transports="vfs" startOnLoad="true" trace="disable">
<parameter name="transport.vfs.ActionAfterProcess">MOVE</parameter>
<parameter name="transport.PollInterval">15</parameter>
<parameter name="transport.vfs.MoveAfterProcess">file:///Users/himasha/Desktop/original</parameter>
<parameter name="transport.vfs.FileURI">file:///Users/himasha/Desktop/in</parameter>
<parameter name="transport.vfs.MoveAfterFailure">file:///Users/himasha/Desktop/failure</parameter>
<parameter name="transport.vfs.FileNamePattern">.*\.xml</parameter>
<parameter name="transport.vfs.ContentType">application/xml</parameter>
<parameter name="transport.vfs.ActionAfterFailure">MOVE</parameter>

<target>
<inSequence>
<log level="custom">
<property name="sequence" value="fileProxy"/>
</log>

<xslt key="xslt-key-req"/>

</inSequence>
</target>

</proxy>

</definitions> 

Here the xslt transformation file is retrieved as a registry resource. As above configuration you need to place your xslt file in repository/samples/resources. According to transport.vfs parameter values, if the process is successful then the input.xml file will be moved to the location given in MoveAfterProcess. If it fails, according to MoveAfterFailure value it will be moved to a folder named failure. (Note that you need to create these folders beforehand). Information and location about the file to be transformed is given in parameters FileUri,FileNamePattern,ContentType. Note that here since my input file is a .xml the contentType and FileNamePattern values are added to match that. 

When running this sample, when the in sequence is getting executed you may see a property log as added in the custom log. Also if u need to store the transformed file back to local file system you need to add an endpoint as below. 
<endpoint>
    <address uri="vfs:file:///home/user/test/out"/>
</endpoint>