Apache Camel Tutorial - File Copy Example | CodeUsingJava..

Apache Camel Tutorial - File Copy Example

Overview

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.

Technology Stack

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

Implementation

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">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.codeusingjava</groupId>
	<artifactId>apache-camel-copy-file</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>3.0.0-M2</version>
		</dependency>

	</dependencies>
</project>

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.
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. A Route consists of Message Channel for using which applications communicate using Messages. The end points of these message channels either consume of produce messages.

Apache Camel Endpoints

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 {

	@Override
	public void configure() throws Exception {
		from("file:C:/input").to("file:C:/output");
	}

}

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 {
			ctx.addRoutes(fileRouteBuilder);
			ctx.start();
			Thread.sleep(5 * 60 * 1000);
			ctx.stop();
		} catch (Exception e) {
			e.printStackTrace();
		}

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

Downloads-

Apache Camel File Copy Example