Versions Compared

Key

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

...

Code Block
java
titleMyRouteBuilder.groovy
java

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

...

Code Block
titleMyRouteBuilder.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());
         }
      });
...

...

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

Code Block
xml
xml

<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:

Code Block
xml
xml

  <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:

Code Block
xml
xml

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

...

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:

Code Block
java
java

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

...

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:

Code Block
java
java

...
   private String someValue
...
   from('direct:test')
      .transform { it.in.body.reverse() + someValue }
      .setHeader("myHeader") { someValue.reverse() }
...

...

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:

Code Block
java
java

...
   private String someValue

   // This time, the closure is stored in a variable
   def pred = { Exchange e -> e.in.body != someValue }
...
   from('direct:test')
      .filter(pred)
...

...

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:

Code Block
java
java

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

...

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:

Code Block
java
java

...
   private String someValue

   // This time, the closure is stored in a variable
   def pred = { Exchange e -> e.in.body != someValue }
...
   from('direct:test')
      // predicate(Closure) -> org.apache.camel.Predicate
      .filter(predicate(pred))
...

...

Code Block
java
titleUnmarshal XML with XmlParser
java

...
   from('direct:test1')
      .unmarshal().gnode() 
      // message body is now of type groovy.util.Node
...

...

Code Block
java
titleUnmarshal XML with XmlSlurper
java

...
   from('direct:test1')
      .unmarshal().gpath(false) // explicitly namespace-unaware
      // message body is now of type groovy.util.slurpersupport.GPathResult
...

...

Code Block
java
titleMarshal XML with XmlNodePrinter
java

...
   from('direct:test1')
      // message body must be of type groovy.util.Node
      .marshal().gnode()
...

...

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

Code Block

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

...

Code Block
java
titleMyExtension.java
java


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);
    }

}

...

Code Block
titleMETA-INF/services/org.codehaus.groovy.runtime.ExtensionModule

moduleName=my-extension
moduleVersion=2.11
extensionClasses=MyExtension
staticExtensionClasses=

...

Code Block
java
titleMyRoute.groovy
java

...
   fromId('direct:test1')
      .fork('direct:null','direct:not-null',body().isNull())
...

...

Code Block
java
titleMyRoute.java
java

...
   from("direct:test1")
      .routeId("direct:test1")
      .choice()
         .when(body().isNull())
            .to("direct:null")
         .otherwise()
            .to("direct:not-null");
...