Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

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:

Code Block
java
java
titleMyRouteBuilder.groovyjava
...
   from('direct:test')
      .transform { it.in.body.reverse() }
      .process { println it.in.body }
...

...

Groovy provides special XML processing support through its XmlParser, XmlNodePrinter and XmlSlurper classes. camel-groovy provides two data formats to use these classes directly in your routes.

Code Block
java
java
titleUnmarshal XML with XmlParserjava
...
   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 false parameter.

Code Block
java
java
titleUnmarshal XML with XmlSlurperjava
...
   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 groovy.util.Node objects.

Code Block
java
java
titleMarshal XML with XmlNodePrinterjava
...
   from('direct:test1')
      // message body must be of type groovy.util.Node
      .marshal().gnode()
...

...

You 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!
As an example, let's write two DSL extensions to make commonly used DSL methods more concise:

Code Block
java
java
titleMyExtension.javajava
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);
    }

}

...

And now your Groovy route can look like this:

Code Block
java
java
titleMyRoute.groovyjava
...
   fromId('direct:test1')
      .fork('direct:null','direct:not-null',body().isNull())
...

Using the plain Java DSL, the route would look something like this:

Code Block
java
java
titleMyRoute.javajava
...
   from("direct:test1")
      .routeId("direct:test1")
      .choice()
         .when(body().isNull())
            .to("direct:null")
         .otherwise()
            .to("direct:not-null");
...