Monday, May 18, 2015

Cron style file transfer with WSO2 ESB File Connector

Given a requirement where you would need to perform a file transfer as a cron job (ex: transfer files during weekends only) , you will have to use a scheduled task. But the default VFS transport mechanism would not work with a scheduled task. Therefore, you should use file connector to perform the file transfers. [1]

1.First of all you need to download file connector from wso2 store.
2.  Go through [2] and enable the file connector zip, downloaded from step 1.
3.Now that you have enabled File connector, you can have a sample proxy configuration like below. (Note: I have used ESB 4.8.0 )

proxy name="SampleProxy" transports="https http" startOnLoad="true" trace="disable">
<target>
<inSequence>
<property name="filelocation" expression="json-eval($.filelocation)"/>
<property name="file" expression="json-eval($.file)"/>
<property name="newfilelocation" expression="json-eval($.newfilelocation)"/>
<property name="filebeforeprocess" expression="json-eval($.filebeforeprocess)"/>
<property name="fileafterprocess" expression="json-eval($.fileafterprocess)"/>
<fileconnector.move>
<filelocation>{$ctx:filelocation}</filelocation>
<file>{$ctx:file}</file>
<newfilelocation>{$ctx:newfilelocation}</newfilelocation>
<filebeforeprocess>{$ctx:filebeforeprocess}</filebeforeprocess>
<fileafterprocess>{$ctx:fileafterprocess}</fileafterprocess>
</fileconnector.move>
<respond/>
</inSequence>
</target>
<description/>
</proxy>


4. Next add a schedule task [3], that would perform this proxy according to given cron expression.
    You can give below values for the schedule task properties.

Task Name : SampleInjectToSequenceTask
Task group : synapse.simple.quartz
Task Implementation :org.apache.synapse.startup.tasks.MessageInjector
Trigger Type : Cron
Cron : * * * * 6-0 ? (add your specific cron expression)
By clicking 'Load Task Properties' button(near Task Implementation) you can add below property values.
injectTo : proxy
proxyName : SampleProxy
message (xml): <m0:getQuote xmlns:m0="http://services.samples">
<m0:request>
<m0:symbol>IBM</m0:symbol>
</m0:request>
</m0:getQuote> 

5. Create a sample JSON input file like below. (sampleFileConnector.xml )

{
    "filelocation":"file:///Users/himasha/Desktop/in/",
    "file":"fileSample.txt",
    "newfilelocation":"file:///Users/himasha/Desktop/out/",
    "filebeforeprocess":"file:///Users/himasha/Desktop/before/",
    "fileafterprocess":"file:///Users/himasha/Desktop/after/"
}

6. Use this curl command to test your proxy configuration.
url -v -X POST  -d @Desktop/sampleFileConnector.xml -H "Content-type: application/json" http://localhost:8280/services/SampleProxy

7. You can look into [4] to get more information on different operations/ properties that are available in File Connector.  Also go through [5] to refer more on WSO2 ESB connectors.

 [1] https://docs.wso2.com/display/ESBCONNECTORS/File+Connector
[2] hhttps://docs.wso2.com/display/ESB481/Managing+Connectors+in+Your+ESB+Instance
[3]  https://docs.wso2.com/display/ESB481/Adding+and+Scheduling+Tasks#AddingandSchedulingTasks
[4] https://docs.wso2.com/display/ESBCONNECTORS/Working+with+the+File+Connector
[5] https://docs.wso2.com/display/ESBCONNECTORS/WSO2+ESB+Connectors+Documentation

No comments:

Post a Comment