Mulesoft Scripting Module and Flow Control Management

Gursimran Singh Ranhotra
Another Integration Blog
5 min readMay 17, 2023

In this article, we will go over the scripting module, as well as how to implement & integrate different scripting languages in Mulesoft like- Groovy, JavaScript & Python etc. We will also discuss on Flow control management like starting or stopping flow using Groovy Script as well as their use cases.

The Scripting Module executes custom logic written in a scripting language (such as Groovy, JavaScript, Python, and Ruby).

Usecases-

  1. We may need to write custom code to perform all or part of a complex task, or to reuse any previously written modules.
    In spite of the rich selection of elements available in Mulesoft,
    we may find use cases in need of creating custom code to carry out all or part of a complex task. The script component is the ideal, versatile tool to use in these cases.

2. It’s also useful when you’re modernizing legacy systems, as you can simply throw the old lines of code into a component instead of having to reengineer the code’s behavior again through a series of different Mule components.

3. To access variables and the registry, log information via the Mule logger, read payloads, flow control, and so on.

4. To integrate any scripting language within your Mulesoft code.

5.Starting Flow only once on project /Enabling and disabling flows on demand or from configuration file as discussed later in this article.

•Mule can be used with any scripting language that supports JSR-223. (JSR223 is a standard scripting API for Java Virtual Machine (JVM) languages).
Scripts can be used as components, transformer implementations & expression evaluations which means that message routing can be controlled by evaluating scripts on the current message.
Since version 2.0, we must provide a JSR-223 scripting language engine that is compliant.

Note- You can use one of the following languages for script component: Groovy, JavaScript, Python, Ruby

Implementing Groovy Scripts in Mulesoft

Steps to configure-

  1. In the pom.xml, add the following dependency or import scripting module from exchange :
<dependency>
<groupId>org.mule.modules</groupId>
<artifactId>mule-scripting-module</artifactId>
<version>2.0.3</version>
<classifier>mule-plugin</classifier>
</dependency>

(On adding the following dependency, you can see & move scripting module to you flow)

2. Configure the required JSR-223 engine & select Groovy engine from the list.
On configuring the maven dependency, it automatically includes required shared libraries and groovy dependency to pom.xml.
(You can either configure directly or include following dependencies in your pom.xml)

Shared Library-
<sharedLibrary>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</sharedLibrary>
Dependencies-
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.21</version>
<classifier>indy</classifier>
</dependency>

Working-

Example: I added the following Groovy code to print numbers from 0 to 99 through a loop-

As you can see, hitting it generates numbers ranging from 0 to 99.

2. Running Python Scripts in Mulesoft-

  1. In the pom.xml, add the scripting module dependency or import the scripting module from the exchange as mentioned earlier in the case of Groovy.
    (No need to add again, if you’ve already configured once in a project).

2. Configure the required JSR-223 engine & select the Jython engine from the list.
On configuring the maven dependency, it automatically includes required shared libraries and python dependency to pom.xml-

Shared Library-
<sharedLibrary>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
</sharedLibrary>
Dependencies-
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.2</version>
</dependency>

Working-
I’ve added Python code to generate factorial of input.

On hitting, you can see that it prints the factorial of the number, i.e., 362880.(Factorial of 10).

The same way, you can run any other scripting languages in Anypoint studio.

3. Starting or stopping flow using Groovy script

We can start and stop flows in Mule 4.x; you just need to acquire the Mule Registry and then get the flow. This article provides a way to stop and start Mule flows using a Groovy script.

Usecases-

1. Start Flow only once on the project start-up.
2. Enable and disable flows on demand or from the configuration file.
3. You don’t want to generate JAR again and redeploy it, in case of no code change.

Note: This can’t be applied in CloudHub applications where the flow triggers with schedulers, as it needs to be managed by their own infrastructure. This article is still applicable for flows that start with an HTTP listener for the CloudHub apps.

In the execute connector, add the following code:

flow = registry.lookupByName(‘//flowName’).get();
if (flow.isStarted())
flow.stop()
else flow.start()

Note: In brackets, add your flow name. Example: In my case, I will test starting or stopping my ‘example-python’ Flow.
So, I have added-

 "flow" = registry.lookupByName(‘example-python’).get();

Testing-
We will test this with ‘example-python’ flow, which we developed in last step.

Stopping the flow-
If we hit our groovy script Flow-

We can see now in the logs that it stops ‘example-python’ flow.

And if we hit the ‘example-python’ Flow now, it throws a 404 error.

Starting the flow -

Hitting it again-

We can see in the logs that it starts ‘example-python’ Flow again.

And now, if we hit flow again, we’ll get 200 status with flow again. And we can see factorial as per the code.

Thanks for reading!
Please feel free to comment and follow me for more helpful articles and insights in the future.

--

--