1.Camel-File Transfer Example

This is the post excerpt.

File-based information exchange is common between distributed system. For example, A java application getting data from the legacy system in txt files and application read those txt files from inbox directory and after receiving a file in inbox directory file moved to outbox directory after processing.

In the following example, we will learn how to route a file from one directory to another directory.

For example .. emplyoyee.txt file is present in inbox directory and application will move it to the outbox directory.
employee.txt file content is

Joe,Smith,Developer,75000,10012009
Jane,Doe,Architect,80000,01152008
Jon,Anderson,Manager,85000,03182007
package com.kp.work.camel;

import org.apache.camel.builder.RouteBuilder;

public class EmployeeFileRouter extends RouteBuilder {

 @Override
 public void configure() throws Exception {
  from("file:C:/camel-filetransfer-example/inbox?noop=true")
  .to("file:C:/camel-filetransfer-example/outbox");
 }
}

If you want to keep the original file as is in inbox directory we need to set the noop option to true.
Try this example with option noop =false to differentiate the behavior.

If you want to give different filename use fileName option to set different file name.
Here is the modified configure method with fileName option.
File name changed from employee.txt to empdetails.txt

@Override
 public void configure() throws Exception {
  from("file:C:/camel-filetransfer-example/inbox?noop=true")
 .to("file:C:/camel-filetransfer-example/outbox?fileName=empdetails.txt");
 
 }  

By default camel moves file to .camel directory.If you want to move file to different directory you can use move option.Moving file to different directory is good option after processing because you can avoid processing same file over and over and you can also keep the backup of processed files.

Modified configure method with move option.

@Override
 public void configure() throws Exception {
 from("file:C:/camel-filetransfer-example/inbox?
      move=C:/camel-filetransfer-example/backup/${date:now:yyyyMMdd}/${file:name}")
  .to("file:C:/camel-filetransfer-example/outbox");
  
 
 }  

package com.kp.work.camel;

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

public class EmployeeMain {

	public static void main(String[] args) {
		CamelContext camelContext = new DefaultCamelContext();
		try {
			camelContext.addRoutes(new EmployeeFileRouter());
			camelContext.start();
			Thread.sleep(10000);
			camelContext.stop();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

In order to run this example we need camel-core dependency because file component is present in camel-core jar

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.kp.work</groupId>
  <artifactId>camel-filetransfer-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <camel-version>2.18.3</camel-version>
  </properties>
  <dependencies>
    
    
       <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>${camel-version}</version>
      </dependency>
 
  </dependencies>
</project>


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s