Fork me on GitHub

Dec 17, 2010

Let's ride the Camel - The simplest program to understand Camel

Camel Is and integration framework. It can pass data from one system to another. Here I will create a simple program to copy a file another place. Maybe this is simple but I think this easiest way to understand camel. I will use camel core and Java to do this. Then I will use Maven to do the same thing.

Think you need to copy a file call "File_A" in "Inbox" directory and you need to copy that file into "Outbox" directory. We can write a simple java I/O program to do that. It will be like this;

import java.io.*;

public class FileCopier {

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

File inboxDir= new File("Inbox");
File outboxDir= new File("Outbox");

outboxDir.mkdir();
File[] files=inboxDir.listFiles();
for (File source : files)
   if (source.isFile()){
File dest=new File(outboxDir.getPath() + File.separator + source.getName());
copyFile(source, dest);
}
   }

   public static void copyFile(File source, File dest) throws IOException {

  OutputStream out = new FileOutputStream(dest);
byte[] buffer=new byte[(int) source.length()];
FileInputStream in= new FileInputStream(source);
in.read(buffer);
try {
out.write(buffer);
}
finally {
out.close();
in.close();
}
   }
}

Note : You may need to create the Inbox directory in the same directory where FileCopier.java ecesis. Otherwise you must give the correct path. Next thing is, before you run this program make sure that the file Inbox/File_A ecesis.


Now we can try the same thing with camel.
To use camel we need to download camel library files (http://camel.apache.org/), and place them in the classpath. Then try this;

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

public class FileCopierWithCamel {

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

CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder(){
  public void configure(){ from("file:Inbox?noop=true").to("file:Outbox");}
});
context.start();
Thread.sleep(10000);
context.stop();
   }

}


So, I think now you can see the difference. Thread.sleep() is to give some time to copy the file. In this program, it required to download camel. But If we use maven, maven will manage that part. No need to copying to class paths and so on.

To do the same thing with the help of maven you can try;
mvn archetype:generate

Then chose the default one - "maven-archetype-quickstart" and give relevant details. This will create a files and a directory. It will look like pom.xml, src. Source code will be placed in src directory and pom.xml will contain all dependencies and plugin details. We use camel as a dependency, so we need to edit the pom.xml. Add this code segment in between the <dependencies> </dependencies> tags.

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

For this program, it will require only the camel-core, but for some programs there are many dependencies. You can find these details from the camel website.

Now we need to replace the code part. You can simply replace the maven generated "App.java" from previously created FileCopierWithCamel.java file. You should insert the package details in the FileCopierWithCamel.java file and that can be grabbed from the "App.java" file.
As previous, we need to create "Inbox/File_A". It should create where the pom.xml ecesis. Now the directory structure will like pom.xml, src, Inbox.

Then you can run it by;
mvn compile java:exec -Dexec.mainClass=<package name>.FileCopierWithCamel

Now you can see, that the program had created the "Outbox" Directory and inside that, you can see the "File_A".