Contact Us 1-800-596-4880

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.

Configuration

General Tab

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.

groovy 1
Panel Description

Display

Defaults to the generic component name. Change the Display Name, which must be alpha-numeric, to reflect the component’s specific role, i.e., Groovy Script Welcome Page.

Script

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.

Advanced Tab

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.

groovy_advanced

Interceptors

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.

For example, you can add an interceptor that logs transactions and 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.

Script Properties

Configure these parameters to define the attribute keys and their associated values. This enables the script component to quickly look up a value associated with a key.

Examples

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.

<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>

The example below uses the “Greedy Coin Changer” algorithm to transform one currency into another. It uses some transformers to transform the input data and then applies a Groovy algorithm to convert the currency.

<http:listener-config name="http_conf" host="localhost" port="8081" doc:name="HTTP Response Configuration"/>
<flow name="greedy">
    <http:listener path="/" doc:name="HTTP Connector" config-ref-inbound="http_conf"/>
    <http:body-to-parameter-map-transformer />

    <set-payload value="#[payload['amount']]" />
    <transformer ref="StringToNumber" />
    <transformer ref="DollarsToCents"/>

    <scripting:component doc:name="USD Currency Script">
        <scripting:script file="greedy.groovy">
            <property key="currency" value="USD"/>
        </scripting:script>
    </scripting:component>
</flow>

The example below uses the “Greedy Coin Changer” algorithm to transform one currency into another. It uses some transformers to transform the input data and then applies a Groovy algorithm to convert the currency.

// Adapted from the Groovy Cookbook
// https://web.archive.org/web/20150213041152/http://groovy.codehaus.org/Greedy+Coin+Changer+in+Groovy

enum USD {
    quarters(25), dimes(10), nickels(5), pennies(1)
    USD(v) { value = v }
    final value
}

enum GBP {
    two_pounds (200), pounds (100), fifty_pence(50), twenty_pence(20), ten_pence(10), five_pence(5), two_pence(2), pennies(1)
    GBP(v) { value = v }
    final value
}

def change(currency, amount) {
  currency.values().inject([]){ list, coin ->
     int count = amount / coin.value
     amount = amount % coin.value
     list += "$count $coin"
  }
}

switch (currency) {
    case "USD": return change(USD, payload).toString()
    case "GBP": return change(GBP, payload).toString()
    default: throw new AssertionError("Unsupported currency: $currency")
}

See Also