Skip to content

Commit d1ea5e8

Browse files
ante003pivovarit
authored andcommitted
- added new camel example (eugenp#812)
- added new camel context for junit test
1 parent f84ba52 commit d1ea5e8

5 files changed

Lines changed: 141 additions & 24 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.camel.file;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.Date;
5+
6+
import org.apache.camel.Exchange;
7+
import org.apache.camel.Processor;
8+
9+
public class FileProcessor implements Processor {
10+
11+
public void process(Exchange exchange) throws Exception {
12+
String originalFileName = (String) exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
13+
14+
Date date = new Date();
15+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
16+
String changedFileName = dateFormat.format(date) + originalFileName;
17+
exchange.getIn().setHeader(Exchange.FILE_NAME, changedFileName);
18+
}
19+
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.camel.file;
2+
3+
import org.apache.camel.builder.RouteBuilder;
4+
5+
public class FileRouter extends RouteBuilder {
6+
7+
private static final String SOURCE_FOLDER = "src/test/source-folder";
8+
private static final String DESTINATION_FOLDER = "src/test/destination-folder";
9+
10+
@Override
11+
public void configure() throws Exception {
12+
from("file://" + SOURCE_FOLDER + "?delete=true").process(new FileProcessor()).to("file://" + DESTINATION_FOLDER);
13+
}
14+
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
5+
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
6+
7+
<bean id="fileRouter" class="com.baeldung.camel.file.FileRouter" />
8+
<bean id="fileProcessor" class="com.baeldung.camel.file.FileProcessor" />
9+
10+
<camelContext xmlns="http://camel.apache.org/schema/spring">
11+
<routeBuilder ref="fileRouter" />
12+
</camelContext>
13+
14+
</beans>
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
55
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
66

7-
<camelContext xmlns="http://camel.apache.org/schema/spring">
7+
<camelContext xmlns="http://camel.apache.org/schema/spring">
88

9-
<route customId="true" id="route1">
9+
<route customId="true" id="route1">
1010

11-
<!-- Read files from input directory -->
12-
<from uri="file://src/test/data/input"/>
11+
<!-- Read files from input directory -->
12+
<from uri="file://src/test/data/input" />
1313

14-
<!-- Transform content to UpperCase -->
15-
<process ref="myFileProcessor"/>
14+
<!-- Transform content to UpperCase -->
15+
<process ref="myFileProcessor" />
1616

17-
<!-- Write converted file content -->
18-
<to uri="file://src/test/data/outputUpperCase"/>
17+
<!-- Write converted file content -->
18+
<to uri="file://src/test/data/outputUpperCase" />
1919

20-
<!-- Transform content to LowerCase -->
21-
<transform>
22-
<simple>${body.toLowerCase()}</simple>
23-
</transform>
20+
<!-- Transform content to LowerCase -->
21+
<transform>
22+
<simple>${body.toLowerCase()}</simple>
23+
</transform>
2424

25-
<!-- Write converted file content -->
26-
<to uri="file://src/test/data/outputLowerCase"/>
25+
<!-- Write converted file content -->
26+
<to uri="file://src/test/data/outputLowerCase" />
2727

28-
<!-- Display process completion message on console -->
29-
<transform>
30-
<simple>.......... File content conversion completed ..........</simple>
31-
</transform>
32-
<to uri="stream:out"/>
28+
<!-- Display process completion message on console -->
29+
<transform>
30+
<simple>.......... File content conversion completed ..........</simple>
31+
</transform>
32+
<to uri="stream:out" />
3333

34-
</route>
34+
</route>
3535

36-
</camelContext>
36+
</camelContext>
3737

38-
<bean id="myFileProcessor" class="com.baeldung.camel.processor.FileProcessor"/>
38+
<bean id="myFileProcessor" class="com.baeldung.camel.processor.FileProcessor" />
3939
</beans>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.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)