Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the early version of the new website
https://camel.apache.org/staging/
We would very much like to receive any feedback on the new site, please join the discussion on the Camel user mailing list.
About the Groovy DSLThe 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. IntroductionBecause 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 DSLTo 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 <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 routesGroovy closures can be used to write concise implementations of Camel processors, expressions, predicates, and aggregation strategies. It is recommended to keep more complicated implementations of these objects in their own classes, e.g. to be able to test them more easily and not to clutter up your routes with business logic. Processor ClosuresAll Java DSL parameters of type ... private String someValue ... from('direct:test') .process { Exchange exchange -> println (exchange.in.body + someValue) } .process { println (it.in.body + someValue) } // equivalent ... Expression ClosuresAll Java DSL parameters of type ... private String someValue ... from('direct:test') .transform { it.in.body.reverse() + someValue } .setHeader("myHeader") { someValue.reverse() } ... Predicate ClosuresAll Java DSL parameters of type ... 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) ... Aggregation Strategy ClosuresJava DSL parameters of type ... 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 } ... Generic closure bridgesIn 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 ... 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)) ... Similarly, Using Groovy XML processingGroovy provides special XML processing support through its Unmarshal XML with XmlParser ... from('direct:test1') .unmarshal().gnode() // message body is now of type groovy.util.Node ... By default, XML processing is namespace-aware. You can change this by providing a boolean Unmarshal XML with XmlSlurper ... from('direct:test1') .unmarshal().gpath(false) // explicitly namespace-unaware // message body is now of type groovy.util.slurpersupport.GPathResult ... Currently, marshalling is only supported for Marshal XML with XmlNodePrinter ... from('direct:test1') // message body must be of type groovy.util.Node .marshal().gnode() ... Using Groovy GStringsGroovy 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 extensionsYou can easily define your custom extensions - be it as a Java DSL extension for your Groovy routes or for any other class unrelated to Camel. All you have to do is to write your extension methods and provide a extension module descriptor - the details are described in the Groovy documentation. And as long as you don't require other extension methods, you can even use plain Java code to achieve this! MyExtension.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); } } Add a corresponding extension module descriptor to META-INF/services/org.codehaus.groovy.runtime.ExtensionModule moduleName=my-extension moduleVersion=2.11 extensionClasses=MyExtension staticExtensionClasses= And now your Groovy route can look like this: MyRoute.groovy ... fromId('direct:test1') .fork('direct:null','direct:not-null',body().isNull()) ... Using the plain Java DSL, the route would look something like this: MyRoute.java ... from("direct:test1") .routeId("direct:test1") .choice() .when(body().isNull()) .to("direct:null") .otherwise() .to("direct:not-null"); ... |