...
The Groovy DSL implementation is built on top of the existing Java-based DSL, but it additionally allows to use Groovy language features in your routes, particularly Closures acting as Processor, Expression, Predicate, or Aggregation Strategy.
With the Groovy DSL you write your RouteBuilder classes entirely in Groovy, while the scripting component allows to embed small scripts into Java routes. The Groovy DSL requires Groovy 2.0 or newer and is available as of Camel 2.11.
...
The majority of the extension methods allow Closures to be used as parameters e.g. for expressions, predicates, processors. The following example reverses a string in the message body and then prints the value to System.out:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
... from('direct:test') .transform { it.in.body.reverse() } .process { println it.in.body } ... |
...
Using Groovy XML processing
Groovy provides special XML processing support through its XmlParser
, XmlNodePrinter
and XmlSlurper
classes. camel-groovy provides two data formats to use these classes directly in your routes.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
... from('direct:test1') .unmarshal().gnode() // message body is now of type groovy.util.Node ... |
By default, XML processing is namespace-aware. You can change this by providing a boolean false
parameter.
Code Block | |||||||
---|---|---|---|---|---|---|---|
| |||||||
... from('direct:test1') .unmarshal().gpath(false) // explicitly namespace-unaware // message body is now of type groovy.util.slurpersupport.GPathResult ... |
Currently, marshalling is only supported for groovy.util.Node
objects.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
... from('direct:test1') // message body must be of type groovy.util.Node .marshal().gnode() ... |
Using Groovy GStrings
Groovy GStrings are declared inside double-quotes and can contain arbitrary Groovy expressions like accessing properties or calling methods, e.g.
...
You can easily define your custom extensions - be it as a Java DSL extension for your Groovy routes or for any other class unrelated to Camel. All you have to do is to write your extension methods and provide a extension module descriptor - the details are described in the Groovy documentation. And as long as you don't require other extension methods, you can even use plain Java code to achieve this!
As an example, let's write two DSL extensions to make commonly used DSL methods more concise:
Code Block | |||||||
---|---|---|---|---|---|---|---|
| |||||||
import org.apache.camel.Endpoint; import org.apache.camel.Predicate; public final class MyExtension { private MyExtension() { // Utility Class } // Set the id of a route to its consumer URI public static RouteDefinition fromId(RouteDefinition delegate, String uri) { return delegate.from(uri).routeId(uri); } public static RouteDefinition fromId(RouteDefinition delegate, Endpoint endpoint) { return delegate.from(endpoint).routeId(endpoint.getEndpointUri()); } // Make common choice pattern more concise public static ProcessorDefinition<?> fork(ProcessorDefinition<?> delegate, String uri1, String uri2, Predicate predicate) { return delegate.choice().when(predicate).to(uri1).otherwise().to(uri2); } } |
...
And now your Groovy route can look like this:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
... fromId('direct:test1') .fork('direct:null','direct:not-null',body().isNull()) ... |
Using the plain Java DSL, the route would look something like this:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
... from("direct:test1") .routeId("direct:test1") .choice() .when(body().isNull()) .to("direct:null") .otherwise() .to("direct:not-null"); ... |