Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
Enterprise Integration Zone is brought to you in partnership with:

Len DiMaggio is a software QE engineer for JBoss at Red Hat. Len concentrates on open source Java middleware and is located in the USA. Len has been contributing to DZone since 2009. Len is a DZone MVB and is not an employee of DZone and has posted 14 posts at DZone. View Full User Profile

Content Based Routing in JBossESB Just Got Easier

11.30.2009
Email
Views: 3969
  • submit to reddit
The Enterprise Integration Zone is presented by DZone and FuseSource. Check out the EI Zone for real world integration scenarios to help you learn which technology will give you the most elegant solution.  For open source systems based on Apache Camel, ActiveMQ, or ServiceMix, look into FuseSource's training and technology.  

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: http://anonsvn.jboss.org/repos/labs/labs/jbossesb/trunk 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="/regex-rules.properties"/>
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.
References
Published at DZone with permission of Len DiMaggio, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Enterprise Integration is a huge problem space for developers, and with so many different technologies to choose from, finding the most elegant solution can be tricky.  The Enterprise Integration Zone is a place for enterprise developers of all backgrounds to share design patterns and technology solutions that make integration easier for various scenarios.  FuseSource proudly supports the EI Zone and provides its own gamut of training, services, and tools based on Apache Camel, ActiveMQ, CXF, and ServiceMix.