Groovy plus camel magic
It’s such a pain in the ass complex task integrate enterprise legacy systems. Bad architectural decisions, poor implementation, or maybe just ‘out-of-date’ tecnology sometimes make integration a hard task.
For some time I’ve been studying some dynamic languages. Groovy seems like a good choice, since I really like java (Erlang and Ruby are in my TODO list).
Some time ago I heard about Apache Camel, an integration framework based on well known Enterprise Integration Patterns.
Looking at suported languages, WOW it has groovy support. So, let’s stop talking and show real code that sends messages from files in a filesystem directory to a gtalk account:
import org.apache.camel.impl.DefaultCamelContext import org.apache.camel.language.groovy.GroovyRouteBuilder
We need to import DefaultCamelContex, where we configure routes and the policies to use.
Also, we use GroovyRouteBuilder, base class with route management capabilities
@Grab(group='org.apache.camel', module='camel-groovy', version='1.6.0') @Grab(group='org.apache.camel', module='camel-core', version='1.6.0') @Grab(group='org.apache.camel', module='camel-xmpp', version='1.6.0')
Uses Grape to retrieve dependencies needed: camel groovy supoort, camel core and camel xmpp component.
class SimpleXMPPRoute extends GroovyRouteBuilder {
protected void configure(){
from("file:///tmp/from/?noop=true").
to("xmpp://talk.google.com:5222/<target_user>?serviceName=gmail.com&user=<sender_user>&password=<sender_passwd>");
}
}
Here we configure our route inside the configure method. Note that our class extends GroovyRouteBuilder.
First, we define the source of data. The example above uses a File component, scanning a directory for any new (or modified) file.
When modifications are detected, the framework ‘sends’ the file to the XMPP component, which sends the file contents to the specified gtalk user.
def camelCtx = new DefaultCamelContext() camelCtx.addRoutes(new SimpleXMPPRoute()) camelCtx.start()
Finally, create the camel context, add our xmpp route and start the context. That’s it!
We can integrate several components like databases, jms queues and so on. It’s easy to transform output data from some system and then plug that output as input to others systems, with no acknowledgment of each other .
Simple, isn’t it?
=D


