In some scenarios, we would like to use a Message Router, but we need to have more flexibility and control over the routing rules that are being used. The drawback to a Message Router is that the rules are embedded statically and can’t be modified during execution. In situations where we need more control, we can utilize a Dynamic Router that allows the routing rules to be defined at startup time.
Beyond the standard input and output channels, the Dynamic Router will also use an additional control channel. At system start-up time, all of the possible recipients will transmit a message to the control channel that is connected to the Dynamic Router. This message is designed to signify their presence and define the conditions upon which they will accept a message.
The Dynamic Router will then persist these defined preferences for all of the recipients into a rules engine or rules base. When a message arrives, the Dynamic Router evaluates all rules and routes the message to the recipient whose rules are fulfilled.
The dynamic nature of this pattern will allow for a flexible, efficient, predictive routing model that eliminates the maintenance dependency of a message router.
Dynamic Router using XML DSL with Bean
<route> <from uri="direct:input"/> <dynamicRouter> <!-- use a method call on a bean as dynamic router --> <method beanType="com.bean.MyRoutingBean" method="findRoute"/> </dynamicRouter> </route>