|
Groovy has been edited by Lars Heinemann (Jun 12, 2008). Content:
ServiceMix supports Groovy The GroovyComponent ExampleBefore we go into detail of how you can work with JBI and Groovy in ServiceMix, lets show a simple hello world kinda example. <sm:activationSpec componentName="myServiceUsingXMLText" service="foo:myServiceUsingXMLText"> <sm:component> <bean class="org.apache.servicemix.components.groovy.GroovyComponent"> <property name="scriptText"> <value> <![CDATA[ // lets output some message properties outMessage.properties = [foo:"hello", someList:[1, 2, 3]] // lets output some non-xml body outMessage.bodyText = """ <hello> <world person="$inMessage.properties.name" location="London"/> </hello> """ ]]> </value> </property> </bean> </sm:component> </sm:activationSpec> As you can see the component is configured with a piece of Groovy to execute when the service is invoked. (BTW if you wanna see this script in action try the test case Now we'll go through the various options which are available when working with JBI and Groovy in ServiceMix Maintaining state across requestsIts often handy to keep track of state across requests. There is a variable called 'bindings' which you can use to maintain state... <activationSpec componentName="myServiceImpl" service="foo:myServiceImpl"> <component> <bean xmlns="" class="org.apache.servicemix.components.groovy.GroovyComponent"> <property name="scriptText"> <value> <![CDATA[ if (bindings.counter == null) { bindings.counter = 1 } else { ++bindings.counter } def date = new Date() outMessage.bodyText = "<response counter='$bindings.counter' date='$date'></response>" ]]> </value> </property> </bean></component></activationSpec> Working with JBI propertiesIn 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 outputGroovy provides various mechanism for generating the output (whether it is the result of a service or a transformation). Which mechanism you use depends on your use case and personal preference. String templatesYou 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 valuesYou can return a POJO as the body of a message - which other components can either transform or the default Marshaler // lets output the body as a POJO
outMessage.body = [3, 2, 1]
Using Groovy MarkupGroovy 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(person:inMessage.properties.name, location:'London')
}
|
Unsubscribe or edit your notifications preferences
