Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Scripting Module Reference

The scripting module provides facilities for using scripting languages in Mule. Any scripting languages that supports http://www.jcp.org/en/jsr/detail?id=223[JSR-223] can be used inside Mule. Scripts can be used as implementations of components or transformers. Also, scripts can be used for expression evaluations, meaning message routing can be controlled using script evaluations on the current message. You can even configure Mule instances from scripts.

xslt: Error accessing style source.

The following example demonstrates how to configure a Groovy script component with an in-line script:


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:script="http://www.mulesoft.org/schema/mule/scripting"
      xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
      xsi:schemaLocation="
       http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.3/mule.xsd
       http://www.mulesoft.org/schema/mule/scripting
       http://www.mulesoft.org/schema/mule/scripting/3.3/mule-scripting.xsd
       http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.3/mule-vm.xsd">
 
    <vm:connector name="vmConnector"/>
 
    <script:script name="myScript" engine="groovy">
        return "$payload Received"
    </script:script>           <flow name="inlineScript">             <vm:inbound-endpoint path="vm:in1"/>
            <script:component>
                <script:script engine="groovy">
                    return "$payload Received"
                </script:script>             </script:component>
            <vm:outbound-endpoint path="out1"/>
        </flow> ...

The following example demonstrates how to orchestrate message flows using bindings. The example calls out to two different services and passes the results on to the outbound router.


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<flow name="scriptWithBindings">
            <inbound>
                <inbound-endpoint ref="client_request"/>
            </inbound>
            <script:component>
                <script:script engine="groovy">
                    msg = CalloutService.doSomething(payload)
                    return CalloutService.doSomethingElse(msg)
                </script:script>                 <script:java-interface-binding interface="org.mule.components.script.CalloutService" method="doSomething">                     <outbound-endpoint ref="callout_1" synchronous="true"/>
                </script:java-interface-binding>                 <script:java-interface-binding interface="org.mule.components.script.CalloutService" method="doSomethingElse">                     <outbound-endpoint ref="callout_2" synchronous="true"/>
                </script:java-interface-binding>             </script:component>
            <outbound>
                <pass-through-router>
                    <outbound-endpoint ref="client_response"/>
                </pass-through-router>
            </outbound>
        </flow>           <flow name="Callout1">             <inbound>                 <inbound-endpoint ref="callout_1"/>
            </inbound>             <test:component appendString=" Received by #[mule:context.serviceName]"/>
        </flow>           <flow name="Callout2">             <inbound>                 <inbound-endpoint ref="callout_2"/>
            </inbound>             <test:component appendString=" Received by #[mule:context.serviceName]"/>
        </flow>

xslt: Error accessing style source.

Script Context Bindings

When run inside Mule, scripts have a number of objects available to them in the script context:

Name Description

log

A logger that can be used to write to Mule’s log file.

muleContext

A reference to the MuleContext object.

registry

A convenience shortcut to Mule registry (otherwise available via muleContext.registry).

eventContext

A reference to the event context. This allows you to dispatch events progammatically from your script.

message

The current message.

originalPayload

The payload of the current message before any transforms.

payload

The transformed payload of the current message if a transformer is configured on the flow. Otherwise this is the same value as originalPayload.

src

Same as payload, kept for backward compatibility.

flowConstruct

A reference to the current flow object.

id

The current message ID.

result

A placeholder object where the result of the script can be written. Usually it’s better to just return a value from the script unless the script method doesn’t have a return value.

If your script needs to return null, you must set result=null instead of simply returning null

message properties

message properties

xslt: Error accessing style source.

To use Groovy as an example, the following transformer configuration converts a comma-separated string of values to a java.util.List.


         
      
1
2
3
4
5
6
7
8
9
<script:transformer name="stringReplaceWithParams">
        <script:script engine="groovy">
            <property key="oldStr" value="l"/>
            <property key="newStr" value="x"/>
            <script:text>
                return payload.toString().replaceAll("$oldStr", "$newStr")
            </script:text>         </script:script>
    </script:transformer>

xslt: Error accessing style source.

xslt: Error accessing style source.

In this topic: