2.Camel-Data transformation with BeanIO

BeanIO is an open-source Java framework for marshalling and unmarshalling Java beans from a flat file, stream, or simple String object. In this example, we have flat-file(employee.txt) with comma-separated values. We will convert data into Employees java object.

BeanIO is configured using a mappings XML file where we define the mapping from the flat format to Objects (POJOs).

To use the camel BeanIODataFormat class we need to configure the data format using mapping file and stream name.

Mapping file name is employee_mapping.xml and stream name is employeeStream.

<beanio xmlns="http://www.beanio.org/2012/03" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">
  <stream name="employeeStream" format="csv" >
    <group name="employees" class="com.kp.work.vo.Employees" minOccurs="0" maxOccurs="unbounded">
     <record name="employee"  class="com.kp.work.vo.Employee" collection="list"  minOccurs="1" maxOccurs="unbounded">
      <field name="firstName"/>
      <field name="lastName" />
      <field name="title" />
      <field name="salary"/>
      <field name="hireDate" format="MMddyyyy"  />
     </record>
    </group>
  </stream>
</beanio>

package com.kp.work.vo;
import java.io.Serializable;
import java.util.List;


public class Employees  implements Serializable 
{
  
	 private static final long serialVersionUID = 1L;
	 private List<Employee> employee = null;

	public List<Employee> getEmployee() {
		return employee;
	}

	public void setEmployee(List<Employee> employee) {
		this.employee = employee;
	}
 
  
}

package com.kp.work.vo;

import java.io.Serializable;
import java.util.Date;
public class Employee  implements Serializable{
 
	private static final long serialVersionUID = 1L;
	
	private String firstName;
	
	private String lastName;
	
	private  String title;
	
	private int salary;
	
	private  Date hireDate;
	
    
    public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public Date getHireDate() {
		return hireDate;
	}
	public void setHireDate(Date hireDate) {
		this.hireDate = hireDate;
	}
	
	
	
    
   
}

package com.kp.work.camel.beanio;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.beanio.BeanIODataFormat;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.DataFormat;

public class EmployeeParsingRoute extends RouteBuilder {

	DataFormat employeeDataformat = new BeanIODataFormat("mapping/employee_mapping.xml", "employeeStream");

	@Override
	public void configure() throws Exception {
		from("file:C:/camel-learning/camel-datatransformation-example/employee?noop=true")
		.unmarshal(employeeDataformat)
		.process(new EmployeeFileProcessor());

	}

	public static void main(String[] args) {
		CamelContext camelContext = new DefaultCamelContext();
		try {
			camelContext.addRoutes(new EmployeeParsingRoute());

			camelContext.start();

			Thread.sleep(10000);
			camelContext.stop();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}




package com.kp.work.camel.beanio;

import java.util.ArrayList;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

import com.kp.work.vo.Employee;
import com.kp.work.vo.Employees;

public class EmployeeFileProcessor implements Processor {
	public void process(Exchange exchange) throws Exception {
		@SuppressWarnings("unchecked")
		ArrayList<Employees> emps = exchange.getIn().getBody(ArrayList.class);
		Employees employees = emps.get(0);
		for(Employee emp : employees.getEmployee()){
			System.out.println("EmployeeName::"+emp.getFirstName()+ " " +emp.getLastName());
		}
		
	}

}

To use BeanIO in Camel routes we need to add camel-beanio dependency in maven file.

<?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.camel</groupId>
	<artifactId>camel-datatransformation-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>camel-poc</name>
	<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-beanio</artifactId>
			<version>${camel-version}</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>


		<dependency>
			<groupId>org.mockito</groupId>
			<artifactId>mockito-all</artifactId>
			<version>1.10.19</version>
			<scope>test</scope>
		</dependency>

	</dependencies>
</project>

Here is the output of Example.

EmployeeName::Joe Smith
EmployeeName::Jane Doe
EmployeeName::Jon Anderson

Here is the project details.

camel-datatransformation

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>