Back Home

Getting Started

A minimal camel project can look like

  public static void main(String[] args) {
        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() }
    }

with the gradle dependencies of

def camelVersion = "2.19.0"
 
dependencies {
    compile "org.apache.camel:camel-core:${camelVersion}"
    compile "org.apache.camel:camel-elasticsearch:${camelVersion}"
    compile "org.apache.camel:camel-jdbc:${camelVersion}"
    compile 'org.slf4j:slf4j-simple:1.6.6'
}

Invoking Beans

class UpperCaseTextService {
    def String transform(String text) {
        return text.toUpperCase()
    }
}
def jndiContext = new JndiContext();
jndiContext.bind("upperCaseTextService", new UpperCaseTextService())
 
def dataDir = "/${System.properties['user.home']}/test/file-poller-demo"
def camelContext = new DefaultCamelContext(jndiContext)
camelContext.addRoutes(new RouteBuilder() {
    def void configure() {
        from("file://${dataDir}/in")
            .to("log://camelLogger")
            .to("bean://upperCaseTextService?method=transform")
            .to("file://${dataDir}/out")
    }
})
camelContext.start()
 
addShutdownHook{ camelContext.stop() }
synchronized(this){ this.wait() }

Camel and gmail

Sending mail:

import org.codehaus.groovy.grails.commons.*

class GetMailRoute {
    
    def configure = {
        def config = ConfigurationHolder.config

        from("imaps://imap.gmail.com?username=" + config.camel.route.gmail.username
            + "&password=" + config.camel.route.gmail.password
            + "&folderName=GoogleAnalytics"
            + "&consumer.delay=" + config.camel.route.gmail.pollInterval)
        .filter { 
            it.in.headers.subject.contains('Analytics')
        }
        .process{ exchange ->
            exchange.in.attachments.each { attachment ->
                def datahandler = attachment.value
                def xml = exchange.context.typeConverter.convertTo(String.class, datahandler.inputStream)
                def file = new File(config.camel.route.save.dir, datahandler.name) << xml
                log.info "Saved " + file.name
            }
        }
    }
}

Kotlin Dsl

Building and Running

 
camel_notes.txt · Last modified: 2017/06/16 08:09 by root
 
RSS - 200 © CrosswireDigitialMedia Ltd