Nav

Migrating the Scripting Component

The <scripting> component in Mule 3 is replaced with a new <scripting> module. This new module is pretty similar and functionality, with just some sintax changes:

Mule 3 Example: Scripting

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 <flow name="greedy">
    <choice>
        <when expression="#[flowVars['currency'] == 'USD']">
            <scripting:component>
                <scripting:script file="greedy.groovy">
                    <property key="currency" value="USD"/>
                </scripting:script> </scripting:component>
            </when> <when expression="#[flowVars['currency'] == 'GBP']"> <scripting:component> <scripting:script file="greedy.py"> <property key="currency" value="GBP"/>
                </scripting:script> </scripting:component>
            </when> </choice>
</flow>

+

Mule 4 Example: Scripting

         
      
1
2
3
4
5
6
7
8
 <flow name="greedy">
    <scripting:execute engine="groovy">
        <scripting:code>${file::greedy.groovy}</scripting:code> <scripting:parameters> #[{currency: vars.currency}] </scripting:parameters>
    </scripting:execute> </flow>

As you can see, the main difference here is that now you can use DataWeave to generate the input parameters.

Accesing variables from the script

The above example uses a variable to show how to pass parameters into the script. Mulesoft recommends that as a best practice to keep the script decoupled from the rest of the flow, so that changes to the mule applications don’t require changes to the script.

However, you can still access the variables from the script using the vars keyword. For example:

Mule 4 Example: Accessing variables directly

         
      
1
2
3
4
5
<flow name="greedy">
    <scripting:execute engine="groovy">
        <scripting:code>return "currency is " + vars.currency</scripting:code> </scripting:execute>
</flow>

To use the Scripting module, simply add it to your application using the Studio palette or add the following dependency in your pom.xml file:


         
      
1
2
3
4
5
6
<dependency>
  <groupId>org.mule.modules</groupId>
  <artifactId>mule-scripting-module</artifactId>
  <version>1.1.0</version> <!-- or newer -->
  <classifier>mule-plugin</classifier>
</dependency>