Contact Us 1-800-596-4880

Scripting Module - Mule 4

Scripting Module v1.1

The Scripting module executes custom logic written in a scripting language. 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. Supported scripting languages:

  • Groovy 2.4.16

  • Jython 2.7.2 (Java implementation of Python)

  • JRuby 9.2.11.1 (Java implementation of Ruby)

  • Nashorn (JavaScript engine)

Installation

The Scripting module is included in Anypoint Studio. If you do not see this module in the Mule Palette, add it using Add Module. Alternatively, you can search in Exchange for the latest version of this module, and add this module to your project’s dependencies.

Basic Usage

To configure a Scripting operation in Studio:

  1. Click the Scripting module from the Mule Palette.

  2. Drag the Execute operation of the Scripting module into your flow, for example:

    Scripting Execute component
  3. Double-click the Execute element to open the Execute configuration tab.

  4. From the General tab, provide your code in the Code text window, as shown in the next example.

  5. Choose the Engine for the execute component:

    • Groovy

    • Nashorn (JavaScript)

    • Jython (Python)

    • JRuby (Ruby)

Script with code

In the Configuration XML editor, the XML looks similar to the following:

Example: Basic Usage
<scripting:execute engine="jython" doc:name="Script">
    <scripting:code>
      def factorial(n):
          if n == 0: return 1
	  return n * factorial(n-1)

      result = factorial(10)
    </scripting:code>
</scripting:execute>

You can also load code from an external file by using a File configuration property, for example:

Script with a file configuration property

In the Configuration XML editor, the XML looks similar to the following:

Example: Using a file configuration property to set a script’s code
<scripting:execute engine="jython" doc:name="Script">
    <scripting:code >${file::script.py}</scripting:code>
</scripting:execute>

Setting Parameters

You can define input parameter values for the script to use through DataWeave, for example:

Script using parameters

Parameters should be a map where keys are strings and the values are any object. The DataWeave expression must produce this type of output to work correctly.

You can use the parameters as binding variables by referencing them by their name, for example: factorial(initialValue + int(payload))

In the Configuration XML editor, the XML looks something like this:

Example: Setting Parameters
<scripting:execute engine="jython" doc:name="Script">
	<scripting:code >def factorial(n):
	if n == 0: return 1
	return n * factorial(n-1)
result = factorial(initialValue + int(payload))</scripting:code>
	<scripting:parameters ><![CDATA[#[{
        initialValue: 10
    }]]]></scripting:parameters>
</scripting:execute>

Advanced Configuration

Use the Advanced tab to modify the target value and to set a variable as the target of the scripting execution.

Advanced settings

In the Configuration XML editor, the XML looks similar to the following:

Example: Advanced Settings
<scripting:execute engine="jython" doc:name="Script" target="variableName">
    <scripting:code >${file::script.py}</scripting:code>
</scripting:execute
View on GitHub