Dynamic Router
The Dynamic Router from the EIP patterns allows you to route messages while avoiding the dependency of the router on all possible destinations while maintaining its efficiency.
In Camel 2.5 we introduced a dynamicRouter
in the DSL which is essentially a dynamic Routing Slip which evaluates the slip on-the-fly.
Dynamic Router in Camel 2.5 onwards
In Java DSL you can use the routingSlip
as shown below:
TODO: e1
Which will leverage a Bean to compute the slip on-the-fly, which could be implemented as follows:
TODO: e2
The same example in Spring XML would be:
You can also use the @DynamicRouter
annotation, for example the Camel 2.4 example below could be written as follows. The route
method would then be invoked repeatedly as the message is processed dynamically. The idea is to return the next endpoint uri where to go. Return null
to indicate the end. You can return multiple endpoints if you like, just as the Routing Slip, where each endpoint is separated by a delimiter.
public class MyDynamicRouter { @Consume(uri = "activemq:foo") @DynamicRouter public String route(@XPath("/customer/id") String customerId, @Header("Location") String location, Document body) { // query a database to find the best match of the endpoint based on the input parameteres // return the next endpoint uri, where to go. Return null to indicate the end. } }
Dynamic Router in Camel 2.4 or older
The simplest way to implement this is to use the RecipientList Annotation on a Bean method to determine where to route the message.
public class MyDynamicRouter { @Consume(uri = "activemq:foo") @RecipientList public List<String> route(@XPath("/customer/id") String customerId, @Header("Location") String location, Document body) { // query a database to find the best match of the endpoint based on the input parameteres ... } }
In the above we can use the Parameter Binding Annotations to bind different parts of the Message to method parameters or use an Expression such as using XPath or XQuery.
The method can be invoked in a number of ways as described in the Bean Integration such as
- POJO Producing
- Spring Remoting
- Bean component
Using This Pattern
If you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.