<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.2/mule.xsd
http://www.mulesoft.org/schema/mule/scripting
http://www.mulesoft.org/schema/mule/scripting/3.2/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.2/mule-vm.xsd">
<vm:connector name="vmConnector"/>
<script:script name="myScript" engine="groovy">
return "$payload Received"
</script:script>
<model>
<service name="inlineScript">
<inbound>
<inbound-endpoint address="vm://in1"/>
</inbound>
<script:component>
<script:script engine="groovy">
return "$payload Received"
</script:script>
</script:component>
<outbound>
<pass-through-router>
<outbound-endpoint address="vm://out1"/>
</pass-through-router>
</outbound>
</service>
...
Scripting Module Reference
[ Component ] [ Script ] [ Script ] [ Script ] [ Script ] [ Script Context Bindings ] [ Transformer ] [ Groovy refreshable ] [ Lang ]
The scripting module provides facilities for using scripting languages in Mule. Any scripting languages that supports JSR-223 can be used inside Mule. Scripts can be used as implementations of service 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.
Component
Defines a script component backed by a JSR-223 compliant script engine such as Groovy, JavaScript, or Ruby. Scripting allows you to either directly embed your script inside the XML config or reference a script using Spring’s dynamic language support: http://static.springframework.org/spring/docs/2.5.x/reference/dynamic-language.html.
Attributes of <component…>
Name | Type | Required | Default | Description |
---|---|---|---|---|
script-ref |
string |
no |
A reference to a script object bean, that is, a |
Child Elements of <component…>
Name | Cardinality | Description |
---|---|---|
script |
0..1 |
A script to be executed by a JSR-223 compliant script engine such as Groovy, JavaScript(Rhino), Python, Ruby, or Beanshell. |
java-interface-binding |
0..* |
A binding associates a Mule endpoint with an injected Java interface (this is like using Spring to inject a bean, but instead of calling a method on the bean a message is sent to an endpoint). Script bindings will only work with Java-based scripting languages. Right now there is no validation on when languages support Java bindinngs because there are so many scripting languages. |
The following example demonstrates how to configure a Groovy script component with an in-line script:
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.
<service 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>
</service>
<service name="Callout1">
<inbound>
<inbound-endpoint ref="callout_1"/>
</inbound>
<test:component appendString=" Received by #[mule:context.serviceName]"/>
</service>
<service name="Callout2">
<inbound>
<inbound-endpoint ref="callout_2"/>
</inbound>
<test:component appendString=" Received by #[mule:context.serviceName]"/>
</service>
Script
A script to be executed by a JSR-223 compliant script engine such as Groovy, JavaScript(Rhino), Python, Ruby, or Beanshell.
Attributes of <script…>
Name | Type | Required | Default | Description |
---|---|---|---|---|
name |
string |
no |
The name used to identify this script object. This is used when you want to reference this script object from a component or transformer. |
|
engine |
string |
no |
The name of the script engine being used. All scripting languages that support JSR-223 have a script engine name such as groovy, ruby, python, etc. If this value is not set, but a script file is configured, Mule will attempt to load the correct script engine according to the script file’s extension. |
|
file |
string |
no |
The script file to load for this object. The file can be on the classpath or local file system. |
Script
Represents a script that can be used as a component for a service or a transformer. The script text can be pulled in from a script file or can be embedded inside this element. A script can be executed by any JSR-223 compliant script engine such as Groovy, JavaScript(Rhino), Python, Ruby, or Beanshell.
Attributes of <script…>
Name | Type | Required | Default | Description |
---|---|---|---|---|
name |
string |
no |
The name used to identify this script object. This is used when you want to reference this script object from a component or transformer. |
|
engine |
string |
no |
The name of the script engine being used. All scripting languages that support JSR-223 have a script engine name such as groovy, ruby, python, etc. If this value is not set, but a script file is configured, Mule will attempt to load the correct script engine according to the script file’s extension. |
|
file |
string |
no |
The script file to load for this object. The file can be on the classpath or local file system. |
Script
A script to be executed by a JSR-223 compliant script engine such as Groovy, JavaScript(Rhino), Python, Ruby, or Beanshell.
Attributes of <script…>
Name | Type | Required | Default | Description |
---|---|---|---|---|
name |
string |
no |
The name used to identify this script object. This is used when you want to reference this script object from a component or transformer. |
|
engine |
string |
no |
The name of the script engine being used. All scripting languages that support JSR-223 have a script engine name such as groovy, ruby, python, etc. If this value is not set, but a script file is configured, Mule will attempt to load the correct script engine according to the script file’s extension. |
|
file |
string |
no |
The script file to load for this object. The file can be on the classpath or local file system. |
Script
A script to be executed by a JSR-223 compliant script engine such as Groovy, JavaScript(Rhino), Python, Ruby, or Beanshell.
Attributes of <script…>
Name | Type | Required | Default | Description |
---|---|---|---|---|
name |
string |
no |
The name used to identify this script object. This is used when you want to reference this script object from a component or transformer. |
|
engine |
string |
no |
The name of the script engine being used. All scripting languages that support JSR-223 have a script engine name such as groovy, ruby, python, etc. If this value is not set, but a script file is configured, Mule will attempt to load the correct script engine according to the script file’s extension. |
|
file |
string |
no |
The script file to load for this object. The file can be on the classpath or local file system. |
Script Context Bindings
When run inside Mule, scripts have a number of objects available to them in the script context:
Name | Description | ||
---|---|---|---|
|
A logger that can be used to write to Mule’s log file. |
||
|
A reference to the MuleContext object. |
||
|
A convenience shortcut to Mule registry (otherwise available via |
||
|
A reference to the event context. This allows you to dispatch events progammatically from your script. |
||
|
The current message. |
||
|
The payload of the current message before any transforms. |
||
|
The transformed payload of the current message if a transformer is configured on the service. Otherwise this is the same value as |
||
|
Same as |
||
|
A reference to the current service object. |
||
|
The current message ID. |
||
|
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.
|
||
message properties |
Any message properties can be used as variables for the script. |
Transformer
Runs a script to perform transformation on the current message.
Child Elements of <transformer…>
Name | Cardinality | Description |
---|---|---|
script |
0..1 |
A script to be executed by a JSR-223 compliant script engine such as Groovy, JavaScript(Rhino), Python, Ruby, or Beanshell. |
To use Groovy as an example, the following transformer configuration converts a comma-separated string of values to a java.util.List
.
<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>
Groovy refreshable
A wrapper for a component object that allows the underlying object to be reloaded at runtime. This makes it possible to hot-deploy new component logic without restarting.
Attributes of <groovy-refreshable…>
Name | Type | Required | Default | Description |
---|---|---|---|---|
name |
string |
yes |
The name for this refreshable groovy bean wrapper. |
|
refreshableBean-ref |
string |
no |
The reference to a |
|
methodName |
string |
no |
The entrypoint method to invoke when a message is received for the object. |
Lang
This element allows the http://www.springframework.org/schema/lang namespace to be embedded. Within this element developers can include the Spring lang
namespace.