Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the asciidoc in the repository: https://github.com/apache/camel/blob/master/README.md https://github.com/apache/camel/blob/master/components/readme.adoc
Java DSLApache Camel offers a Java based DSL using the fluent builder style. The Java DSL is available by extending the RouteBuilder class, and implement the This is best illustrate by an example. In the code below we create a new class called
import org.apache.camel.builder.RouteBuilder;
/**
* A Camel Java DSL Router
*/
public class MyRouteBuilder extends RouteBuilder {
/**
* Let's configure the Camel routing rules using Java code...
*/
public void configure() {
// here is a sample which processes the input files
// (leaving them in place - see the 'noop' flag)
// then performs content based routing on the message using XPath
from("file:src/data?noop=true")
.choice()
.when(xpath("/person/city = 'London'"))
.to("file:target/messages/uk")
.otherwise()
.to("file:target/messages/others");
}
}
In the
from("file:src/data?noop=true")
Then we use the Content Based Router (eg the choice) to route the message depending if the person is from London or not.
.choice()
.when(xpath("/person/city = 'London'"))
.to("file:target/messages/uk")
.otherwise()
.to("file:target/messages/others");
RoutesCamel supports the definition of routing rules using a Java DSL (domain specific language) which avoids the need for cumbersome XML using a RouteBuilder. For example a simple route can be created as follows. Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
RouteBuilder builder = new RouteBuilder() {
public void configure() {
errorHandler(deadLetterChannel("mock:error"));
from("direct:a").to("direct:b");
}
};
As you can see from the above Camel uses URIs to wire endpoints together. URI String formattingAvailable as of Camel 2.0 If you have endpoint URIs that accept options and you want to be able to substitute the value, e.g. build the URI by concat the strings together, then you can use the
from("direct:start").toF("file://%s?fileName=%s", path, name);
fromF("file://%s?include=%s", path, pattern).toF("mock:%s", result);
FiltersYou can combine simple routes with filters which can be arbitrary Predicate implementations. Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
RouteBuilder builder = new RouteBuilder() {
public void configure() {
errorHandler(deadLetterChannel("mock:error"));
from("direct:a")
.filter(header("foo").isEqualTo("bar"))
.to("direct:b");
}
};
ChoicesWith a choice you provide a list of predicates and outcomes along with an optional default otherwise clause which is invoked if none of the conditions are met. Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
RouteBuilder builder = new RouteBuilder() {
public void configure() {
errorHandler(deadLetterChannel("mock:error"));
from("direct:a")
.choice()
.when(header("foo").isEqualTo("bar"))
.to("direct:b")
.when(header("foo").isEqualTo("cheese"))
.to("direct:c")
.otherwise()
.to("direct:d");
}
};
Using a custom processorHere is an example of using a custom Processor Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
myProcessor = new Processor() {
public void process(Exchange exchange) {
log.debug("Called with exchange: " + exchange);
}
};
RouteBuilder builder = new RouteBuilder() {
public void configure() {
errorHandler(deadLetterChannel("mock:error"));
from("direct:a")
.process(myProcessor);
}
};
You can mix and match custom processors with filters and choices. Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
RouteBuilder builder = new RouteBuilder() {
public void configure() {
errorHandler(deadLetterChannel("mock:error"));
from("direct:a")
.filter(header("foo").isEqualTo("bar"))
.process(myProcessor);
}
};
See Also |
