You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »

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
...
   from('direct:test')
      .transform { it.in.body.reverse() }
      .process { println it.in.body }
...

The corresponding route in Java would look something like this:

MyRouteBuilder.java
...
   from("direct:test")
      .transform(new Expression() {
         @Override
         public Object evaluate(Exchange e) {
            return new StringBuffer(e.getIn().getBody().toString()).reverse().toString();
         }
      })
      .process(new Processor() {
         @Override
         public void process(Exchange e) {
           System.out.println(e.getIn().getBody());
         }
      });
...

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).

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-groovy</artifactId>
  <version>2.11.0</version>
</dependency>

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:

  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <compilerId>groovy-eclipse-compiler</compilerId>
    </configuration>
    <dependencies>
      <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-eclipse-compiler</artifactId>
	<version>2.7.0-01</version>
      </dependency>
    </dependencies>
  </plugin>

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:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <configuration>
      <additionalProjectnatures>
        <projectnature>org.eclipse.jdt.groovy.core.groovyNature</projectnature>
      </additionalProjectnatures>
      <classpathContainers>
        <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
        <classpathContainer>GROOVY_DSL_SUPPORT</classpathContainer>
      </classpathContainers>              
    </configuration>
  </plugin>	  

Using Closures in your routes

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:

...
   private String someValue
...
   from('direct:test')
      .process { Exchange exchange -> println exchange.in.body + someValue }
      .process { println it.in.body + someValue } // equivalent
...
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:

...
   private String someValue
...
   from('direct:test')
      .transform { it.in.body.reverse() + someValue }
      .setHeader("myHeader") { someValue.reverse() }
...
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:

...
   private String someValue
...
   from('direct:test')
      .filter { it.in.body != someValue }
...
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:

...
   private String separator
...
   from('direct:test1')
      .enrich('direct:enrich') { Exchange original, Exchange resource -> 
         original.in.body += resource.in.body + separator
         original  // don't forget to return resulting exchange
      }
...

Using Groovy XML processing

Using Groovy GStrings

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

def x = "It is currently ${ new Date() }"

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

Custom DSL extensions

  • No labels