Content Based Routing on Camel

Content based routing (CBR) is considered as a necessary technology for service integration and has been widely used in current enterprise integration software. XML is a intermediary message type which provides the mediation between diverse data structures and formats as messages passing between applications or services. CBR can be thought as a service routing mechanism that determines a service route by analysing the message content at runtime. Camel support CBR using the Content Based Router and it defines a set of DSLs to simplify the usage of this patten .

In Load Balance for existing Messaging Service sample, we have used the Content Based Router. This sample will guide you to creating your processor, registering and using it in Web Console.

Build the sample

You can build a sample as that in Load Balance for existing Messaging Service.

Create a processor

Now we create a validating processor which will check whether the message has been expired. If expired, the processor will throw a validation exception. The processor can be defined as follows:

Register the processor

We can register the processor in the spring context in two steps.

  • Add a component-scan package in the applicationContext.xml. Spring will scan the package for service registry when starting.
  • Annotate the processor by adding one line code in the processor. Now the processor is as follows:

Now you can run the sample, but it won't do the validation because we haven't configured the route to pass through the processor. You can complete it by editing the applicationContext.xml before running it, but here we want to show you how to do it through Web Console in groovy language.

Use Content Based Router to orchestrate the processors

We should execute the validation before processing the message, so we edit the route1 as follows:

Now a expired date can be specified in the message, so the validating processor can filter the expired messages. You can add message files with content:

When the the message is out of date, a validation exception is thrown. To make it a little elegant, you may use doTry...doCatch...doFinally to wrap it up and specify a queue to store the invalid messages.

You can define custom exceptions before running, even in Web Console at runtime. However, We don't encourage to create new classes in Web Console since that involves non-treatable inner-classes for Groovy Renderer and you will find it hard to deal with through Web Console.

Simpify your test by using Web Console

To run a test, you can create a file holding the message above and throw it into the directory. However, Web Console can help to simplify your operations. Go to the endpoints page by directing your browser to: http://localhost:8080/endpoints and you can see the file://src/data?noop=true endpoint. Just open it and then select the Send to this endpoint link for inputing your message. Web Console will save your message as a file in the directory: src/data, and camel will process the message file then.

© 2004-2014 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram