You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by kaoru <ka...@hotmail.com> on 2010/12/14 19:05:03 UTC
dynamicRouter Thread safe issue
Hi guys,
from the dynamic-router webpage, there is an example:
/**
* Use this method to compute dynamic where we should route next.
*
* @param body the message body
* @return endpoints to go, or <tt>null</tt> to indicate the end
*/
public String slip(String body) {
bodies.add(body);
invoked++;
if (invoked == 1) {
return "mock:a";
} else if (invoked == 2) {
return "mock:b,mock:c";
} else if (invoked == 3) {
return "direct:foo";
} else if (invoked == 4) {
return "mock:result";
}
// no more so return null
return null;
}
But if there are multiple messages, that 'invoked' variable won't be thread
safe. How can we solve this?
Thanks
--
View this message in context: http://camel.465427.n5.nabble.com/dynamicRouter-Thread-safe-issue-tp3305007p3305007.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: dynamicRouter Thread safe issue
Posted by kaoru <ka...@hotmail.com>.
Cool. That's great.
Thank you for the reply.
--
View this message in context: http://camel.465427.n5.nabble.com/dynamicRouter-Thread-safe-issue-tp3305007p3306748.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: dynamicRouter Thread safe issue
Posted by Claus Ibsen <cl...@gmail.com>.
On Tue, Dec 14, 2010 at 7:05 PM, kaoru <ka...@hotmail.com> wrote:
>
> Hi guys,
>
> from the dynamic-router webpage, there is an example:
>
>
> /**
> * Use this method to compute dynamic where we should route next.
> *
> * @param body the message body
> * @return endpoints to go, or <tt>null</tt> to indicate the end
> */
> public String slip(String body) {
> bodies.add(body);
> invoked++;
>
> if (invoked == 1) {
> return "mock:a";
> } else if (invoked == 2) {
> return "mock:b,mock:c";
> } else if (invoked == 3) {
> return "direct:foo";
> } else if (invoked == 4) {
> return "mock:result";
> }
>
> // no more so return null
> return null;
> }
>
>
> But if there are multiple messages, that 'invoked' variable won't be thread
> safe. How can we solve this?
>
The Exchange contains a header with the last endpoint that was
returned/selected. So you can use that to know where you are.
Also you can store the state on the Exchange, which is per Exchange,
and thus thread safe.
But well spotted the example is not thread safe. We should add a note
about it, or improve it :)
>
>
> Thanks
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/dynamicRouter-Thread-safe-issue-tp3305007p3305007.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
--
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/