Versions Compared

Key

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

...

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:

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

Code Block
java
java

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

Code Block
java
java

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

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

Using Groovy XML processing

...