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.
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:
The corresponding route in Java would look something like this:
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:
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.
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:
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:
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:
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:
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.
By default, XML processing is namespace-aware. You can change this by providing a boolean false parameter.
Currently, marshalling is only supported for groovy.util.Node objects.
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.
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!
Add a corresponding extension module descriptor to META-INF/services:
And now your Groovy route can look like this:
Using the plain Java DSL, the route would look something like this: