Advanced Camel article (#905)

* - changed test package name from org.baeldung to com.baeldung
- streams are added where neccessary
- format fixes

* Adding Java config for Content Based File Router

* Adding Java config for Content Based File Router
This commit is contained in:
Ante Pocedulic 2016-12-21 20:34:34 +01:00 committed by Grzegorz Piwowarek
parent 42067af3e7
commit acdea70d4c
4 changed files with 151 additions and 36 deletions

View File

@ -9,7 +9,7 @@
<url>http://maven.apache.org</url>
<properties>
<env.camel.version>2.16.1</env.camel.version>
<env.camel.version>2.18.1</env.camel.version>
<env.spring.version>4.3.4.RELEASE</env.spring.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<java.version>1.8</java.version>
@ -53,6 +53,12 @@
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-javaconfig</artifactId>
<version>${env.camel.version}</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,26 @@
package com.baeldung.camel.file.cfg;
import java.util.Arrays;
import java.util.List;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spring.javaconfig.CamelConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baeldung.camel.file.ContentBasedFileRouter;
@Configuration
public class ContentBasedFileRouterConfig extends CamelConfiguration {
@Bean
ContentBasedFileRouter getContentBasedFileRouter() {
return new ContentBasedFileRouter();
}
@Override
public List<RouteBuilder> routes() {
return Arrays.asList(getContentBasedFileRouter());
}
}

View File

@ -7,50 +7,65 @@ import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.baeldung.camel.file.cfg.ContentBasedFileRouterConfig;
@RunWith(JUnit4.class)
public class ContentBasedFileRouterIntegrationTest {
private static final long DURATION_MILIS = 10000;
private static final String SOURCE_FOLDER = "src/test/source-folder";
private static final String DESTINATION_FOLDER_TXT = "src/test/destination-folder-txt";
private static final String DESTINATION_FOLDER_OTHER = "src/test/destination-folder-other";
private static final long DURATION_MILIS = 10000;
private static final String SOURCE_FOLDER = "src/test/source-folder";
private static final String DESTINATION_FOLDER_TXT = "src/test/destination-folder-txt";
private static final String DESTINATION_FOLDER_OTHER = "src/test/destination-folder-other";
@Before
public void setUp() throws Exception {
File sourceFolder = new File(SOURCE_FOLDER);
File destinationFolderTxt = new File(DESTINATION_FOLDER_TXT);
File destinationFolderOther = new File(DESTINATION_FOLDER_OTHER);
@Before
public void setUp() throws Exception {
File sourceFolder = new File(SOURCE_FOLDER);
File destinationFolderTxt = new File(DESTINATION_FOLDER_TXT);
File destinationFolderOther = new File(DESTINATION_FOLDER_OTHER);
cleanFolder(sourceFolder);
cleanFolder(destinationFolderTxt);
cleanFolder(destinationFolderOther);
cleanFolder(sourceFolder);
cleanFolder(destinationFolderTxt);
cleanFolder(destinationFolderOther);
sourceFolder.mkdirs();
File file1 = new File(SOURCE_FOLDER + "/File1.txt");
File file2 = new File(SOURCE_FOLDER + "/File2.csv");
file1.createNewFile();
file2.createNewFile();
}
sourceFolder.mkdirs();
File file1 = new File(SOURCE_FOLDER + "/File1.txt");
File file2 = new File(SOURCE_FOLDER + "/File2.csv");
file1.createNewFile();
file2.createNewFile();
}
private void cleanFolder(File folder) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
file.delete();
}
}
}
}
private void cleanFolder(File folder) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
file.delete();
}
}
}
}
@Test
@Ignore
public void routeTest() throws InterruptedException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-ContentBasedFileRouterTest.xml");
Thread.sleep(DURATION_MILIS);
applicationContext.close();
@Test
@Ignore
public void routeWithXMLConfigTest() throws InterruptedException {
AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"camel-context-ContentBasedFileRouterTest.xml");
Thread.sleep(DURATION_MILIS);
applicationContext.close();
}
}
@Test
@Ignore
public void routeWithJavaConfigTest() throws InterruptedException {
AbstractApplicationContext applicationContext = new AnnotationConfigApplicationContext(
ContentBasedFileRouterConfig.class);
Thread.sleep(DURATION_MILIS);
applicationContext.close();
}
}

View File

@ -0,0 +1,68 @@
package com.apache.camel.file.processor;
import java.io.File;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.baeldung.camel.file.FileProcessor;
public class FileProcessorTest {
private static final long DURATION_MILIS = 10000;
private static final String SOURCE_FOLDER = "src/test/source-folder";
private static final String DESTINATION_FOLDER = "src/test/destination-folder";
@Before
public void setUp() throws Exception {
File sourceFolder = new File(SOURCE_FOLDER);
File destinationFolder = new File(DESTINATION_FOLDER);
cleanFolder(sourceFolder);
cleanFolder(destinationFolder);
sourceFolder.mkdirs();
File file1 = new File(SOURCE_FOLDER + "/File1.txt");
File file2 = new File(SOURCE_FOLDER + "/File2.txt");
file1.createNewFile();
file2.createNewFile();
}
private void cleanFolder(File folder) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
file.delete();
}
}
}
}
@Test
public void moveFolderContentJavaDSLTest() throws Exception {
final CamelContext camelContext = new DefaultCamelContext();
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("file://" + SOURCE_FOLDER + "?delete=true").process(new FileProcessor()).to("file://" + DESTINATION_FOLDER);
}
});
camelContext.start();
Thread.sleep(DURATION_MILIS);
camelContext.stop();
}
@Test
public void moveFolderContentSpringDSLTest() throws InterruptedException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-test.xml");
Thread.sleep(DURATION_MILIS);
applicationContext.close();
}
}