Versions Compared

Key

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

...

For example you could use the groovy function to create an Predicate in a Message Filter or as an Expression for a Recipient List

Groovy processors

Dependency

You should add the camel-groovy dependeny when using Groovy language with Camel. The generic camel-script is not optimized for best Groovy experience, and hence you should add camel-groovy as dependency.

Customizing Groovy Shell

Sometimes you may need to use custom GroovyShell instance in your Groovy expressions. To provide custom GroovyShell, add implementation of the You can use Groovy closures to define the processors in Java DSL. Static method org.apache.camel.language.groovy.extend.CamelGroovyMethods.toProcessor provides a bridge between Groovy closures and Java:GroovyShellFactory SPI interface to your Camel registry. For example after adding the following bean to your Spring context...

import static org.apache.camel.groovy.extend.CamelGroovyMethods.toProcessor ... from("direct:start").processor(toProcessor{ Exchange exchange -> exchange.in.body = 'body' });
Code Block
languagejava
public class CustomGroovyShellFactory implements GroovyShellFactory {
 
  public GroovyShell createGroovyShell(Exchange exchange) {
    ImportCustomizer importCustomizer = new ImportCustomizer();
    importCustomizer.addStaticStars("com.example.Utils");
    CompilerConfiguration configuration = new CompilerConfiguration();
    configuration.addCompilationCustomizers(importCustomizer);
    return new GroovyShell(configuration);
  }

}

...Camel will use your custom GroovyShell instance (containing your custom static imports), instead of the default one.

Example

Code Block
languagejava
// lets route if a line item is over $100
from("queue:foo").filter(groovy("request.lineItems.any { i -> i.value > 100 }")).to("queue:bar")

...