Groovy and Grape - easiest way to send gtalk message with Apache Camel?

Groovy 1.6 makes it really easy to quickly try out new frameworks. Dependency management build into the language allows you to write simple scripts, all required libraries will be downloaded automatically when you run it.

Here’s an example using Apache Camel. This script can simply be run from the command prompt. Groovy will compile it, download all dependencies and run it. The dependencies are specified using the @Grab annotation. Groovy uses Ivy to download these dependecies.


import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.language.groovy.GroovyRouteBuilder;

@Grab(group='org.apache.camel', module='camel-groovy', version='1.6.0')
@Grab(group='org.apache.camel', module='camel-xmpp', version='1.6.0')
@Grab(group='org.apache.camel', module='camel-core', version='1.6.0')
class SampleRoute extends GroovyRouteBuilder {
  protected void configure(){
      to("xmpp://[email protected]?");

def camelCtx = new DefaultCamelContext()
camelCtx.addRoutes(new SampleRoute());

The script defines a Camel Route which will look for files in the folder /tmp/jabber. Whenever it finds a new file, the file is sent to the gtalk account specified.

Before you can run this script, you need to configure an extra maven repository. The smack library used to do xmpp communication from Java isn’t included in any of the default maven repositories, so we need to add a ServiceMix repository, hosted by apache. All the other lines are copied from the default grapeConfig. Save the following document in $HOME/.groovy/grapeConfig.xml. It’s a normal ivy settings configuration file, but specifically used by Groovy.

<?xml version="1.0" encoding="utf-8"?>
  <settings defaultResolver="downloadGrapes" />
	<chain name="downloadGrapes">
	  <filesystem name="cachedGrapes">
	    <ivy pattern="${user.home}/.groovy/grapes/[organisation]/[module]/ivy-[revision].xml" />
	    <artifact pattern="${user.home}/.groovy/grapes/[organisation]/[module]/[type]s/[artifact]-[revision].[ext]" />
	  <!-- todo add 'endorsed groovy extensions' resolver here -->
	  <ibiblio name="codehaus" root="" m2compatible="true" />
	  <ibiblio name="ibiblio" m2compatible="true" />
	  <ibiblio name="java.net2" root="" m2compatible="true" />
	  <ibiblio name="apache-servicemix-repository" root="" m2compatible="true" usepoms="true" />

One small script and you’re running a small ESB. Doesn’t get much simpler than this.

blog comments powered by Disqus