Friday, October 2, 2009

Content Based Routing in JBossESB Just Got Easier

One of the main tasks that the JBossESB performs is that of routing messages to the correct services. (As I never tire of telling people, in the context of the ESB, everything is either a message or a service. ;-) The ESB supports multiple types of message routing, including routing based on the content of the message itself. This Content Based Routing (CBR) was originally implemented in the ESB by means of using JBoss Drools.[1]

JBoss Drools is a complete enterprise platform for rules-based application development, workflow, administration, and event processing. It also provides an integration with JBossESB to support content based routing. You define the content based routing algorithm in a set of rules.

But, Drools might be a larger tool than you may want to use for some routing tasks.

Two additional (and simpler) approaches for content based routing were just added to the JBossESB project. (Note that these were added to the JBoss ESB project in trunk here: and should be in the next project release.) Let's take a look at these new approaches for content based routing as illustrated in the "jbos-esb.xml" file of the "fun_cbr" quickstart sample application:

XPath Content Based Routing

To configure XPath content based routing you define a service like this:
32  <service category="Fun_CBRServices_ESB" name="XPath_FunCBRService_ESB" description="ESB Listener - for the native clients" invmScope="GLOBAL">
33 <listeners>
34 <!-- Gateway -->
35 <jms-listener name="TheGateway" busidref="xpathQuickstartGwChannel" is-gateway="true" />
36 </listeners>
37 <actions mep="OneWay">
38 <action class="org.jboss.soa.esb.actions.ContentBasedRouter" name="ContentBasedRouter">
39 <property name="cbrAlias" value="XPath"/>
40 <property name="destinations">
41 <namespace prefix="ord" uri="http://org.jboss.soa.esb/Order" />
42 <route-to service-category="BlueTeam" service-name="GoBlue" expression="/ord:Order[@statusCode='0']" />
43 <route-to service-category="RedTeam" service-name="GoRed" expression="/ord:Order[@statusCode='1']" />
44 <route-to service-category="GreenTeam" service-name="GoGreen" expression="/ord:Order[@statusCode='2']" />
45 </property>
46 </action>
47 </actions>
48 </service>
The "cbrAlias" property defined on line 39 indicates that one of the new approaches for content based routing is to be used. On line 41 the namespace is defined and lines 42-44 define the actual routes. Note that this is completely defined in the jboss-esb.xml file. No additional configuration files are needed.

Regex Content Based Routing
52  <service category="Fun_CBRServices_ESB" name="Regex_FunCBRService_ESB" description="ESB Listener - for the native clients" invmScope="GLOBAL">
53 <listeners>
54 <!-- Gateway -->
55 <jms-listener name="TheGateway" busidref="regexQuickstartGwChannel" is-gateway="true" />
56 </listeners>
57 <actions mep="OneWay">
58 <action class="org.jboss.soa.esb.actions.ContentBasedRouter" name="ContentBasedRouter">
59 <property name="cbrAlias" value="Regex"/>
60 <property name="ruleSet" value="/"/>
61 <property name="ruleReload" value="true"/>
62 <property name="destinations">
63 <route-to destination-name="blue" service-category="BlueTeam" service-name="GoBlue" />
64 <route-to destination-name="red" service-category="RedTeam" service-name="GoRed" />
65 <route-to destination-name="green" service-category="GreenTeam" service-name="GoGreen" />
66 </property>
67 </action>
68 </actions>
69 </service>
Again, Line 59 defines the cbrAlias property and lines 63-65 define the actual paths. On line 60, we have a reference to the external file that contains the XPath expressions that will govern the routing. That file looks like this:
1  blue=.* statusCode="0".*
2 red=.* statusCode="1".*
3 green=.* statusCode="2".*
It's important to note that any applications built with Drools-based content based routing will continue to function without needing any changes or migrations.

To sum it up, content based routing has always been a flexible way to route messages to services. With these changes to JBossESB, it's even easier to use.