Getting started with Camel, Spring and Maven

Getting up and running with Camel is not so hard, once you know how to do it. It took me a little more time than I wanted despite the extensive amount of examples provides by Camel. This post should help you get an up and running example in no time!

To start, I’ll start with the pom.xml:



    
        epub-organizer
        epub-organizer
        1.0
    
    4.0.0

    camel

    
            2.10.0
    

    
        
            org.apache.camel
            camel-core
            ${camel.version}
        
        
            org.apache.camel
            camel-spring
            ${camel.version}
        
    

This setups a simple application. Next, we’ll create a Camel route:

package nl.jworks.epub.organizer.routes;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spring.Main;

public class MyRouteBuilder extends RouteBuilder {
    public static void main(String[] args) throws Exception {
        new Main().run(args);
    }

    public void configure() {
        // set up a listener on the file component
        from("file://target/test?noop=true").beanRef("myProcessor", "process");
    }
}

The nice thing here is that a route is also executable, which makes testing of the route really easy. As you can see here, the application is looking into the target location, and will process everything found there by giving an Exchange to the process method of the myProcessor bean. This bean is wired in the camel-context.xml, which should be placed in the src/main/resources/META-INF/spring directory.




    
        nl.jworks.epub.organizer.routes
    

    

    

And the MyProcessor bean:

package nl.jworks.epub.organizer.routes;

import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.epub.EpubReader;
import org.apache.camel.Exchange;
import org.apache.camel.Message;

import javax.activation.DataHandler;
import java.io.ByteArrayInputStream;
import java.io.IOException;

public class MyProcessor {
    public void process(Exchange exchange) {
        try {
            System.out.println("process");
            Message in = exchange.getIn();
            byte[] file = in.getBody(byte[].class);
            System.out.println("bytes found:" + file.length);
        } catch (IOException e) {
            System.out.println("could not read file: " + e.getMessage());
        }
    }
}

And that’s it. That should get you up to speed with using Camel, Spring and Maven!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>