Friday, May 6, 2011

Executing Groovy scripts using WSO2 ESB

Following post will discuss how to execute a Groovy script using the WSO2 ESB's script mediator [1].

1) Download a distribution of the WSO2 ESB [2].

2) Extract the zip file. The directory created will be referred as ESB_HOME.

3) Go to the ESB_HOME/samples/axis2Server/src/SimpleStockQuoteService directory. Run "ant" to build the service. A service archive will be created at ESB_HOME/samples/axis2Server/repository/services. The generated service archive can be deployed at an Axis2 server.
eg. SimpleStockQuoteService.aar

4) Copy the groovy-all-1.1-rc-1.jar into ESB_HOME/repository/components/lib/.

5) Go to the ESB_HOME/bin and run the script which starts wso2server.
eg. wso2server.bat for windows environments
wso2server.sh for linux environments

6) Once the server stats, log into the Mangement Console using following credentials (following are the default username/password).
username: admin
password: admin

7) Add the following proxy to the Synapse configuration using the Source View tab.
<proxy xmlns="http://ws.apache.org/ns/synapse" name="groovyProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<script language="groovy">println "The current time is: ${new Date()}"</script>
<send>
<endpoint name="endpoint_urn_uuid_4ECE7CA834EC574477533951">
<address uri="http://localhost:9000/services/SimpleStockQuoteService" />
</endpoint>
</send>
</inSequence>
</target>
</proxy>
8) Execute the client using the following command.
heshan@heshan-ThinkPad:~/Dev/wso2esb-4.0.0-SNAPSHOT/samples/axis2Client$ ant stockquote -Daddurl=http://localhost:8280/services/groovyProxy
9) Following message will be printed at ESB console.
The current time is: Fri May 06 14:00:40 IST 2011
References
[1] - http://wso2.org/project/esb/java/3.0.1/docs/mediators/script.html
[2] - http://wso2.org/downloads/esb