Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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 Processor, expression Expression, predicate Predicate, or aggregation strategy 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.

Overview

...

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:

Code Block
MyRouteBuilder.groovy
MyRouteBuilder.groovy

...
   from('direct:test')
      .transform { it.in.body.reverse() }
...

The corresponding route in Java would look something like this:

Code Block
MyRouteBuilder.java
MyRouteBuilder.java

...
   from('direct:test')
      .transform(new Expression() {
         public Object evaluate(Exchange e) {
            return new StringBuffer(e.getIn().getBody().toString()).reverse().toString();
         }
      });
   }
...

Using Closures

Using Groovy XML processing

...