Skip to content

Commit acdea70

Browse files
ante003pivovarit
authored andcommitted
Advanced Camel article (eugenp#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
1 parent 42067af commit acdea70

4 files changed

Lines changed: 157 additions & 42 deletions

File tree

‎spring-apache-camel/pom.xml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<url>http://maven.apache.org</url>
1010

1111
<properties>
12-
<env.camel.version>2.16.1</env.camel.version>
12+
<env.camel.version>2.18.1</env.camel.version>
1313
<env.spring.version>4.3.4.RELEASE</env.spring.version>
1414
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
1515
<java.version>1.8</java.version>
@@ -53,6 +53,12 @@
5353
<artifactId>slf4j-log4j12</artifactId>
5454
<version>1.7.21</version>
5555
</dependency>
56+
<dependency>
57+
<groupId>org.apache.camel</groupId>
58+
<artifactId>camel-spring-javaconfig</artifactId>
59+
<version>${env.camel.version}</version>
60+
</dependency>
61+
5662
</dependencies>
5763

5864
<build>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.camel.file.cfg;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import org.apache.camel.builder.RouteBuilder;
7+
import org.apache.camel.spring.javaconfig.CamelConfiguration;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
11+
import com.baeldung.camel.file.ContentBasedFileRouter;
12+
13+
@Configuration
14+
public class ContentBasedFileRouterConfig extends CamelConfiguration {
15+
16+
@Bean
17+
ContentBasedFileRouter getContentBasedFileRouter() {
18+
return new ContentBasedFileRouter();
19+
}
20+
21+
@Override
22+
public List<RouteBuilder> routes() {
23+
return Arrays.asList(getContentBasedFileRouter());
24+
}
25+
26+
}

‎spring-apache-camel/src/test/java/com/apache/camel/file/processor/ContentBasedFileRouterIntegrationTest.java‎

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,65 @@
77
import org.junit.Test;
88
import org.junit.runner.RunWith;
99
import org.junit.runners.JUnit4;
10+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
11+
import org.springframework.context.support.AbstractApplicationContext;
1012
import org.springframework.context.support.ClassPathXmlApplicationContext;
1113

14+
import com.baeldung.camel.file.cfg.ContentBasedFileRouterConfig;
15+
1216
@RunWith(JUnit4.class)
1317
public class ContentBasedFileRouterIntegrationTest {
1418

15-
private static final long DURATION_MILIS = 10000;
16-
private static final String SOURCE_FOLDER = "src/test/source-folder";
17-
private static final String DESTINATION_FOLDER_TXT = "src/test/destination-folder-txt";
18-
private static final String DESTINATION_FOLDER_OTHER = "src/test/destination-folder-other";
19-
20-
@Before
21-
public void setUp() throws Exception {
22-
File sourceFolder = new File(SOURCE_FOLDER);
23-
File destinationFolderTxt = new File(DESTINATION_FOLDER_TXT);
24-
File destinationFolderOther = new File(DESTINATION_FOLDER_OTHER);
25-
26-
cleanFolder(sourceFolder);
27-
cleanFolder(destinationFolderTxt);
28-
cleanFolder(destinationFolderOther);
29-
30-
sourceFolder.mkdirs();
31-
File file1 = new File(SOURCE_FOLDER + "/File1.txt");
32-
File file2 = new File(SOURCE_FOLDER + "/File2.csv");
33-
file1.createNewFile();
34-
file2.createNewFile();
35-
}
36-
37-
private void cleanFolder(File folder) {
38-
File[] files = folder.listFiles();
39-
if (files != null) {
40-
for (File file : files) {
41-
if (file.isFile()) {
42-
file.delete();
43-
}
44-
}
45-
}
46-
}
47-
48-
@Test
49-
@Ignore
50-
public void routeTest() throws InterruptedException {
51-
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-ContentBasedFileRouterTest.xml");
52-
Thread.sleep(DURATION_MILIS);
53-
applicationContext.close();
54-
55-
}
19+
private static final long DURATION_MILIS = 10000;
20+
private static final String SOURCE_FOLDER = "src/test/source-folder";
21+
private static final String DESTINATION_FOLDER_TXT = "src/test/destination-folder-txt";
22+
private static final String DESTINATION_FOLDER_OTHER = "src/test/destination-folder-other";
23+
24+
@Before
25+
public void setUp() throws Exception {
26+
File sourceFolder = new File(SOURCE_FOLDER);
27+
File destinationFolderTxt = new File(DESTINATION_FOLDER_TXT);
28+
File destinationFolderOther = new File(DESTINATION_FOLDER_OTHER);
29+
30+
cleanFolder(sourceFolder);
31+
cleanFolder(destinationFolderTxt);
32+
cleanFolder(destinationFolderOther);
33+
34+
sourceFolder.mkdirs();
35+
File file1 = new File(SOURCE_FOLDER + "/File1.txt");
36+
File file2 = new File(SOURCE_FOLDER + "/File2.csv");
37+
file1.createNewFile();
38+
file2.createNewFile();
39+
}
40+
41+
private void cleanFolder(File folder) {
42+
File[] files = folder.listFiles();
43+
if (files != null) {
44+
for (File file : files) {
45+
if (file.isFile()) {
46+
file.delete();
47+
}
48+
}
49+
}
50+
}
51+
52+
@Test
53+
@Ignore
54+
public void routeWithXMLConfigTest() throws InterruptedException {
55+
AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(
56+
"camel-context-ContentBasedFileRouterTest.xml");
57+
Thread.sleep(DURATION_MILIS);
58+
applicationContext.close();
59+
60+
}
61+
62+
@Test
63+
@Ignore
64+
public void routeWithJavaConfigTest() throws InterruptedException {
65+
AbstractApplicationContext applicationContext = new AnnotationConfigApplicationContext(
66+
ContentBasedFileRouterConfig.class);
67+
Thread.sleep(DURATION_MILIS);
68+
applicationContext.close();
69+
70+
}
5671
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.apache.camel.file.processor;
2+
3+
import java.io.File;
4+
5+
import org.apache.camel.CamelContext;
6+
import org.apache.camel.builder.RouteBuilder;
7+
import org.apache.camel.impl.DefaultCamelContext;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.springframework.context.support.ClassPathXmlApplicationContext;
11+
12+
import com.baeldung.camel.file.FileProcessor;
13+
14+
15+
public class FileProcessorTest {
16+
17+
private static final long DURATION_MILIS = 10000;
18+
private static final String SOURCE_FOLDER = "src/test/source-folder";
19+
private static final String DESTINATION_FOLDER = "src/test/destination-folder";
20+
21+
@Before
22+
public void setUp() throws Exception {
23+
File sourceFolder = new File(SOURCE_FOLDER);
24+
File destinationFolder = new File(DESTINATION_FOLDER);
25+
26+
cleanFolder(sourceFolder);
27+
cleanFolder(destinationFolder);
28+
29+
sourceFolder.mkdirs();
30+
File file1 = new File(SOURCE_FOLDER + "/File1.txt");
31+
File file2 = new File(SOURCE_FOLDER + "/File2.txt");
32+
file1.createNewFile();
33+
file2.createNewFile();
34+
}
35+
36+
private void cleanFolder(File folder) {
37+
File[] files = folder.listFiles();
38+
if (files != null) {
39+
for (File file : files) {
40+
if (file.isFile()) {
41+
file.delete();
42+
}
43+
}
44+
}
45+
}
46+
47+
@Test
48+
public void moveFolderContentJavaDSLTest() throws Exception {
49+
final CamelContext camelContext = new DefaultCamelContext();
50+
camelContext.addRoutes(new RouteBuilder() {
51+
@Override
52+
public void configure() throws Exception {
53+
from("file://" + SOURCE_FOLDER + "?delete=true").process(new FileProcessor()).to("file://" + DESTINATION_FOLDER);
54+
}
55+
});
56+
camelContext.start();
57+
Thread.sleep(DURATION_MILIS);
58+
camelContext.stop();
59+
}
60+
61+
@Test
62+
public void moveFolderContentSpringDSLTest() throws InterruptedException {
63+
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-test.xml");
64+
Thread.sleep(DURATION_MILIS);
65+
applicationContext.close();
66+
67+
}
68+
}

0 commit comments

Comments
 (0)