HelloWorld Camel tutorial

The following is the most basic example of data integration using Camel. We will show how to copy files from one folder to another using a simple Camel route.

Provided that you have downloaded Camel, launch your IDE and create a simple Java project containing this class:

import java.util.Scanner;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class App {
  public static void main(String args[]) throws Exception {
    CamelContext context = new DefaultCamelContext();
    context.addRoutes(new RouteBuilder() {
      public void configure() {
    System.out.println("Press enter to continue...");
    Scanner keyboard = new Scanner(System.in);

The purpose of this simple code is to route files from folder /home/francesco to /home/camel.

The most interesting part is the CamelContext: every Camel application uses a CamelContext that’s subsequently started and then stopped. By starting the CamelContext, you will be able to access Routes in Camel are defined in such a way that they flow when read. This route can be read like this: route files from /home/francesco with the noop option set, and send to /home/camel.

The String “file:/home/francesco?noop=true” is the Endpoint URI of a Route. It is composed of three parts: The Schema (file), the Context Path (/home/francesco) and Options (noop=true)

The noop option tells Camel to leave the source file as is. If you didn’t use this option, the file would be moved.

In order to compile this example, you will need to include the Camel core libraries and the slf4j log library:

helloworld camel

Running with Maven

Creating the same project with Maven is pretty easy as well: start by setting up a quickstart project:

D:\maven>mvn archetype:generate -DgroupId=com.sample -DartifactId=hellocamel -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Now replace the com.sample.App class that is created with the quickstart archetype with the one above. Finally, include Camel core dependency in your pom.xml:


You can run your application using the exec task as follows:

mvn exec:java -Dexec.mainClass="com.sample.App" 

3 Ways to run a Camel Application

What we have just seen is just an example to run a Camel based application. Actually there are three ways to run a Camel application:

1 – Running as a Java standalone application

Camel runs in a standalone application triggering the CamelContext initialization. This approach has been described at the beginning of this tutorial and just needs to initialize the CamelContext from your Java code and package the required Camel libraries in your application:

public class JavaRouter {

   public static void main(String[] args) throws Exception {

      context.addRoutes(new RouteBuilder() {

         public void configure() {


2 – Running Camel with Spring and a Java starter class

An alternative way is to trigger the startup with the Spring Framework.

public class SpringRouter {

   public static void main(String[] args) throws Exception {

      AbstractXmlApplicationContext springCtx = new  ClassPathXmlApplicationContext("META-INF/camelContext.xml");







Here is the example camelContext.xml file:

<beans xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <camelContext id="camel5" xmlns="http://camel.apache.org/schema/spring">

       <routeBuilder ref="myBuilder" />    


  <bean id="myBuilder" class="org.apache.camel.spring.example.test1.MyRouteBuilder"/>


3 – Running Camel with Spring and Camel Maven plugin

The last option allows you to write and execute Camel routes without writing Java code at all. You will define your route in Spring’s application context file and use Camel’s Maven plugin to compile and run the code. Here is the application-context.xml file:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
   http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">

  <camelContext xmlns="http://camel.apache.org/schema/spring">
      <from uri="file://data/in" />
      <to uri="file://data/out" />


And this is the pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


  <name>Using Camel for Simple file copy - with Spring integration</name>



        <!-- the file must be in the classpath -->


To run the above example, execute the following command in the shell:

mvn clean compile camel:run