About the Groovy DSL

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.

Introduction

Because Groovy is syntactically very similar to Java, you can write your Groovy routes just like Java routes. The same Java DSL classes are being used, with the exception that some of the DSL classes get extended with a bunch of new methods at runtime. This is achieved by turning camel-groovy into a Groovy Extension Module that defines extension methods on existing classes.

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:

MyRouteBuilder.groovy

The corresponding route in Java would look something like this:

MyRouteBuilder.java

Developing with the Groovy DSL

To be able to use the Groovy DSL in your camel routes you need to add the a dependency on camel-groovy which implements the Groovy DSL.

If you use Maven you can just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions).

Additionally you need to make sure that the Groovy classes will be compiled. You can either use gmaven for this or, particularly with mixed projects containing Java and Groovy code, you might want to use the Groovy Eclipse compiler:

As Eclipse user, you might want to configure the Maven Eclipse plugin in a way so that your project is set up correctly for using Eclipse Plugin for Groovy when mvn eclipse:eclipse is executed:

Using Closures in your routes

Groovy closures can be used to write concise implementations of Camel processors, expressions, predicates, and aggregation strategies. It is recommended to keep more complicated implementations of these objects in their own classes, e.g. to be able to test them more easily and not to clutter up your routes with business logic.

Processor Closures

All Java DSL parameters of type org.apache.camel.Processor can be replaced by a closure that accepts an object of type org.apache.camel.Exchange as only parameter. The return value of the closure is disregarded. All closures may also refer to variables not listed in their parameter list. Example:

Expression Closures

All Java DSL parameters of type org.apache.camel.Expression can be replaced by a closure that accepts an object of type org.apache.camel.Exchange as only parameter. The return value of the closure is the result of the expression. Example:

Predicate Closures

All Java DSL parameters of type org.apache.camel.Predicate can be replaced by a closure that accepts an object of type org.apache.camel.Exchange as only parameter. The return value of the closure is translated into a boolean value representing the result of the predicate. Example:

Aggregation Strategy Closures

Java DSL parameters of type org.apache.camel.processor.aggregate.AggregationStrategy can be replaced by a closure that accepts two objects of type org.apache.camel.Exchange representing the two Exchanges to be aggregated. The return value of the closure must be the aggregated Exchange. Example:

Generic closure bridges

In addition to the above-mentioned DSL extensions, you can use closures even if no DSL method signature with closure parameters is available. Assuming there's no filter(Closure) method, you could instead write:

Similarly, expression(Closure) returns a Camel expression, processor(Closure) returns a Processor, and aggregator(Closure) returns an AggregationStrategy.

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.

Unmarshal XML with XmlParser

By default, XML processing is namespace-aware. You can change this by providing a boolean false parameter.

Unmarshal XML with XmlSlurper

Currently, marshalling is only supported for groovy.util.Node objects.

Marshal XML with XmlNodePrinter

Using Groovy GStrings

Groovy GStrings are declared inside double-quotes and can contain arbitrary Groovy expressions like accessing properties or calling methods, e.g.

Because GStrings aren't Strings, camel-groovy adds the necessary TypeConverter to automatically turn them into the required type.

Custom DSL extensions

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:

MyExtension.java

Add a corresponding extension module descriptor to META-INF/services:

META-INF/services/org.codehaus.groovy.runtime.ExtensionModule

And now your Groovy route can look like this:

MyRoute.groovy

Using the plain Java DSL, the route would look something like this:

MyRoute.java
© 2004-2014 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram