The Apache camel tutorial below creates a route to poll the c:/temp/simple folder every minute (i.e. 60000 milliseconds) for files with patterns like test1.csv, test2.csv, etc. These files are then zipped and copied to the archive folder under c:/temp/simple.
Step 1: Define the Apache camel and spring libraries required.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>my-app</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Spring framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>3.1.2.RELEASE</version> </dependency> <!-- Aspectj --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.11</version> </dependency> <!-- Camel --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>2.10.4</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring</artifactId> <version>2.10.4</version> </dependency> </dependencies> </project> |
Step 2: Define the Spring configuration file applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <context:component-scan base-package="com.mycompany.app3" /> <context:annotation-config /> <context:spring-configured /> <task:annotation-driven /> <import resource="classpath*:route.xml" /> </beans> |
Step 3: Define the Apache configuration file with the relevant routes. The rouet.xml defines the CamelContext and simple_file_route.xml defines the route context. More files can be added and included in the rout.xml for additional routes in a modular fashion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <routeContext id="simpleFileRoute" xmlns="http://camel.apache.org/schema/spring"> <route id="fileIn"> <from uri="file://c:/temp/simple?include=test.*csv&delay=600000&initialDelay=5000" /> <marshal ref="zip" /> <to uri="file://c:/temp/simple/archive?fileName=${date:now:yyyyMMdd}/${file:name.noext}_${date:now:yyyyMMdd}_${date:now:hhmmss}.zip" /> </route> </routeContext> </beans> |
${file:name.noext} is a file expression language, and refer to the Camel manual for further details.
Step 4: Finally, the main class that runs that runs Apache camel in a stand-alone mode continuously polling for files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package com.mycompany.app3; import org.apache.camel.spring.Main; public class StandAloneCamelWithSpring { private Main main; public static void main(String[] args) throws Exception { StandAloneCamelWithSpring example = new StandAloneCamelWithSpring(); example.boot(); } private void boot() throws Exception { // create a Main instance main = new Main(); // enable hangup support so you can press ctrl + c to terminate the JVM main.enableHangupSupport(); main.setApplicationContextUri("applicationContext.xml"); // run until you terminate the JVM System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n"); main.run(); } } |
Step 5:Run the above class as a Java application, and create a new test1.csv file under c:\temp\simple and you will see that this files gets picked up, zipped, and copied to the archive folder.
Let’s extend the above to show how we can read a file say test1.csv in the format shown below
1 2 3 | firstname,surname, age John, Smith, 35 Peter, Smith, 25 |
and then convert it to a format shown below where fields firstname and surname are swapped.
1 2 3 | surname,firstname,age Smith,John,35 Smith,Peter,25 |
Step 1: Add camel-csv dependency to the pom.xml file that enables reading csv files
1 2 3 4 5 6 7 | <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-csv</artifactId> <version>2.10.4</version> </dependency> |
Step 2: The next step is to define the routes in the simple_file_route.xml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <routeContext id="simpleFileRoute" xmlns="http://camel.apache.org/schema/spring"> <route id="fileIn"> <from uri="file://c:/temp/simple/input?include=test.*csv&delay=600000&initialDelay=5000" /> <unmarshal> <csv skipFirstLine="true" /> </unmarshal> <to uri="bean:personMapper" /> <marshal> <csv delimiter="," /> </marshal> <convertBodyTo type="java.lang.String" /> <to uri="file://c:/temp/simple/output?fileName=${file:name.noext}_new.csv" /> </route> </routeContext> </beans> |
Step 3: The above route uses the “PersonMapper” bean class to transform the fields. This is basically translating the file by rearranging “surname” and “firstname” columns.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package com.mycompany.app3; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Component; @Component("personMapper") public class PersonMapper { public Object service(List<List<String>> data) { List<Map<String, String>> results = new ArrayList<Map<String, String>>(); Map<String, String> headerLine = new LinkedHashMap<String, String>(); headerLine.put("surname", "surname"); headerLine.put("firstname", "firstname"); headerLine.put("age", "age"); results.add(headerLine); for (List<String> line : data) { Map<String, String> resultLine = new LinkedHashMap<String, String>(); resultLine.put("surname", line.get(1)); resultLine.put("firstname", line.get(0)); resultLine.put("age", line.get(2)); results.add(resultLine); } return results; } } |
That’s all to it. This is the power of the integration framework Apache Camel. It supports 20+ protocols like ftp, smtp, etc.