Skip to content

Commit 371a44b

Browse files
author
Artem Pronchakov
committed
Add camel file transformation example
1 parent 6e364d6 commit 371a44b

7 files changed

Lines changed: 130 additions & 0 deletions

File tree

Camel/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<name>Camel</name>
1111

1212
<modules>
13+
<module>spring-file-multiplier</module>
1314
<module>spring-route</module>
1415
<module>dsl-route</module>
1516
<module>camel-activemq</module>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
20
2+
30
3+
40
4+
50
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>edu.javacourse</groupId>
7+
<artifactId>examples</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>../../</relativePath>
10+
</parent>
11+
12+
<groupId>edu.javacourse.camel</groupId>
13+
<artifactId>spring-file-multiplier</artifactId>
14+
<version>1.0-SNAPSHOT</version>
15+
<packaging>jar</packaging>
16+
17+
<name>Camel Spring file multiplier</name>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>com.sun.xml.bind</groupId>
22+
<artifactId>jaxb-impl</artifactId>
23+
<version>2.3.3</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.apache.camel</groupId>
27+
<artifactId>camel-core</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.apache.camel</groupId>
31+
<artifactId>camel-spring</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.slf4j</groupId>
35+
<artifactId>slf4j-api</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.slf4j</groupId>
39+
<artifactId>slf4j-simple</artifactId>
40+
</dependency>
41+
</dependencies>
42+
43+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package edu.javacourse.camel.simpleroute;
2+
3+
import org.apache.camel.spring.Main;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
/**
8+
* @author Artem Pronchakov <artem.pronchakov@calisto.email>
9+
*/
10+
public class CamelSpringRouteExample {
11+
12+
private static final Logger log = LoggerFactory.getLogger(CamelSpringRouteExample.class);
13+
14+
public static void main(String[] args) throws Exception {
15+
Main main = new Main();
16+
main.setApplicationContextUri("META-INF/spring/camel-context.xml");
17+
main.enableHangupSupport();
18+
main.run();
19+
}
20+
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package edu.javacourse.camel.simpleroute;
2+
3+
import org.apache.camel.Body;
4+
5+
public class Multiplier {
6+
public Integer multipleByTwo(@Body Integer input) {
7+
return input * 2;
8+
}
9+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package edu.javacourse.camel.simpleroute;
2+
3+
import org.apache.camel.Exchange;
4+
import org.apache.camel.processor.aggregate.AggregationStrategy;
5+
6+
public class SplitAggregator implements AggregationStrategy {
7+
@Override
8+
public Exchange aggregate(Exchange _old, Exchange _new) {
9+
if (_old == null) {
10+
Integer body = _new.getIn().getBody(Integer.class);
11+
String s = String.valueOf(body);
12+
_new.getIn().setBody(s);
13+
return _new;
14+
} else {
15+
Integer body = _new.getIn().getBody(Integer.class);
16+
String s = String.valueOf(body);
17+
18+
String oldBody = _old.getIn().getBody(String.class);
19+
_old.getIn().setBody(oldBody + "\n" + s);
20+
return _old;
21+
}
22+
}
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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="
5+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6+
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
7+
8+
<bean id="multiplier" class="edu.javacourse.camel.simpleroute.Multiplier"/>
9+
<bean id="aggregator" class="edu.javacourse.camel.simpleroute.SplitAggregator"/>
10+
11+
<camelContext xmlns="http://camel.apache.org/schema/spring">
12+
<route>
13+
<from uri="file:Camel/spring-file-multiplier/inbox?charset=utf-8&amp;noop=true"/>
14+
<log message="File body: ${body}"/>
15+
<split strategyRef="aggregator">
16+
<tokenize token="\n"/>
17+
<log message="Line before transformation: ${body}"/>
18+
<convertBodyTo type="java.lang.Integer"/>
19+
<transform>
20+
<method ref="multiplier"/>
21+
</transform>
22+
<log message="Line after transformation: ${body}"/>
23+
</split>
24+
<log message="After split aggregation: ${body}"/>
25+
<to uri="file:Camel/spring-file-multiplier/outbox?charset=utf-8"/>
26+
</route>
27+
</camelContext>
28+
29+
</beans>

0 commit comments

Comments
 (0)