Access Keys:
Skip to content (Access Key - 0)


Scripting Example

This example demonstrates how to invoke a JSR-223 Script from Mule ESB. It also illustrates how to use custom transformers, the chaining router, 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 Installing Mule instructions.
  2. At the command line, navigate to the examples/scripting directory under your Mule home directory.
  3. Type scripting.bat on Windows or ./scripting on UNIX.
  4. Follow the on-screen prompts.
  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 the command line, performs some simple transformations on it, and accumulates the amount of change requested so far in a custom component:

<service name="Accumulator">
        <stdio:inbound-endpoint system="IN" transformer-refs="StringToNumber DollarsToCents"/>
        <singleton-object class="org.mule.example.scripting.AccumulatorComponent"/>
            <vm:outbound-endpoint path="script"/>

This amount is then passed on to a script-driven component:

Script-driven component
<service name="ScriptService">
        <vm:inbound-endpoint path="script"/>
        <script:script file="${scriptfile}">
            <property key="currency" value="${currency}"/>

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)
result = "[" + c.result + "]"

Note that both the name of the script and the value of the currency parameter are passed in as environment variables to the JVM at startup (refer to the scripting shell script or scripting.bat batch file to see how the variables are passed in from the command line).

The scripting engine to use (Groovy or Python) is determined automatically based on the file extension of the script file (greedy.groovy or

The result from the script component is then sent back to the command line via the chaining router, that is, the synchronous response from the first endpoint (vm) is forwarded to the second endpoint (stdio):

Chaining Router
<service name="Accumulator">
            <vm:outbound-endpoint path="script"/>
            <stdio:outbound-endpoint system="OUT"/>


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. To see what other languages are available, refer to

Adaptavist Theme Builder Powered by Atlassian Confluence