{{announcement.body}}
{{announcement.title}}

Scripting Language Execution in Mulesoft Using Scripting Module(2.0)

DZone 's Guide to

Scripting Language Execution in Mulesoft Using Scripting Module(2.0)

Scripting Module is useful to execute scripting languages to perform all or part of a complex task or to reuse modules that you have already written.

· Integration Zone ·
Free Resource

About Scripting Module(2.0)

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 support 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.

XML
 


x
 
1
<!-- Scripting Module Mule-4 -->
2
<dependency>
3
    <groupId>org.mule.modules</groupId>
4
    <artifactId>mule-scripting-module</artifactId>
5
    <version>2.0.0</version>
6
    <classifier>mule-plugin</classifier>
7
</dependency>


This Scripting module does not provide execution engines directly, you have to provide an execution engine yourself(Only Oracle Nashorn engine for Javascript is inbuilt.). Refer to the following dependency.

Groovy:

XML
 


xxxxxxxxxx
1
1
<dependency>
2
    <groupId>org.codehaus.groovy</groupId>
3
    <artifactId>groovy-all</artifactId>
4
    <version>2.4.16</version>
5
    <classifier>indy</classifier>
6
</dependency>


JRuby (Ruby):

XML
 




x


1
<dependency>
2
    <groupId>org.jruby</groupId>
3
    <artifactId>jruby-core</artifactId>
4
    <version>9.2.11.1</version>
5
</dependency>



Jython (Python):

XML
 




xxxxxxxxxx
1


1
<dependency>
2
    <groupId>org.python</groupId>
3
    <artifactId>jython-standalone</artifactId>
4
    <version>2.7.2</version>
5
</dependency>



Let's get started with a simple use case.

Use Case: 

How to stop and start the flow in Mule 4 programmatically?

Prerequisites:

  • Anypoint Studio 7.x.x
  • 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”.

scripting config in AnyPoint Studio

(Note: First you have to create a global element for the Scripting module. Otherwise, respective engines won't be shown in properties.)

Step 2 

Create mule flows as shown below. The initial state of the “stopped-flow” is stopped.

mule-scripting

Step 3

The following image represents the configuration of the "Execute" connector of the Scripting module.

Execute connector configuration

  • Engine: Select the Scripting Engine.
  • Code: Write the code here directly or we can also load code from an external file. (Here we have added flowStartStop.groovy file to src/main/resources).
  • Parameters: Define input values for the script to use through DataWeave.

flowStartStop.groovy

Groovy
 




xxxxxxxxxx
1


1
flow = registry.lookupByName("stopped-flow").get();
2
        if (flow.isStarted())
3
            flow.stop()
4
        else
5
            flow.start()



Step 4

Deploy the application and navigate to the URL of your application from Postman, you will get the following logs showing the stopped-flow has started.

stopped-flow has started

To summarize, 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 from an exchange in Anypoint Studio.

Username: guestuser1

Password: Muleuser@1

References

  1) https://docs.mulesoft.com/scripting-module/2.0/

  2) https://help.mulesoft.com/s/article/How-To-Stop-Or-Start-Flows-In-Mule-4-x-Programmatically

Topics:
integration, mulesoft, scripting module, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}