Apache Camel — What is a Dynamic Router EIP? (5/250–2016)

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.

Dynamic router is similar to Routing Slip — the difference being
1. Routing Slip — the decision is made before hand
2. Dynamic Router (Dynamic Routing Slip) — the decision is based on the fly — Provide logic — using Java code/query Database /rules Engine — to route msg to particular destination

Example:

Spring DSL

<bean id=”myBean” class=”xyz.DynamicRouterAnnotationBean”/>
<camelContext xmlns=”http://camel.apache.org/schema/spring">
 <route>
 <from uri=”direct:start”/>
 <bean ref=”myBean” method=”route”/>
 <to uri=”mock:result”/>
 </route>
<route>
 <from uri=”direct:a”/>
 <log message=”in direct:a”/>
 </route>
 <route>
 <from uri=”direct:b”/>
 <log message=”in direct:b”/>
 </route>
 <route>
 <from uri=”direct:c”/>
 <log message=”in direct:c”/>
 </route>
 <route>
 <from uri=”direct:d”/>
 <log message=”in direct:c”/>
 </route>
</camelContext>

Java Bean

public class DynamicRouterAnnotationBean {
 @DynamicRouter
 public String route(String body, @Header(Exchange.SLIP_ENDPOINT) String previous) {
 System.out.println(“**************route, body-”+body+”, previous-”+previous);
 if (previous == null) {
 // 1st time
 return “
direct://a”;
 } else if (“direct://a”.equals(previous)) {
 // 2nd time -
 return “
direct://b”;
 } else if (“direct://b”.equals(previous)) {
 // 2nd time -
 return “
direct://d,direct://c”;
 } else if (“direct://c”.equals(previous)) {
 // 3rd time — transform the message body using the simple language
 return “
direct://d”;
 } else if (“direct://d”.equals(previous)) {
 // 3rd time — transform the message body using the simple language
 return “language://simple:Bye ${body}”;
 } else {
 // no more, so return null to indicate end of dynamic router
 return null;
 }
 }
}
  • From the camel route “direct:start”, we call the method “route” in the bean DynamicRouterAnnotationBean.
  • The method “route” has an annotation @DynamicRouter which tells camel that this is not a regular java bean, but follows EIPs
  • the route method will then be invoked repeatedly until it returns “null”
  • you can return multiple endpoints using delimeter

Output

**************route, body-Camel, previous-null
2016–01–27 11:17:14,910 [ main] INFO route2 — in direct:a
**************route, body-Camel, previous-direct://a
2016–01–27 11:17:14,914 [ main] INFO route3 — in direct:b
**************route, body-Camel, previous-direct://b
2016–01–27 11:17:14,925 [ main] INFO route5 — in direct:d
2016–01–27 11:17:14,928 [ main] INFO route4 — in direct:c
**************route, body-Camel, previous-direct://c
2016–01–27 11:17:14,930 [ main] INFO route5 — in direct:d
**************route, body-Camel, previous-direct://d
**************route, body-Bye Camel, previous-language://simple:Bye%20$%7Bbody%7D

References:
http://camel.apache.org/dynamic-router.html
https://github.com/camelinaction/camelinaction