Merge pull request #350 from yrpatil/master
Using Apache Camel with Spring
This commit is contained in:
commit
9d6f56c40a
|
@ -0,0 +1,28 @@
|
|||
|
||||
<b><h2>Configure and Use Apache Camel with Spring</h2></b>
|
||||
|
||||
This article will demonstrate how to configure and use Apache Camel with Spring Framework.
|
||||
|
||||
<b><h2>Relevant Article</h2></b>
|
||||
|
||||
<ul>
|
||||
<li><a href="http://camel.apache.org/">Apache Camel</a></li>
|
||||
<li><a href="http://www.enterpriseintegrationpatterns.com/patterns/messaging/toc.html">Enterprise Integration Patterns</a></li>
|
||||
</ul>
|
||||
|
||||
<b><h2>Framework Versions:</h2></b>
|
||||
|
||||
<ul>
|
||||
<li>Spring 4.2.4</li>
|
||||
<li>Apache Camel 2.16.1</li>
|
||||
</ul>
|
||||
|
||||
<b><h2>Build and Run Application</h2></b>
|
||||
|
||||
To build this application execute following maven command in ApacheCamelFileProcessor directory.
|
||||
|
||||
<code>mvn clean install</code>
|
||||
|
||||
To run this application you can either run our main class org.apache.camel.main.App from your IDE or you can execute following maven command:
|
||||
|
||||
<code>mvn exec:java -Dexec.mainClass="org.apache.camel.main.App"</code>
|
|
@ -0,0 +1,67 @@
|
|||
<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>org.apache.camel</groupId>
|
||||
<artifactId>spring-apache-camel</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>spring-apache-camel</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<env.camel.version>2.16.1</env.camel.version>
|
||||
<env.spring.version>4.2.4.RELEASE</env.spring.version>
|
||||
<java.version>1.7</java.version>
|
||||
<junit.version>4.1</junit.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.camel</groupId>
|
||||
<artifactId>camel-core</artifactId>
|
||||
<version>${env.camel.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.camel</groupId>
|
||||
<artifactId>camel-spring</artifactId>
|
||||
<version>${env.camel.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.camel</groupId>
|
||||
<artifactId>camel-stream</artifactId>
|
||||
<version>${env.camel.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${env.spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<verbose>true</verbose>
|
||||
<fork>true</fork>
|
||||
<compilerVersion>${java.version}</compilerVersion>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,12 @@
|
|||
package org.apache.camel.main;
|
||||
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class App {
|
||||
public static void main(final String[] args) throws Exception {
|
||||
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context.xml");
|
||||
// Keep main thread alive for some time to let application finish processing the input files.
|
||||
Thread.sleep(5000);
|
||||
applicationContext.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package org.apache.camel.processor;
|
||||
|
||||
import org.apache.camel.Exchange;
|
||||
import org.apache.camel.Processor;
|
||||
|
||||
public class FileProcessor implements Processor {
|
||||
|
||||
public void process(Exchange exchange) throws Exception {
|
||||
String originalFileContent = exchange.getIn().getBody(String.class);
|
||||
String upperCaseFileContent = originalFileContent.toUpperCase();
|
||||
exchange.getIn().setBody(upperCaseFileContent);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
|
||||
|
||||
<camelContext xmlns="http://camel.apache.org/schema/spring">
|
||||
|
||||
<route customId="true" id="route1">
|
||||
|
||||
<!-- Read files from input directory -->
|
||||
<from uri="file://src/test/data/input" />
|
||||
|
||||
<!-- Transform content to UpperCase -->
|
||||
<process ref="myFileProcessor" />
|
||||
|
||||
<!-- Write converted file content -->
|
||||
<to uri="file://src/test/data/outputUpperCase" />
|
||||
|
||||
<!-- Transform content to LowerCase -->
|
||||
<transform>
|
||||
<simple>${body.toLowerCase()}</simple>
|
||||
</transform>
|
||||
|
||||
<!-- Write converted file content -->
|
||||
<to uri="file://src/test/data/outputLowerCase" />
|
||||
|
||||
<!-- Display process completion message on console -->
|
||||
<transform>
|
||||
<simple> .......... File content conversion completed ..........
|
||||
</simple>
|
||||
</transform>
|
||||
<to uri="stream:out" />
|
||||
|
||||
</route>
|
||||
|
||||
</camelContext>
|
||||
|
||||
<bean id="myFileProcessor" class="org.apache.camel.processor.FileProcessor" />
|
||||
</beans>
|
|
@ -0,0 +1 @@
|
|||
THIS IS UPPERCASE CONTENT. this is lowercase content.
|
|
@ -0,0 +1,117 @@
|
|||
package org.apache.camel.main;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.apache.camel.util.FileUtil;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AppTest extends TestCase {
|
||||
|
||||
private static final String FILE_NAME = "file.txt";
|
||||
private static final String SAMPLE_INPUT_DIR = "src/test/data/sampleInputFile/";
|
||||
private static final String TEST_INPUT_DIR = "src/test/data/input/";
|
||||
private static final String UPPERCARE_OUTPUT_DIR = "src/test/data/outputUpperCase/";
|
||||
private static final String LOWERCASE_OUTPUT_DIR = "src/test/data/outputLowerCase/";
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Prepare input file for test
|
||||
copySampleFileToInputDirectory();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
System.out.println("Deleting the test input and output files...");
|
||||
deleteFile(TEST_INPUT_DIR);
|
||||
deleteFile(LOWERCASE_OUTPUT_DIR);
|
||||
deleteFile(UPPERCARE_OUTPUT_DIR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void testMain() throws Exception {
|
||||
App.main(null);
|
||||
|
||||
String inputFileContent = readFileContent(SAMPLE_INPUT_DIR+FILE_NAME);
|
||||
String outputUpperCase = readFileContent(UPPERCARE_OUTPUT_DIR+FILE_NAME);
|
||||
String outputLowerCase = readFileContent(LOWERCASE_OUTPUT_DIR+FILE_NAME);
|
||||
|
||||
System.out.println("Input File content = ["+inputFileContent+"]");
|
||||
System.out.println("UpperCaseOutput file content = ["+outputUpperCase+"]");
|
||||
System.out.println("LowerCaseOtput file content = ["+outputLowerCase+"]");
|
||||
|
||||
assertEquals(inputFileContent.toUpperCase(), outputUpperCase);
|
||||
assertEquals(inputFileContent.toLowerCase(), outputLowerCase);
|
||||
}
|
||||
|
||||
private String readFileContent(String path) throws IOException {
|
||||
byte[] encoded = Files.readAllBytes(Paths.get(path));
|
||||
return new String(encoded);
|
||||
}
|
||||
|
||||
private void deleteFile(String path) {
|
||||
try {
|
||||
FileUtil.removeDir(new File(path));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy sample input file to input directory.
|
||||
*/
|
||||
private void copySampleFileToInputDirectory() {
|
||||
File sourceFile = new File(SAMPLE_INPUT_DIR + FILE_NAME);
|
||||
File destFile = new File(TEST_INPUT_DIR + FILE_NAME);
|
||||
|
||||
if (!sourceFile.exists()) {
|
||||
System.out.println("Sample input file not found at location = [" + SAMPLE_INPUT_DIR + FILE_NAME + "]. Please provide this file.");
|
||||
}
|
||||
|
||||
if (!destFile.exists()) {
|
||||
try {
|
||||
System.out.println("Creating input file = [" + TEST_INPUT_DIR + FILE_NAME + "]");
|
||||
|
||||
File destDir = new File(TEST_INPUT_DIR);
|
||||
if(!destDir.exists()) {
|
||||
destDir.mkdir();
|
||||
}
|
||||
destFile.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
FileChannel source = null;
|
||||
FileChannel destination = null;
|
||||
|
||||
try {
|
||||
source = new FileInputStream(sourceFile).getChannel();
|
||||
destination = new FileOutputStream(destFile).getChannel();
|
||||
if (destination != null && source != null) {
|
||||
destination.transferFrom(source, 0, source.size());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
source.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
destination.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue