Contact Us 1-800-596-4880

Scripting Module Reference - Mule 4

Scripting Module v1.1

The Scripting module executes custom logic written in a scripting language.

Configurations


Default Configuration

Use these parameters to configure the default configuration.

Parameters

Name Type Description Default Value Required

Name

String

The name for this configuration. Connectors reference the configuration with this name.

x

Operations

Execute

<scripting:execute>

Run a script with the provided code, according to the engine and passing parameters. The script provides many bindings variables to work with:

Name Type Description

payload

Object

The payload of the message being processed.

dataType

Data type of the message payload.

correlationId

String

The correlationId of the message being processed.

attributes

Map<String, Object>

Attributes of the Mule message being processed.

vars

Map<String, Object>

The variables that are set on the current Mule event being processed.

message

The message being processed.

error

Error associated with the mule message being processed.

authentication

Information about the authentication.

flow

String

The name of the current flow.

log

Logger

Logger object that logs to the app’s log file, MULE_HOME/logs/<app-name>.log

result

Object

Variable representing the result of the script execution. If the code doesn’t produce an implicit result, you have to set this variable explicitly with the execution result. The payload will be set with this value unless you have configured a Target Variable as described in Scripting Module Overview.

registry

org.mule.runtime.api.artifact.Registry

The Registry object can be used to access any other service within the Mule API.

Assume that you have a flow whose XML looks like the following:

....
<set-variable variableName="increment" value="#[22]" />
.....
<scripting:execute doc:name="Script" engine="nashorn">
    <scripting:code>
        log.info("Incrementing payload by " + (vars.increment + initialValue))
        result = vars.increment + Number(payload) + initialValue
    </scripting:code>
    <scripting:parameters><![CDATA[#[{ initialValue: 10 }]]]></scripting:parameters
</scripting:execute>

The previous example shows:

  • How to use a flow variable in the scripting code: vars.increment

  • How to use scripting parameters by referencing them, like: initialValue

  • How to log some information by using the log variable: log.info("…​.")

  • How to use the result variable to set the result of the execution.
    After the scripting:execute operation the payload is set as defined in vars.increment + Number(payload) + initialValue

Parameters

Name Type Description Default Value Required

Code

String

The script source code to execute

x

Engine

String

Name of the scripting engine for running this script

x

Parameters

Map<String, Object>

Variables provided to the script as bindings

Target Variable

String

The name of a variable on which the operation’s output will be placed

Target Value

String

An expression that will be evaluated against the operation’s output and the outcome of that expression will be stored in the target variable

#[payload]

The classes used with the Scripting Module must be exported, otherwise, the execution will fail with a SCRIPTING:COMPILATION error. See How to export resources.

Throws

  • SCRIPTING:COMPILATION

  • SCRIPTING:UNKNOWN_ENGINE

  • SCRIPTING:EXECUTION

View on GitHub