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.
- Make sure you have met the prerequisites and installed Mule according to the Installing Mule instructions.
- At the command line, navigate to the examples/scripting directory under your Mule home directory.
- Type scripting.bat on Windows or ./scripting on UNIX.
- Follow the on-screen prompts.
- To stop Mule, type 'CTRL-C' in the Mule console window.
You can build the example by either running ant or mvn from the current directory.
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:
This amount is then passed on to a script-driven component:
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.
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):
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.