Author: Suraj Rohankar
The scripting module provides facilities for using scripting languages in Mule. The Scripting Module executes custom logic written in a scripting language (such as Groovy, JavaScript, Python and Ruby). In some cases, you might need to create custom code to perform all or part of a complex task, or to reuse modules that you have already written.
Similarly any scripting languages that supports JSR-223 can be used inside Mule. Scripts can be used as implementations of components or transformers. Also, scripts can be used for expression evaluations, meaning message routing can be controlled using script evaluations on the current message. You can even configure Mule instances from scripts. Since version 2.0 you must provide a compliant JSR-223 scripting language engine.
What is JSR-223?
JSR223 is a standard scripting API for Java Virtual Machine (JVM) languages. The JVM languages provide varying levels of support for the JSR223 API and interoperability with the Java runtime.
How to use the Scripting module (2.0)?
In Anypoint Studio create a new Mule project, Search for the “Scripting” module in the Mule palette or simply add the following dependency in your pom.xml of the created project.
<!-- Scripting Module Mule-4 -->
<dependency>
<groupId>org.mule.modules</groupId>
<artifactId>mule-scripting-module</artifactId>
<version>2.0.0</version>
<classifier>mule-plugin</classifier>
</dependency>
This Scripting Module does not provide execution engines. You have to provide an execution engine yourself (only Oracle Nashorn engine for Javascript is inbuilt). Refer to the following dependency.
Groovy:
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.16</version>
<classifier>indy</classifier>
</dependency>
JRuby (Ruby):
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby-core</artifactId>
<version>9.2.11.1</version>
</dependency>
Jython (Python):
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.2</version>
</dependency>
Let’s get started with a simple use case.
Use Case: For this example we will have a flow that will load the JMS queue with 100 json messages. It has an HTTP Listener on 8081/publish that will trigger the process.
A script to populate a list that will then be iterated with a For each to load the JMS queue with messages that contain in the CSV.
Prerequisites:
- Anypoint Studio 7.x.x
- ActiveMQ
- Postman
Step 1: Create a Mule project in Anypoint Studio and create a Global Element for Scripting module. We can configure the respective engine by clicking “Configure”.
Note: First you have to create a global element for the Scripting module, otherwise respective engines won’t be shown in properties.

This is the folder structure of the application.

Step 2: Create simple flow as shown below.

Step 3: Configuration of all the connectors as displayed in the above image. The following image represents the configuration of the Execute connector of the Scripting module.

- Engine: Select Scripting Engine.
- Code: Write the code here directly or we can also load code from an external file (here we have added iterationScript.groovy file to src/main/resources).
- Parameters: Define input values for the script to use through DataWeave.
iterationScript.groovy
List<String> list = new ArrayList<String>();
for (int i = 0; i < 100; i++){
list.add(i);
}
return list;
Step 4: Deploy the application and navigate to the URL of your application from Postman.
- When you send the request to localhost:8081/publish, 100 copies of the same message will be published to the given queue.

- When you send the request to localhost:8081/consume, only a single copy of message will be consumed by one consumer.

To summarise, using the Scripting module we can perform all or part of a complex task by writing the custom logic in scripting languages.
For more examples, you can login to anypoint platform account and open the examples in Anypoint Studio.
Username: guestuser1
Password: Muleuser@1
References:
Happy Learning!