Groovy Component Reference
In spite of the rich selection of elements available in Mule, you may find yourself in need to put together some custom code to carry out part of a complex task, if you’re comfortable coding in Groovy, then the Groovy Component is the ideal versatile tool to fall back on for these cases. Mule also includes also equivalent scripting components for other languages. The Groovy Component is also handy for when you’re modernizing legacy systems, as you can simply throw the old lines of code into a component instead of having to reengineer the code’s behavior through a series of different Mule components (this is not ideal from a debugging and maintenance perspective, though).
Groovy also allows the developer to configure interceptors and alter the values or references of particular properties in a script. Interceptors are configured to provide additional services to a message as it flows through a component. For example, a developer can configure an interceptor to execute scheduling or logging of a particular event while a message is being processed. The Groovy component also includes a custom interceptor which allows you to configure settings for Spring elements. Use the Advanced tab to access Script Properties, then define the value and reference for a particular property in the script.
When using Groovy, typing-in or appending a script to the component is the only configuration required.
|Placing a Groovy Component is actually the same as placing a Script component and then selecting the Groovy engine.|
To configure the Groovy component, from the Message Flow canvas double-click the Groovy icon to open the Pattern Properties pane. Use the General tab to specify the file location of the script or simply type in the script on the script text window. The scripting engine is Groovy.
Defaults to the generic component name. Change the Display Name, which must be alpha-numeric, to reflect the component’s specific role, i.e.,
Script Text: Type the script that the component will load directly into this space.
Script File: Enter the location of the script to be loaded by the component. The file can reside on the classpath or the local file system**.
Bean Reference**: Lets you add a Java bean, which encapsulates multiple objects into a single bean. The Script component can then store and re-use the bean when applicable.
Use the Advanced tab to optionally configure interceptors and, depending on the interceptor, enter Spring values.You also have the option to specify script properties, which are key/value pairs used to alter or change properties in the script. See the Script Component Reference page for information on using the advanced tab.
Interceptors alter the values or references of particular properties in a script. They are configured to provide additional services to a message as it flows through a component. For example, you can configure an interceptor to execute scheduling or logging of a particular event while a message is being processed. The Script component also includes a custom interceptor which allows you to configure settings for Spring elements.
Interceptors enable the developer to provide additional services to the component such as the ability to log transactions and the ability to log the time for each transaction. Use the Add Custom Interceptor to create a custom interceptor that can reference Spring objects. The Interceptor Stack enables you to bundle multiple interceptors. Use the Interceptor Stack to apply multiple interceptors on a Groovy component. The interceptors are applied in the order defined in the stack.
The example below uses two properties and executes a Groovy code that’s written directly on the component. The executed code inspects the payload and replaces every instance of the string "1" with the string "x", both these values are defined in the properties.
1 2 3 4 5 6 7 8 9 10 11 <flow name="groovyTransformerWithParameters"> <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> </flow>