Integrate with Groovy and Camel

Groovy is an easy language for its simplicity and the strong support from Java.

And Camel is a popular integration framework that works perfectly for small to medium projects.

However, setting up Camel, especially in Java is a little complex, especially when you just need to do a Demo

This leads us to think of the option of using Groovy to work with Camel, and provide some simple solution.

Following is an example of how basic Camel framework can be set up in Groovy with only 20 lines – well, I did remove empty lines to make it look small šŸ˜‰

And thanks to Groovy’s Grape dependency handling, it automatically grabs all the dependency packages for us — enables you to focus on the core transformation

Following code prints out a “Hello World!” string every 3 seconds

@Grab('org.apache.camel:camel-core:2.10.0')
@Grab('org.slf4j:slf4j-simple:1.6.6')
import org.apache.camel.*
import org.apache.camel.impl.*
import org.apache.camel.builder.*
def camelContext = new DefaultCamelContext()
camelContext.addRoutes(new RouteBuilder(){
def void configure(){
from("timer://jdkTimer?period=3000")
.to("log://camelLogger?level=INFO")
.process(new Processor(){
def void process(Exchange exchange){
println("Hello World!")
}
})
}
})
camelContext.start()
addShutdownHook{ camelContext.stop() }
synchronized(this){ this.wait() }