Thursday, June 25, 2015

Dynamic routing in Camel

    Howdy! Long time no see. I have some time to add a pretty simplistic trick which is related to the problem I encountered with dynamic routing in Camel.
The idea was to route the message on the basis of its content. Like that:
from(protocol:origin).
to(protocol:destinationA)
or
to(protocol:destinationB);
Camel has a solution for that which should work out of the box:
from("protocol:origin").
dynamicRouter(method("myDynamicRouter", "determineDestination"));
It's nice, isn't it? Other thing is it does not work as supposed. We got to know about that after some time despite the fact we had integration tests in place. The destination will be determined correctly but once it is chosen the message will be forwarded there infinite number of times. From Camel's documentation:
 
Important: The expression will be invoked repeatedly until it returns null, 
so be sure it does that, otherwise it will be invoked endlessly.

The cure is pretty simple:
from("protocol:origin").
recipientList().method("myDynamicRouter", "determineDestination");
Why am I writing about that? Because some knowledge can be gained. The first thing is that sometimes the libraries do not work according to your assumptions. Even though something may be obvious it may be worth double checking. The second thing is related to tests. We had integration test which sent a message and then checked if the message was on the queue. Sure it was there - multiplied by thousands. It is always good to write as strict assertions as possible. Have fun!

No comments :

Post a Comment