Using the Content based Router pattern
Using the Content based Router pattern
The Content-Based Router pattern from the EIP patterns examines the message content and routes to the correct destination based on the contents of the message. This use case uses the Servicemix Camel component to demonstrate the EIP.

It demonstrates how to
- Create the camel-context.xml file.
- Define the RouterBuilder class
The cbrService is defined to represent the Content Based Router Service. It will route the exchange to specific external Web Service's based on the ItemID of the product chosen.
In the Logisticx Demo this use case is defined in the "order-route-sa" assembly and the source code is within the "order-route" module of the Logsticx directory.
Walkthrough
To create the Content based router pattern and deploy it to Fuse ESB you need to do the following:
1. Set up the order-route project
- Prerequisites
- Create a POM file for this project
2. Create a Servicemix Camel Service unit
3. Create an Order route Service Assembly.
4. Deploy it to FUSE ESB
1. Setting up the project
Pre-requisites
To create and run the use case you need to download and install the following:
- FUSE ESB 3.3.0.6 or higher
- JDK 1.5 or higher
- Apache Maven 2.0.6 or higher
- The Logisticx demo source code
After installing Maven, you need to change the following settings in your operating system environment:
- Set the M2_HOME environment variable to point at the Maven root directory.
- Add the Maven bin directory (%M2_HOME%\bin on Windows or $M2_HOME/bin on UNIX) to your PATH.
Creating a Maven Project
All our use cases are Maven based around the Logisticx demo. This use case is a sub-project of the Logisticx demo.
So the first thing to do is create a directory called "
LOGISTICX_DIR/order-route" and place the following POM file within that directory.
Example 1.0: POM for Order Route
<?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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.iona.fuse.demo.logisticx</groupId> <artifactId>logisticx-demo-parent</artifactId> <version>1.1-SNAPSHOT</version> </parent> <groupId>com.iona.fuse.demo.logisticx.order.route</groupId> <artifactId>order-route</artifactId> <version>${logisticx-version}</version> <name>Order Routing Parent Project</name> <packaging>pom</packaging> </project>
We have given this project a groupId of com.iona.fuse.demo.logisticx.order.route and an artifact of order-route.
Run the following command to ensure everything builds ok.
=> mvn install
Also add the directory name as a module to the root Logisticx POM file.
Example 1.1: Module definition
<modules> <module>model</module> <module>derby</module> <module>order-route</module> .... </modules>
We will now create each of our service units and assembly from here.
2. Creating a Servicemix Camel Service unit
As we plan to deploy our Content Based Router pattern to FUSE ESB, we need to create a service unit to contain the service.
We will use a Maven archetype to create a project skeleton that will house the service. The resulting project will have a directory structure with
- the correct <dependency/> already in place for the servicemix-camel component
- a camel-context.xml file
- an example of a RouteBuilder class
- and a POM file.
Run the Maven archetype command from the "order-route" directory to create the servicemix-camel-service-unit and it will add the order-camel-cbr-su to your project.
DEMO_LOCATION\order-route> mvn archetype:create -DarchetypeGroupId=org.apache.servicemix.tooling -DarchetypeArtifactId=servicemix-camel-service-unit -DarchetypeVersion=3.3.1.5-fuse -DgroupId=com.iona.fuse.demo.logisticx.order.route -DartifactId=order-camel-cbr-su
Afterwards, add the new service unit to the service assembly by adding the necessary <dependency/> element to the SA's pom.xml.
Ensure the following dependencies are defined in the POM File
Example 1.2: Order Camel CBR POM File
.... <dependencies> <dependency> <groupId>org.apache.servicemix</groupId> <artifactId>servicemix-camel</artifactId> <version>${servicemix-version}</version> </dependency> <dependency> <groupId>org.apache.servicemix</groupId> <artifactId>servicemix-core</artifactId> <version>${servicemix-version}</version> </dependency> </dependencies> <properties> <componentName>servicemix-camel</componentName> </properties> ....
Updating the Camel-Context.xml file.
What this does is boot up the Spring ApplicationContext defined in the file /resources/camel-context.xml on the classpath. This is a regular Spring XML document that uses the Camel XML configuration to configure a CamelContext.
Change the contents of the generated file to
Example 1.3: Camel-context.xml file
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd"> <camelContext xmlns="http://activemq.apache.org/camel/schema/spring" xmlns:logisticx="http://logisticx.demo.fuse.iona.com/" xmlns:ns1="http://logisticx.demo.fuse.iona.com/warehouseService/"> <route> <from uri="jbi:endpoint:http://logisticx.demo.fuse.iona.com//cbrService/cbrEndpoint"/> <choice> <when> <xpath>/ns1:inStock/arg0/lineItem/itemId/text() = '1002'</xpath> <to uri="jbi:endpoint:http://logisticx.demo.fuse.iona.com/warehouseService//WarehouseService/WarehouseEndpoint?mep=in-out,operation=http://logisticx.demo.fuse.iona.com/warehouseService/:instock"/> </when> <when> <xpath>/ns1:inStock/arg0/lineItem/itemId/text() = '1004'</xpath> <to uri="jbi:endpoint:http://logisticx.demo.fuse.iona.com/warehouseService//WarehouseServiceSoapJMS/WarehouseEndpointSoapJMS?mep=in-out,operation=http://logisticx.demo.fuse.iona.com/warehouseService/:instock"/> </when> <otherwise> <to uri="jbi:endpoint:http://logisticx.demo.fuse.iona.com/warehouseService//WarehouseService/WarehouseEndpoint?mep=in-out,operation=http://logisticx.demo.fuse.iona.com/warehouseService/:instock"/> </otherwise> </choice> </route> </camelContext> </beans>
Here the cbrService will route the exchange to the Warehouse Web Service if the Id is 1002 and to the Warehouse Soap/Jms Web Service if the Id is 1004.
(For now the default is also the Warehouse Web Service until we implement another service).
Define the RouterBuilder class
Change the contents of the generated RouterBuilder to
Example 1.4: Route Builder Class
package com.iona.fuse.demo.logisticx; import org.apache.camel.builder.RouteBuilder; /** * A Camel Router * */ public class MyRouteBuilder extends RouteBuilder { public void configure() throws Exception { } }
The configuration was defined in the camel-context.xml file so there is no need to define it here.
3. Creating an Order route Service Assembly
You will now create a Servicemix Service Assembly to wrap the service units together and deploy it to FUSE ESB. Use the archetype:create command again.
mvn archetype:create \-DarchetypeGroupId=org.apache.servicemix.tooling \-DarchetypeArtifactId=servicemix-service-assembly \-DarchetypeVersion=3.3.1.5-fuse \-DgroupId=com.iona.fuse.demo.logisticx.order.route\ \-DartifactId=order-route-sa \-DremoteRepositories=[http://repo.open.iona.com/maven2/]
This generates a project called order-route-sa with a pom.xml file. Update the pom.xml file and add the following dependency in for the cbr service unit.
Example 1.5: Order SA Dependency
....
<dependency>
<groupId>com.iona.fuse.demo.logisticx.order.route</groupId>
<artifactId>order-camel-cbr-su</artifactId>
<version>${logisticx-version}</version>
</dependency>
....
4. Deploying to Fuse ESB
You are now ready to deploy your service assembly to FUSE ESB.
Deploying to Servicemix 3
Before you start, make sure that you have started ServiceMix.
You can navigate to your project's SA directory and use the JBI Maven plugin to deploy your project.
Run mvn jbi:projectDeploy
The command will not only deploy the service assembly, but also install any dependencies automatically. For this use case it will deploy the servicemix-camel component.
You may also copy the SA to the hotdeploy folder.
Deploying to Servicemix 4
You are now ready to deploy your service assembly to FUSE ESB.
Copy the built artifact to the "deploy" directory of FUSE ESB 4.
Start Servicemix - you can check if the service has started by running the "jbi list" comand.
