Google

Jun 3, 2013

Standalone Apache Camel and Spring tutorial

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.

<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

<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.

<?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.

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.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home