Give the pack some love
- Including all 4 scripting languages by default in the community distribution
- We upgraded Groovy and Jython to versions 2.3.7-indy and 2.7.3B3, which are the latest available at the moment of the 3.6 release
Is Mule limited to only these four scripting languages? No! Any language supporting JSR-223 will be discovered an available to use.
Performance boost through invoke dynamic
In JDK7, a new byte code instruction called “invoke dynamic” was added to allow scripting languages to have a big performance improvement. Starting with version 3.6, JDK7 is now the minimum required JDK to run Mule which means that we can now take advantage of that Java feature. The Groovy and Jython versions that are now added to the distributions take advantage of this which means that you will see a speed boost in your groovy and python scripts!
Seamless integration with Mule
No matter which scripting language you’re using, Mule will automatically bind some variables into your script so that you can interact with mule. Those properties are:
|log||a logger that can be used to write to Mule’s log file.|
|muleContext||a reference to the MuleContext object.|
|eventContext||A reference to the EventContext. This allows you to dispatch events programmatically from your script|
|message||the current message.|
|originalPayload||the payload of the current message before any transforms.|
|payload||the transformed payload of the current message if a transformer is configured on the service. Otherwise this is the same value as originalPayload.|
|src||same as payload, kept for backward compatibility.|
|service||a reference to the current service object.|
|id||the current event id.|
|result||a placeholder object where the result of the script can be written to. Usually it’s better to just return a value from the script unless the script method doesn’t have a return value.|
Let’s see an example of how to implement a flow which uses the “Greedy Coin Changer” algorithm. This is actually an example that ships with the Mule distribution. Below I’ll show a simplification of it, you can see the full example following this link.
This is the flow configuration:
In the above flow we see a flow which starts with a http inbound endpoint. It uses some transformers to transform the input data and then applies the algorithm, using Groovy if the currency is US Dollars or Python in the case of British pounds. This is the groovy implementation of the algorithm:
The use of scripting languages is not limited to components, you can also use if for transformations:
As you can see, the groovy is bounded to the message payload through the ‘payload’ variable (although message.getPayload() would have done it as well). But is for simplicity reasons you want to pass your own parameters, you can do that as well!