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

Contents

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:

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

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

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

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 + "]"

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 greedy.py).

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">
    ...cut...
    <outbound>
        <chaining-router>
            <vm:outbound-endpoint path="script"/>
            <stdio:outbound-endpoint system="OUT"/>
        </chaining-router>
    </outbound>
</service>

References

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 https://scripting.dev.java.net.

Adaptavist Theme Builder Powered by Atlassian Confluence