DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint
  • A Deep Dive into Tracing Agentic Workflows (Part 1)

Trending

  • Genkit Middleware: Intercept, Extend, and Harden your Gen AI Pipelines
  • You Are Using Claude Wrong (And So Is Everyone You Know)
  • Introduction to Retrieval Augmented Generation (RAG)
  • Reactive Kafka With Spring Boot
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Scripting Language Execution in Mulesoft Using Scripting Module(2.0)

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.

By 
Suraj Rohankar user avatar
Suraj Rohankar
·
Updated Aug. 27, 20 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
12.9K Views

Join the DZone community and get the full member experience.

Join For Free

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

Scripting language Execution (computing) Java virtual machine Engine MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint
  • A Deep Dive into Tracing Agentic Workflows (Part 1)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook