Skip to content

Instantly share code, notes, and snippets.

@MoriTanosuke
Last active February 2, 2017 12:29
Show Gist options
  • Save MoriTanosuke/3e0311a19eb7a44cae68a5b11ab679ed to your computer and use it in GitHub Desktop.
Save MoriTanosuke/3e0311a19eb7a44cae68a5b11ab679ed to your computer and use it in GitHub Desktop.
Simple 1-file example for Apache Camel and two transformers in one route
package de.kopis.examples;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CamelTransformerExample {
public static void main(String... args) throws Exception {
final Main main = new Main();
main.addRouteBuilder(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("file:input?noop=true")
// transform to REVERSE
.process(new Processor() {
private final Logger LOGGER = LoggerFactory.getLogger("Processor1");
public void process(final Exchange exchange) throws Exception {
LOGGER.info("Processing message: " + exchange.getIn().getBody(String.class));
Object body = exchange.getIn().getBody(String.class);
exchange.getOut().setBody(new StringBuilder((String) body).reverse());
}
})
// transform to UPPERCASE
.process(new Processor() {
private final Logger LOGGER = LoggerFactory.getLogger("Processor2");
public void process(final Exchange exchange) throws Exception {
LOGGER.info("Processing message: " + exchange.getIn().getBody(String.class));
Object body = exchange.getIn().getBody(String.class);
exchange.getOut().setBody(((String) body).toUpperCase());
}
})
.to("file:output");
}
});
main.run();
}
}
# Root logger option
log4j.rootLogger=DEBUG, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.kopis.examples</groupId>
<artifactId>camel-transformer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.18.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment