You are viewing an older version of this topic. To go to a different version, use the version menu at the upper-right. +

Scripting Example

This example demonstrates how to invoke a JSR-223 Script from Mule ESB. It also illustrates how to use custom transformers, routing based on the content type of the payload and how to use JVM environment variables in your Mule configuration. The script implements a Greedy Algorithm for giving change.

Running the Application

  1. Make sure you have met the prerequisites and installed Mule according to the nstalling Mule instructions.

  2. At the command line, navigate to the examples/scripting directory under your Mule home directory.

  3. Copy the pre-built example zip to the applications folder ($MULE_HOME/apps).

  4. Open one of the following URLs in your browser:

  5. To stop Mule, type 'CTRL-C' in the Mule console window.

Building the Application

You can build the example by either running ant or mvn from the current directory.

How it Works

The example takes input from a HTTP request and transforms the request’s body to a Map using the HttpRequestToMap transformer. It then extracts the currency from that Map to determine the script to call. Before actually calling the script the amout will be extracted from the Map, converted into a number and normalized into an integer amout of cents.


<flow name="http-inbound">
 <http:inbound-endpoint host="" port="47493" path="change-machine"
   exchange-pattern="request-response" transformer-refs="HttpRequestToMap">
   <wildcard-filter pattern="/favicon.ico"/>
  <when expression="payload.currency == 'USD'" evaluator="groovy">
    <transformer ref="ExtractAmount StringToNumber DollarsToCents"/>
 <script:script file="greedy.groovy"> <property key="currency" value="USD"/> </script:script> </script:component> </processor-chain> </when> <when expression="payload.currency == 'GBP'" evaluator="groovy"> <processor-chain> <transformer ref="ExtractAmount StringToNumber DollarsToCents"/> <script:component>  <script:script file=""> <property key="currency" value="GBP"/> </script:script> </script:component> </processor-chain> </when> </choice> </flow>

The actual script is loaded from an external file and is passed a variable called currency, which is bound to a local script variable according to the JSR-223 spec.

Entry Point for Groovy Script

switch (currency) {
 case "USD": return change(USD, payload)
 case "GBP": return change(GBP, payload)
 default: throw new AssertionError("Unsupported currency: $currency")

Entry point for Python script

c = Change(currency, payload)c.recursive_change(c.amount)result = "[" + c.result + "]"


The Groovy algorithm is adapted from the Groovy Cookbook and the Python algorithm is adapted from an article at O’Reilly’s ONLamp Blog

Any language that has a JSR 223-compliant engine can be used with Mule in this manner.