Monthly Archives: October 2019

Using Apache Camel from Groovy

Apache Camel is an open source integration framework that empowers you to quickly and easily integrate various systems consuming or producing data.

Apache Groovy is a Java-syntax-compatible object-orientedprogramming language for the Java platform. It is both a static and dynamic language with features similar to those of PythonRuby, and Smalltalk. It can be used as both a programming language and a scripting language for the Java Platform, is compiled to Java virtual machine (JVM) bytecode, and interoperates seamlessly with other Java code and libraries. Groovy uses a curly-bracket syntax similar to Java’s. Groovy supports closures, multiline strings, and expressions embedded in strings. Much of Groovy’s power lies in its AST transformations, triggered through annotations. [Wikipedia]

Create a file camel-test.groovy like the following

 @Grab('org.apache.camel:camel-core:2.21.5')
 @Grab('javax.xml.bind:jaxb-api:2.3.0')
 @Grab('org.slf4j:slf4j-simple:1.7.21')
 @Grab('javax.activation:activation:1.1.1')

 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")
     }
 })
 camelContext.start()
 addShutdownHook{ camelContext.stop() }
 synchronized(this){ this.wait() }

Test it with

JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64 groovy camel-test.groovy