Contact Us 1-800-596-4880

Scripting Module - Examples - Mule 4

The following Scripting module examples show you how to use binding variables and how to access the registry.

Use Binding Variables Example

The following Scripting Module XML example shows how to:

  • Use a flow variable in the scripting code, such as vars.increment

  • Use scripting parameters by referencing them, such as initialValue

  • Log some information by using the log variable, such as log.info("…​.")

  • Use the result variable to set the result of the execution

  • Define the payload in vars.increment + Number(payload) + initialValue

<set-variable variableName="increment" value="#[22]" />
<scripting:execute engine="ECMAScript">
    <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>

Access the Registry Example

The following Scripting Module XML example shows how to:

  • Access the registry

  • Stop or start a flow

<scripting:execute  engine="Groovy">
    <scripting:code ><![CDATA[flow = registry.lookupByName("test-flow").get();
        if (flow.isStarted())
	        flow.stop()
        else
	        flow.start()]]>
    </scripting:code>
</scripting:execute>
View on GitHub