In this stage, you examine the sample code in
.
This sample implements a typical EIP pattern, a content-based router, and illustrates how
easily — and concisely — you can solve integration problems using
Apache Camel.ProjectRoot/simple-router/src/main/java/tutorial/MyRouteBuilder.java
The following code shows how the sample route is implemented:
... package tutorial;import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.spring.Main; import static org.apache.camel.builder.xml.XPathBuilder.xpath; /** * A Camel Router * * @version $ */ public class MyRouteBuilder extends RouteBuilder { /** * A main() so we can easily run these routing rules in our IDE */ public static void main(String... args) throws Exception { Main.main(args);
} /** * Lets configure the Camel routing rules using Java code... */ public void configure() { // TODO create Camel routes here. // 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"); } }
This sample has several notable features. First, observe how the Java DSL uses a series of method calls to create an English-like expression. This technique makes the intent of the code clear: the sample reads input messages from a directory, applies an XPath predicate to each message's XML content, and, based on the result, chooses a different route for the output messages.
Note also the following:
Maven automatically creates a package name based on the value of the
| |
Apache Camel defines a convenient wrapper class for the Spring container. To instantiate
a Spring container instance, all that you need to do is write a short
| |
The | |
The |