Search

Dark theme | Light theme

April 1, 2009

Poll for e-mail with Groovy and Apache Camel

Just recently I read this interesting article and it triggered me to read more about Apache Camel. Apache Camel is a framework for integration patterns with a lot of useful components. The syntax is straighforward and the Java DSL is easy to use.

The following example shows how we can poll our Google mail inbox every 10 minutes and log the message if the subject contains Groovy:

#!/usr/bin/env groovy

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-mail', version='1.6.0')
@Grab(group='org.apache.camel', module='camel-core', version='1.6.0')
class GroovyMailRoute extends GroovyRouteBuilder {
    protected void configure(){
        from("imaps://imap.gmail.com?username=username@gmail.com"
                                 + "&password=secret"
                                 + "&deleteProcessedMessages=false"
                                 + "&processOnlyUnseenMessages=true"
                                 + "&consumer.delay=600000")
            .filter { it.in.headers.subject.contains('Groovy') }
            .to("log:groovymail?showAll=true&multiline=true")
    }
}

def camelCtx = new DefaultCamelContext()
camelCtx.addRoutes(new GroovyMailRoute());
camelCtx.start();