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.
Join the DZone community and get the full member experience.Join For Free
About Scripting Module(2.0)
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.
<!-- Scripting Module Mule-4 -->
Let's get started with a simple use case.
How to stop and start the flow in Mule 4 programmatically?
- Anypoint Studio 7.x.x
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.)
Create mule flows as shown below. The initial state of the “stopped-flow” is stopped.
The following image represents the configuration of the "Execute" connector of the Scripting module.
- 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
- Parameters: Define input values for the script to use through DataWeave.
flow = registry.lookupByName("stopped-flow").get();
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.
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.
Opinions expressed by DZone contributors are their own.
How To Approach Java, Databases, and SQL [Video]
Competing Consumers With Spring Boot and Hazelcast
Health Check Response Format for HTTP APIs
Writing a Vector Database in a Week in Rust