In this tutorial you will:
add a content based-router to a route
configure a content-based router
rewire a route to use the content-based router
To add a content-based router to your route:
Open the
camelContext.xmlfile in thesrc/main/resources/META-INF/springfolder of your simple-route project.Drag a Choice element (
) from the Routing section of the pallet
onto the route1 section of the editor canvas.The choice node will show up on the editor canvas.
With the choice node selected, open the context menu.
Select |.
A when node (
), with a connection to the choice node, appears on the canvas and the
property editor is configured to edit the when node as shown in
Figure 3.1.Enter
/person/city = 'London'into the property editor's Expression field.This XPath expression is used to determine which messages will follow this path in the route.
Select xpath from the Language drop down list.
Select the choice node on the canvas.
Select | from the context menu.
An otherwise node (
), with a connection to the choice node, appears on the canvas. The
otherwise node is used to route any messages that are not matched by one of the when
nodes.Click on the first endpoint in the route and drag a connector to the choice node.
A connector will appear connecting the endpoint with the choice.
Click on the otherwise node and drag a connector the terminal endpoint in the route.
A connector will appear connecting the otherwise node with the endpoint.
Select the connector between the two endpoints.
Select | to delete the connector.
![[Tip]](imagesdb/tip.gif)
Tip The connector's context menu includes a item.
Drag an Endpoint element (
) from the pallet into the route square.Enter
file:target/messages/ukin the Uri field.Click on the when node and drag a connector the new endpoint.
The route on the canvas should resemble Figure 3.2.
Select | to save the completed route.
Click the Source tab on the bottom of the editor canvas.
You will see the XML for the route. The
camelContextelement will look like Example 3.1.Example 3.1. XML for content-based router
<camelContext xmlns="http://camel.apache.org/schema/spring"> <route id="route1"> <from uri="file:src/data?noop=true"/> <choice> <when id="when1"> <language language="xpath">/person/city = 'London'</language> <to uri="file:target/messages/uk"/> </when> <otherwise id="otherwise1"> <to uri="file:target/messages"/> </otherwise> </choice> </route> </camelContext>
You can run the new route as described in Running the route.
To learn more about message enrichment see:
When in Enterprise Integration Pattern Reference









