You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

ServiceMix supports Groovy scripts which can be used as endpoints, transformers or services. This allows you to combine the power of the Groovy scripting language with the ServiceMix JBI container and any off the shelf JBI components to create a very flexible and agile integration solution.

The GroovyComponent is an extension of the JSR 223 support and supports the same variable bindings.

Working with properties

In ServiceMix you can access the JBI message properties as a Map and work natively with it in Groovy using various mechanisms. e.g.

// lets output some message properties
outMessage.properties.foo = "hello"
outMessage.properties.someList = [1, 2, 3]

or use an intermediate object if you've lots of properties to set

def props = outMessage.properties
props.foo = "hello"
props.someList = [1, 2, 3]

or just use the native property syntax

outMessage.properties = [foo:"hello", someList:[1, 2, 3]]

Generating output

Groovy provides various mechanism for generating the output (whether it is the result of a service or a transformation). Which mechnism you use depends on your use case and personal preference.

String templates

You can use Groovy string templates to output XML, which is a nice, simple way to generate blocks of XML with dynamic content

outMessage.bodyText = """
<hello>
  <world person="$inMessage.properties.name"/>
</hello>
"""

Notice the user above of the input messages's 'name' property, which is equivalent to the expression

inMessage.getProperty("name")

POJO return values

You can return a POJO as the body of a message - which other components can either transform or the deafult Marshaler will figure out the right thing to do.

// lets output the body as a POJO
outMessage.body = [3, 2, 1]

Using Groovy Markup

Groovy supports a simple and concise markup mechanism which can be used to programatically generate some XML markup (either DOM, SAX or any other XML model) while retaining the full power of Groovy within the control flow of the markup.

// lets output some XML using GroovyMarkup
outMessage.body = builder.hello(version:1.2) {
  world(location:'London')
}

Examples

You can see all of the above mechanims in use in this test case and XML config file.

  • No labels