Using Groovy Component
- By Rahul Kumar
Groovy
The Groovy Component is handy 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
Groovy
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 reuse
the bean when applicable.
Examples
<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>
This example demonstrates simple string replacement.
Examples
<<scripting:component doc:name="Script">
<scripting:script engine="Groovy"><![CDATA[
throw new IllegalArgumentException('bad arg')
]]></scripting:script>
</scripting:component>
This example demonstrates throwing Exception with Groovy
Examples
<<scripting:component doc:name="Script">
<scripting:script engine="Groovy"><![CDATA[
Thread.sleep(60000)
]]></scripting:script>
</scripting:component>
This example demonstrates how to use Groovy to make the Thread sleep for 1 munute.
Thank You !!

Using groovy component