Scripting language execution in MuleSoft using Scripting Module (2.0)
- September 14, 2020
Scripting Module provides facilities for using scripting languages in Mule. The module executes custom logic written in a scripting language (such as Groovy, JavaScript, Python or 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’ve already written.
Similarly, any scripting language that supports JSR-223 can be used inside Mule. Scripts can be used as implementations of components or transformers. Additionally, scripts can be used for expression evaluations, meaning that 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?
JSR-223 is a standard scripting API for Java Virtual Machine (JVM) languages. JVM languages provide varying levels of support for the JSR-223 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 doesn’t provide execution engines; you must provide one (only Oracle Nashorn engine for JavaScript is inbuilt). Refer to the following dependencies:
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. For this example, we’ll have a flow that will load the JMS queue with one hundred JSON messages. It has an HTTP Listener on 8081/publish that will trigger the process. A script to populate a list will then be iterated for each to load the JMS queue with messages contained 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 the Scripting Module. Configure the respective engine by clicking Configure.
NOTE: First, you must create a global element for the Scripting Module. Otherwise, the respective engines won’t be shown in properties.
This is the folder structure of the application:
Step 2. Create a simple flow, as shown below:
The image above also shows the configuration of all the connectors.
The image below shows the configuration of the Execute connector of the Scripting Module.
Engine: Select scripting engine
Code: Write the code here directly or load code from an external file (this example added the 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 3. Deploy the application and navigate to the URL of your application from Postman.
- When you send the request to localhost:8081/publish, one hundred 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 the message will be consumed by one consumer.
To summarize, using the Scripting Module, you can perform all or part of a complex task by writing custom logic in a scripting language.
For more examples, log in to your Anypoint Platform account and open the examples in Anypoint Studio.
Username: guestuser1
Password: Muleuser@1
References:
Happy learning!
— By Suraj Rohankar