Apache Camel Tutorial - Hello World Example | CodeUsingJava..

Apache Camel Tutorial - File Copy Example


In a previous tutorial we have defined what is Apache Camel. In this tutorial we will be creating a simple maven project to copy files from one folder to another using Apache Camel. We will be making use of the Apache Camel File Component.

Table Of Contents :

Technology Stack

We will be making use of-
  • Java 1.8
  • Apache Camel 3.0.0-M2
  • Maven


The maven project we will be creating is as follows-
Apache Camel Maven Project
The pom.xml will be as follows-

<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">




In this hello world example we will be creating a simple route to copy files from one folder to another. In Camel all logic is written using Routes. Let us first quickly understand what are camel routes and then we will define a route to copy file from one location to another.

What are Routes?

Route is the most important unit of Apache Camel. Using Routes we define the message flow and integration logic for our application.
A Route consists of Message Channel which is used for passing messages from one endpoint to another. The end points of these message channels either consume of produce messages.

Apache Camel Endpoints

Apache Camel Routes can be written in various Domain Specific Languages(DSL). Most popular are-
  • Java DSL - A Java based DSL
  • Spring XML - A XML based DSL in Spring XML files
For this example we make use of Java DSL for writing Routes.

What are Components?

Component act as an endpoint factory using which we can interact with external systems. Camel provides a large number of components using which we can interact with externals systems.
For example here we have to transfer files from one folder to another, so we make use of the file component at both the end of the message channel.

Apache Camel File Copy Example
If suppose we have to transfer a file to a JMS Queue, then we make use of the File Component at one end and JMS Component at the other end.
Apache Camel Components
In Apache Camel for creating a route we need to extend RouteBuilder class and override configure method. Then in configure method we define our route. So our route using Java DSL will be as follows-

package com.codeusingjava.route;

import org.apache.camel.builder.RouteBuilder;

public class FileRouteBuilder extends RouteBuilder {

	public void configure() throws Exception {

To start camel routes we first need the CamelContext. Let us have a look at it.

What is CamelContext?

We cannot run our Camel Application only using the route. We also need the camel context which acts as a runtime system that runs and manages the routes. It is responsible for all managing all aspects of a route.
Apache Camel Maven Project
We create and configure Apache Camel Component using the following steps -
  • 1. Create CamelContext.
  • 2. Add routes to the CamelContext.
  • 3. Start the CamelContext. So that the Routes get executed.
  • 4. Stop the Camel Context.
We define the CamelContext in the CamelMain class as follows-

package com.codeusingjava;

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

import com.codeusingjava.route.FileRouteBuilder;

public class CamelMain {

	public static void main(String[] args) {
		CamelContext ctx = new DefaultCamelContext();
		FileRouteBuilder fileRouteBuilder = new FileRouteBuilder();

		try {
			Thread.sleep(5 * 60 * 1000);
		} catch (Exception e) {

Run the Application. The Route will get executed to copy all content from folder input to the folder output.


Apache Camel File Copy Example