Mule : Scripting
This page last changed on Oct 28, 2006 by ross.
Mule Components, transports and configuration can be scripted using any JSR-223 compliant scripting language such as PHP (stateless), Groovy or Rhino (javascript). Script ComponentsScripts can be invoked as Mule managed components by using org.mule.components.script.jsr223.ScriptComponent. To use Groovy as an example - <mule-descriptor name="GroovyUMO" inboundEndpoint="jms://groovy.queue" implementation="org.mule.components.script.jsr223.ScriptComponent"> <properties> <property name="scriptEngineName" value="groovy"/> <text-property name="scriptText"> return "Received message: " + message.getPayload() </text-property> </properties> </mule-descriptor> The following properties can be set on the ScriptComponent
Interacting with Mule from your scriptsWhen a script is invoked, the JSR-223 api has a notion of a namespace, a collection of objects that are referenceable by name from your script. Mule makes the following objects available.
Script Configuration BuilderThe ScriptConfigurationBuilder allows developers to create a mule instance from a JSR-223 compliant script. To load the manager from javascript - ScriptConfigurationBuilder builder = new ScriptConfigurationBuilder("javascript"); UMOManager manager = builder.configure("../conf/mule-config.js"); Or to start the server from the command line - java -Dorg.mule.script.engine=javascript -cp ... org.mule.MuleServer -builder org.mule.config.builders.ScriptConfigurationBuilder -config ../conf/mule-config.js For more information about configuring a Mule instance from code or script see Configuring Mule Programmatically. Script TransformersScript transforms can be used to perform transformations on objects using scripting. The transformers are configuring in the same way as the ScriptComponent. To use Groovy as an example, the configuration for a transformer that converts a comma-separated string of values to a java.util.List would look like - <transformer name="StringToList" returnClass="java.util.List" className="org.mule.transformers.script.ScriptTransformer"> <properties> <property name="scriptEngineName" value="groovy"/> <property name="sourceType" value="java.lang.String"/> <text-property name="scriptText"> return src.toString().tokenize(",") </text-property> </properties> </transformer> The sourceType property tells the transformer only to accept source objects of type string. This property can be set multiple times to register different source types. The scriptText property specifies the groovy script to invoke. The returnClass attribute on the transformer ensures that we will always get a java.util.List back from this transformer. |
Document generated by Confluence on Nov 27, 2006 10:27 |