Clean up Apache Camel example
This commit is contained in:
parent
9d6f56c40a
commit
54a1235d17
|
@ -23,6 +23,6 @@ To build this application execute following maven command in ApacheCamelFileProc
|
||||||
|
|
||||||
<code>mvn clean install</code>
|
<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:
|
To run this application you can either run our main class App from your IDE or you can execute following maven command:
|
||||||
|
|
||||||
<code>mvn exec:java -Dexec.mainClass="org.apache.camel.main.App"</code>
|
<code>mvn exec:java -Dexec.mainClass="App"</code>
|
|
@ -1,12 +1,12 @@
|
||||||
package org.apache.camel.main;
|
package com.baeldung.camel.main;
|
||||||
|
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
public static void main(final String[] args) throws Exception {
|
public static void main(final String[] args) throws Exception {
|
||||||
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context.xml");
|
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context.xml");
|
||||||
// Keep main thread alive for some time to let application finish processing the input files.
|
// Keep main thread alive for some time to let application finish processing the input files.
|
||||||
Thread.sleep(5000);
|
Thread.sleep(5000);
|
||||||
applicationContext.close();
|
applicationContext.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,14 +0,0 @@
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,40 +1,39 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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
|
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">
|
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
|
||||||
|
|
||||||
<camelContext xmlns="http://camel.apache.org/schema/spring">
|
<camelContext xmlns="http://camel.apache.org/schema/spring">
|
||||||
|
|
||||||
<route customId="true" id="route1">
|
<route customId="true" id="route1">
|
||||||
|
|
||||||
<!-- Read files from input directory -->
|
<!-- Read files from input directory -->
|
||||||
<from uri="file://src/test/data/input" />
|
<from uri="file://src/test/data/input"/>
|
||||||
|
|
||||||
<!-- Transform content to UpperCase -->
|
<!-- Transform content to UpperCase -->
|
||||||
<process ref="myFileProcessor" />
|
<process ref="myFileProcessor"/>
|
||||||
|
|
||||||
<!-- Write converted file content -->
|
<!-- Write converted file content -->
|
||||||
<to uri="file://src/test/data/outputUpperCase" />
|
<to uri="file://src/test/data/outputUpperCase"/>
|
||||||
|
|
||||||
<!-- Transform content to LowerCase -->
|
<!-- Transform content to LowerCase -->
|
||||||
<transform>
|
<transform>
|
||||||
<simple>${body.toLowerCase()}</simple>
|
<simple>${body.toLowerCase()}</simple>
|
||||||
</transform>
|
</transform>
|
||||||
|
|
||||||
<!-- Write converted file content -->
|
<!-- Write converted file content -->
|
||||||
<to uri="file://src/test/data/outputLowerCase" />
|
<to uri="file://src/test/data/outputLowerCase"/>
|
||||||
|
|
||||||
<!-- Display process completion message on console -->
|
<!-- Display process completion message on console -->
|
||||||
<transform>
|
<transform>
|
||||||
<simple> .......... File content conversion completed ..........
|
<simple>.......... File content conversion completed ..........</simple>
|
||||||
</simple>
|
</transform>
|
||||||
</transform>
|
<to uri="stream:out"/>
|
||||||
<to uri="stream:out" />
|
|
||||||
|
|
||||||
</route>
|
</route>
|
||||||
|
|
||||||
</camelContext>
|
</camelContext>
|
||||||
|
|
||||||
<bean id="myFileProcessor" class="org.apache.camel.processor.FileProcessor" />
|
<bean id="myFileProcessor" class="com.baeldung.camel.processor.FileProcessor"/>
|
||||||
</beans>
|
</beans>
|
|
@ -1 +1 @@
|
||||||
THIS IS UPPERCASE CONTENT. this is lowercase content.
|
This is data that will be processed by a Camel route!
|
|
@ -1,5 +1,12 @@
|
||||||
package org.apache.camel.main;
|
package org.apache.camel.main;
|
||||||
|
|
||||||
|
import com.baeldung.camel.main.App;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import org.apache.camel.util.FileUtil;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
@ -8,110 +15,103 @@ import java.nio.channels.FileChannel;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
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 {
|
public class AppTest extends TestCase {
|
||||||
|
|
||||||
private static final String FILE_NAME = "file.txt";
|
private static final String FILE_NAME = "file.txt";
|
||||||
private static final String SAMPLE_INPUT_DIR = "src/test/data/sampleInputFile/";
|
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 TEST_INPUT_DIR = "src/test/data/input/";
|
||||||
private static final String UPPERCARE_OUTPUT_DIR = "src/test/data/outputUpperCase/";
|
private static final String UPPERCASE_OUTPUT_DIR = "src/test/data/outputUpperCase/";
|
||||||
private static final String LOWERCASE_OUTPUT_DIR = "src/test/data/outputLowerCase/";
|
private static final String LOWERCASE_OUTPUT_DIR = "src/test/data/outputLowerCase/";
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
// Prepare input file for test
|
// Prepare input file for test
|
||||||
copySampleFileToInputDirectory();
|
copySampleFileToInputDirectory();
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() throws Exception {
|
public void tearDown() throws Exception {
|
||||||
System.out.println("Deleting the test input and output files...");
|
System.out.println("Deleting the test input and output files...");
|
||||||
deleteFile(TEST_INPUT_DIR);
|
deleteFile(TEST_INPUT_DIR);
|
||||||
deleteFile(LOWERCASE_OUTPUT_DIR);
|
deleteFile(LOWERCASE_OUTPUT_DIR);
|
||||||
deleteFile(UPPERCARE_OUTPUT_DIR);
|
deleteFile(UPPERCASE_OUTPUT_DIR);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void testMain() throws Exception {
|
public final void testMain() throws Exception {
|
||||||
App.main(null);
|
App.main(null);
|
||||||
|
|
||||||
String inputFileContent = readFileContent(SAMPLE_INPUT_DIR+FILE_NAME);
|
String inputFileContent = readFileContent(SAMPLE_INPUT_DIR + FILE_NAME);
|
||||||
String outputUpperCase = readFileContent(UPPERCARE_OUTPUT_DIR+FILE_NAME);
|
String outputUpperCase = readFileContent(UPPERCASE_OUTPUT_DIR + FILE_NAME);
|
||||||
String outputLowerCase = readFileContent(LOWERCASE_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 {
|
System.out.println("Input File content = [" + inputFileContent + "]");
|
||||||
byte[] encoded = Files.readAllBytes(Paths.get(path));
|
System.out.println("UpperCaseOutput file content = [" + outputUpperCase + "]");
|
||||||
return new String(encoded);
|
System.out.println("LowerCaseOtput file content = [" + outputLowerCase + "]");
|
||||||
}
|
|
||||||
|
|
||||||
private void deleteFile(String path) {
|
assertEquals(inputFileContent.toUpperCase(), outputUpperCase);
|
||||||
try {
|
assertEquals(inputFileContent.toLowerCase(), outputLowerCase);
|
||||||
FileUtil.removeDir(new File(path));
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
private String readFileContent(String path) throws IOException {
|
||||||
* Copy sample input file to input directory.
|
byte[] encoded = Files.readAllBytes(Paths.get(path));
|
||||||
*/
|
return new String(encoded);
|
||||||
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()) {
|
private void deleteFile(String path) {
|
||||||
try {
|
try {
|
||||||
System.out.println("Creating input file = [" + TEST_INPUT_DIR + FILE_NAME + "]");
|
FileUtil.removeDir(new File(path));
|
||||||
|
} catch (Exception e) {
|
||||||
File destDir = new File(TEST_INPUT_DIR);
|
e.printStackTrace();
|
||||||
if(!destDir.exists()) {
|
}
|
||||||
destDir.mkdir();
|
}
|
||||||
}
|
|
||||||
destFile.createNewFile();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FileChannel source = null;
|
|
||||||
FileChannel destination = null;
|
|
||||||
|
|
||||||
try {
|
/**
|
||||||
source = new FileInputStream(sourceFile).getChannel();
|
* Copy sample input file to input directory.
|
||||||
destination = new FileOutputStream(destFile).getChannel();
|
*/
|
||||||
if (destination != null && source != null) {
|
private void copySampleFileToInputDirectory() {
|
||||||
destination.transferFrom(source, 0, source.size());
|
File sourceFile = new File(SAMPLE_INPUT_DIR + FILE_NAME);
|
||||||
}
|
File destFile = new File(TEST_INPUT_DIR + FILE_NAME);
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
if (!sourceFile.exists()) {
|
||||||
} finally {
|
System.out.println("Sample input file not found at location = [" + SAMPLE_INPUT_DIR + FILE_NAME + "]. Please provide this file.");
|
||||||
try {
|
}
|
||||||
source.close();
|
|
||||||
} catch (Exception e) {
|
if (!destFile.exists()) {
|
||||||
e.printStackTrace();
|
try {
|
||||||
}
|
System.out.println("Creating input file = [" + TEST_INPUT_DIR + FILE_NAME + "]");
|
||||||
try {
|
|
||||||
destination.close();
|
File destDir = new File(TEST_INPUT_DIR);
|
||||||
} catch (Exception e) {
|
if (!destDir.exists()) {
|
||||||
e.printStackTrace();
|
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