Sunday 23 December 2018

Apache Camel: HelloWorld Application

Aim: Read files from one directory and process them and write the files to some other directory.


This tutorial is divided into two steps.
a.   Setting up maven project in Eclipse
b.   Implementing file transfer functionality using Apache Camel.

Setting up maven project in Eclipse
Open Eclipse.

Right click on Project Explorer -> New -> Other


It opens ‘Select a wizard’ window. Search for ‘maven’ in Wizards: field.


Select ‘Maven Project’ and click on Next button.


In New Maven project window, select the checkbox ‘Create a simple project (skip archetype selection)’ and click on Next button.
Give Artifact Id, Group Id as ‘camelFileTransfer’ and click on Finish button.

Project structure created like below.



Open pom.xml and add camel dependencies. At the time of writing this post, 2.22.1 is the latest camel version.

I added below maven dependency.

<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-core</artifactId>
    <version>2.22.1</version>
</dependency>

Final pom.xml file looks like below.
<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>camelFileTransfer</groupId>
 <artifactId>camelFileTransfer</artifactId>
 <version>1</version>

 <dependencies>
  <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
  <dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-core</artifactId>
   <version>2.22.1</version>
  </dependency>

 </dependencies>
</project>


Implementing file transfer functionality using Apache Camel.
Create a package ‘com.sample.app.routes’ and Define the class FileCopyRoute like below.

FileCopyRoute.java
package com.sample.app.routes;

import org.apache.camel.builder.RouteBuilder;

public class FileCopyRoute extends RouteBuilder{

 @Override
 public void configure() throws Exception {
  this.from("file:C:\\Users\\Public\\demo?noop=true").to("file:C:\\Users\\Public\\demoCopy");
 }

}


Define the class 'Application.java' under the package com.sample.app.
Application.java
package com.sample.app;

import java.util.concurrent.TimeUnit;

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

import com.sample.app.routes.FileCopyRoute;

public class Application {
 public static void main(String args[]) throws Exception {
  CamelContext context = new DefaultCamelContext();

  context.addRoutes(new FileCopyRoute());

  context.start();

  TimeUnit.MINUTES.sleep(1);

  context.stop();
 }
}

Run Application.java, you can observe the application copies the files from the directory ‘C:\\Users\\Public\\demo’ to ‘C:\\Users\\Public\\demoCopy’.

Things to note here.
a.   If the destination folder ‘C:\\Users\\Public\\demoCopy’ is not exist, then camel create the folder for you.
b.   It copies only the files that are direct children in source folder.
c.   If you copy any files to source directory while the application is running, the same files are copied to destination directory.

file:C:\\Users\\Public\\demo?noop=true
Above statement is a java DSL statement. Above statement tells camel that copy all the files from the source directory. If you do not set the query argument noop=true, then camel move the files from source directory to destination directory.

What we did?
a.   Created a route that copies the files from one folder to other.
b.  Instantiated CamelContext (CamelContext context = new DefaultCamelContext();)
c.   Add the route defined in step a to camel context (context.addRoutes(new FileCopyRoute());)
d.  Start the camel context (context.start(); )
e.   Wait for some time to finish copying the files (TimeUnit.MINUTES.sleep(1);)
f.    Stop the camel context (context.stop();).

I hope you understand HelloWorld application. Let’s see more examples in later posts.

Reference




Previous                                                 Next                                                 Home

No comments:

Post a Comment