You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by davelund <da...@gmail.com> on 2013/03/26 14:13:16 UTC

Clearing ThreadLocal when exchange completes

We are currently using ThreadLocal to store some information that gets
included in a log4j converter (and is used in other projects that dont have
camel in). Is there a callback I can use that for all routes in my context
so that at the end of the route it will get called (without explicitly
setting it) For example:
from("jms:somequeue")
  .processRef("someRandomProcessor")
  .processRef("someProcessorThatSetsThreadLocalValue")
  ....
  .to("jms:someotherqueue")


Ideally I'd like the threadlocal cleared when the exchange has been written
to the queue, without adding a processor that explicitly clears it (as
developers when adding new routes will forget to do it). I know the
onCompletion runs in a seperate thread so isn't fit for my purpose. I've
noticed  syncronisations and unitofworks, but cant seem to find any
documentation on it. Would this be fit for purpose?



--
View this message in context: http://camel.465427.n5.nabble.com/Clearing-ThreadLocal-when-exchange-completes-tp5729849.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Clearing ThreadLocal when exchange completes

Posted by Chris Wolf <cw...@gmail.com>.
If you're willing to implement a custom policy, you could implement
onEchangeDone()

http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/spi/RoutePolicy.html#onExchangeDone%28org.apache.camel.Route,%20org.apache.camel.Exchange%29

You could subclass RoutePolicySupport and override onExchangeDone,
which is a no-op there.

The route would look like:

 from("jms:somequeue")
.routeId("my.route").routePolicyRef("cleanupPolicy")
.processRef("someRandomProcessor")
.processRef("someProcessorThatSetsThreadLocalValue")   ....
.to("jms:someotherqueue")

N.B. the route must have an explicit ID for the policy to find it.

On Tue, Mar 26, 2013 at 9:13 AM, davelund <da...@gmail.com> wrote:
> We are currently using ThreadLocal to store some information that gets
> included in a log4j converter (and is used in other projects that dont have
> camel in). Is there a callback I can use that for all routes in my context
> so that at the end of the route it will get called (without explicitly
> setting it) For example:
> from("jms:somequeue")
>   .processRef("someRandomProcessor")
>   .processRef("someProcessorThatSetsThreadLocalValue")
>   ....
>   .to("jms:someotherqueue")
>
>
> Ideally I'd like the threadlocal cleared when the exchange has been written
> to the queue, without adding a processor that explicitly clears it (as
> developers when adding new routes will forget to do it). I know the
> onCompletion runs in a seperate thread so isn't fit for my purpose. I've
> noticed  syncronisations and unitofworks, but cant seem to find any
> documentation on it. Would this be fit for purpose?
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Clearing-ThreadLocal-when-exchange-completes-tp5729849.html
> Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Clearing ThreadLocal when exchange completes

Posted by Raul Kripalani <ra...@evosent.com>.
Hi David,

The cleanest and most modular way to achieve this is by using
EventNotifiers [1], as they execute synchronously. Enable only the
ExchangeCompletedEvent and add the bean to your registry. The CamelContext
will pick it up automatically.

Beware of the Asynchronous Routing Engine, as it can inadvertently change
the thread in which the response from an endpoint gets processed [2].

Let us know if this helped.

[1]
http://camel.apache.org/eventnotifier-to-log-details-about-all-sent-exchanges.html
[2] http://camel.apache.org/asynchronous-routing-engine.html

*Raúl Kripalani*
Enterprise Architect, Open Source Integration specialist, Program
Manager | Apache
Camel Committer
http://about.me/raulkripalani | http://www.linkedin.com/in/raulkripalani
http://blog.raulkr.net | twitter: @raulvk

On Tue, Mar 26, 2013 at 1:13 PM, davelund <da...@gmail.com> wrote:

> We are currently using ThreadLocal to store some information that gets
> included in a log4j converter (and is used in other projects that dont have
> camel in). Is there a callback I can use that for all routes in my context
> so that at the end of the route it will get called (without explicitly
> setting it) For example:
> from("jms:somequeue")
>   .processRef("someRandomProcessor")
>   .processRef("someProcessorThatSetsThreadLocalValue")
>   ....
>   .to("jms:someotherqueue")
>
>
> Ideally I'd like the threadlocal cleared when the exchange has been written
> to the queue, without adding a processor that explicitly clears it (as
> developers when adding new routes will forget to do it). I know the
> onCompletion runs in a seperate thread so isn't fit for my purpose. I've
> noticed  syncronisations and unitofworks, but cant seem to find any
> documentation on it. Would this be fit for purpose?
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Clearing-ThreadLocal-when-exchange-completes-tp5729849.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Clearing ThreadLocal when exchange completes

Posted by Raul Kripalani <ra...@evosent.com>.
But isn't it the same with the Synchronization approach? In all cases,
special caution is required when the Async Routing Engine kicks in, or when
threadpool-based EIPs are involved. I mentioned it in my first reply.

Regards,
Raúl.

On Thu, Mar 28, 2013 at 11:25 AM, Claus Ibsen <cl...@gmail.com> wrote:

> On Thu, Mar 28, 2013 at 9:36 AM, Raul Kripalani <ra...@evosent.com> wrote:
> > With this technique, developers  have to remember to add the custom
> > processor at the start of each route. (Unless it's set from an
> > interceptor...).
> >
> > Since the user explicitly asked for a non intrusive technique,
> > EventNotifier is the only method that fulfills that requirement. Set once
> > per Camel context and forget about it.
> >
> > And it's higher level, not fiddling with internal APIs.
> >
>
> That would only work if its the same thread that calls the listener at
> exchange created and exchange done events. As he need to use that same
> thread for clearing his thread locals.
>
>
>
> > Just my 2 cents.
> >
> > Regards,
> > Raúl.
> >  On 28 Mar 2013 06:11, "Claus Ibsen" <cl...@gmail.com> wrote:
> >
> >> On Wed, Mar 27, 2013 at 10:02 PM, Christian Müller
> >> <ch...@gmail.com> wrote:
> >> > Isn't it called in a separate thread, isn't it?
> >> >
> >>
> >> Ah yeah, then use this as base class
> >>
> >>
> http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/support/SynchronizationAdapter.html
> >>
> >> And return false from the allowHandover method
> >> Then its done on the same thread.
> >>
> >>
> >> > Sent from a mobile device
> >> > Am 27.03.2013 08:14 schrieb "Claus Ibsen" <cl...@gmail.com>:
> >> >
> >> >> You can use this method on the Exchange
> >> >>
> >> >>
> >> >>
> >>
> http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Exchange.html#addOnCompletion(org.apache.camel.spi.Synchronization)
> >> >>
> >> >>
> >> >> On Tue, Mar 26, 2013 at 2:13 PM, davelund <da...@gmail.com>
> >> wrote:
> >> >> > We are currently using ThreadLocal to store some information that
> gets
> >> >> > included in a log4j converter (and is used in other projects that
> dont
> >> >> have
> >> >> > camel in). Is there a callback I can use that for all routes in my
> >> >> context
> >> >> > so that at the end of the route it will get called (without
> explicitly
> >> >> > setting it) For example:
> >> >> > from("jms:somequeue")
> >> >> >   .processRef("someRandomProcessor")
> >> >> >   .processRef("someProcessorThatSetsThreadLocalValue")
> >> >> >   ....
> >> >> >   .to("jms:someotherqueue")
> >> >> >
> >> >> >
> >> >> > Ideally I'd like the threadlocal cleared when the exchange has been
> >> >> written
> >> >> > to the queue, without adding a processor that explicitly clears it
> (as
> >> >> > developers when adding new routes will forget to do it). I know the
> >> >> > onCompletion runs in a seperate thread so isn't fit for my purpose.
> >> I've
> >> >> > noticed  syncronisations and unitofworks, but cant seem to find any
> >> >> > documentation on it. Would this be fit for purpose?
> >> >> >
> >> >> >
> >> >> >
> >> >> > --
> >> >> > View this message in context:
> >> >>
> >>
> http://camel.465427.n5.nabble.com/Clearing-ThreadLocal-when-exchange-completes-tp5729849.html
> >> >> > Sent from the Camel - Users mailing list archive at Nabble.com.
> >> >>
> >> >>
> >> >>
> >> >> --
> >> >> Claus Ibsen
> >> >> -----------------
> >> >> Red Hat, Inc.
> >> >> FuseSource is now part of Red Hat
> >> >> Email: cibsen@redhat.com
> >> >> Web: http://fusesource.com
> >> >> Twitter: davsclaus
> >> >> Blog: http://davsclaus.com
> >> >> Author of Camel in Action: http://www.manning.com/ibsen
> >> >>
> >>
> >>
> >>
> >> --
> >> Claus Ibsen
> >> -----------------
> >> Red Hat, Inc.
> >> FuseSource is now part of Red Hat
> >> Email: cibsen@redhat.com
> >> Web: http://fusesource.com
> >> Twitter: davsclaus
> >> Blog: http://davsclaus.com
> >> Author of Camel in Action: http://www.manning.com/ibsen
> >>
>
>
>
> --
> Claus Ibsen
> -----------------
> Red Hat, Inc.
> FuseSource is now part of Red Hat
> Email: cibsen@redhat.com
> Web: http://fusesource.com
> Twitter: davsclaus
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen
>

Re: Clearing ThreadLocal when exchange completes

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Mar 28, 2013 at 9:36 AM, Raul Kripalani <ra...@evosent.com> wrote:
> With this technique, developers  have to remember to add the custom
> processor at the start of each route. (Unless it's set from an
> interceptor...).
>
> Since the user explicitly asked for a non intrusive technique,
> EventNotifier is the only method that fulfills that requirement. Set once
> per Camel context and forget about it.
>
> And it's higher level, not fiddling with internal APIs.
>

That would only work if its the same thread that calls the listener at
exchange created and exchange done events. As he need to use that same
thread for clearing his thread locals.



> Just my 2 cents.
>
> Regards,
> Raúl.
>  On 28 Mar 2013 06:11, "Claus Ibsen" <cl...@gmail.com> wrote:
>
>> On Wed, Mar 27, 2013 at 10:02 PM, Christian Müller
>> <ch...@gmail.com> wrote:
>> > Isn't it called in a separate thread, isn't it?
>> >
>>
>> Ah yeah, then use this as base class
>>
>> http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/support/SynchronizationAdapter.html
>>
>> And return false from the allowHandover method
>> Then its done on the same thread.
>>
>>
>> > Sent from a mobile device
>> > Am 27.03.2013 08:14 schrieb "Claus Ibsen" <cl...@gmail.com>:
>> >
>> >> You can use this method on the Exchange
>> >>
>> >>
>> >>
>> http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Exchange.html#addOnCompletion(org.apache.camel.spi.Synchronization)
>> >>
>> >>
>> >> On Tue, Mar 26, 2013 at 2:13 PM, davelund <da...@gmail.com>
>> wrote:
>> >> > We are currently using ThreadLocal to store some information that gets
>> >> > included in a log4j converter (and is used in other projects that dont
>> >> have
>> >> > camel in). Is there a callback I can use that for all routes in my
>> >> context
>> >> > so that at the end of the route it will get called (without explicitly
>> >> > setting it) For example:
>> >> > from("jms:somequeue")
>> >> >   .processRef("someRandomProcessor")
>> >> >   .processRef("someProcessorThatSetsThreadLocalValue")
>> >> >   ....
>> >> >   .to("jms:someotherqueue")
>> >> >
>> >> >
>> >> > Ideally I'd like the threadlocal cleared when the exchange has been
>> >> written
>> >> > to the queue, without adding a processor that explicitly clears it (as
>> >> > developers when adding new routes will forget to do it). I know the
>> >> > onCompletion runs in a seperate thread so isn't fit for my purpose.
>> I've
>> >> > noticed  syncronisations and unitofworks, but cant seem to find any
>> >> > documentation on it. Would this be fit for purpose?
>> >> >
>> >> >
>> >> >
>> >> > --
>> >> > View this message in context:
>> >>
>> http://camel.465427.n5.nabble.com/Clearing-ThreadLocal-when-exchange-completes-tp5729849.html
>> >> > Sent from the Camel - Users mailing list archive at Nabble.com.
>> >>
>> >>
>> >>
>> >> --
>> >> Claus Ibsen
>> >> -----------------
>> >> Red Hat, Inc.
>> >> FuseSource is now part of Red Hat
>> >> Email: cibsen@redhat.com
>> >> Web: http://fusesource.com
>> >> Twitter: davsclaus
>> >> Blog: http://davsclaus.com
>> >> Author of Camel in Action: http://www.manning.com/ibsen
>> >>
>>
>>
>>
>> --
>> Claus Ibsen
>> -----------------
>> Red Hat, Inc.
>> FuseSource is now part of Red Hat
>> Email: cibsen@redhat.com
>> Web: http://fusesource.com
>> Twitter: davsclaus
>> Blog: http://davsclaus.com
>> Author of Camel in Action: http://www.manning.com/ibsen
>>



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: Clearing ThreadLocal when exchange completes

Posted by Raul Kripalani <ra...@evosent.com>.
With this technique, developers  have to remember to add the custom
processor at the start of each route. (Unless it's set from an
interceptor...).

Since the user explicitly asked for a non intrusive technique,
EventNotifier is the only method that fulfills that requirement. Set once
per Camel context and forget about it.

And it's higher level, not fiddling with internal APIs.

Just my 2 cents.

Regards,
Raúl.
 On 28 Mar 2013 06:11, "Claus Ibsen" <cl...@gmail.com> wrote:

> On Wed, Mar 27, 2013 at 10:02 PM, Christian Müller
> <ch...@gmail.com> wrote:
> > Isn't it called in a separate thread, isn't it?
> >
>
> Ah yeah, then use this as base class
>
> http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/support/SynchronizationAdapter.html
>
> And return false from the allowHandover method
> Then its done on the same thread.
>
>
> > Sent from a mobile device
> > Am 27.03.2013 08:14 schrieb "Claus Ibsen" <cl...@gmail.com>:
> >
> >> You can use this method on the Exchange
> >>
> >>
> >>
> http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Exchange.html#addOnCompletion(org.apache.camel.spi.Synchronization)
> >>
> >>
> >> On Tue, Mar 26, 2013 at 2:13 PM, davelund <da...@gmail.com>
> wrote:
> >> > We are currently using ThreadLocal to store some information that gets
> >> > included in a log4j converter (and is used in other projects that dont
> >> have
> >> > camel in). Is there a callback I can use that for all routes in my
> >> context
> >> > so that at the end of the route it will get called (without explicitly
> >> > setting it) For example:
> >> > from("jms:somequeue")
> >> >   .processRef("someRandomProcessor")
> >> >   .processRef("someProcessorThatSetsThreadLocalValue")
> >> >   ....
> >> >   .to("jms:someotherqueue")
> >> >
> >> >
> >> > Ideally I'd like the threadlocal cleared when the exchange has been
> >> written
> >> > to the queue, without adding a processor that explicitly clears it (as
> >> > developers when adding new routes will forget to do it). I know the
> >> > onCompletion runs in a seperate thread so isn't fit for my purpose.
> I've
> >> > noticed  syncronisations and unitofworks, but cant seem to find any
> >> > documentation on it. Would this be fit for purpose?
> >> >
> >> >
> >> >
> >> > --
> >> > View this message in context:
> >>
> http://camel.465427.n5.nabble.com/Clearing-ThreadLocal-when-exchange-completes-tp5729849.html
> >> > Sent from the Camel - Users mailing list archive at Nabble.com.
> >>
> >>
> >>
> >> --
> >> Claus Ibsen
> >> -----------------
> >> Red Hat, Inc.
> >> FuseSource is now part of Red Hat
> >> Email: cibsen@redhat.com
> >> Web: http://fusesource.com
> >> Twitter: davsclaus
> >> Blog: http://davsclaus.com
> >> Author of Camel in Action: http://www.manning.com/ibsen
> >>
>
>
>
> --
> Claus Ibsen
> -----------------
> Red Hat, Inc.
> FuseSource is now part of Red Hat
> Email: cibsen@redhat.com
> Web: http://fusesource.com
> Twitter: davsclaus
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen
>

Re: Clearing ThreadLocal when exchange completes

Posted by Claus Ibsen <cl...@gmail.com>.
On Wed, Mar 27, 2013 at 10:02 PM, Christian Müller
<ch...@gmail.com> wrote:
> Isn't it called in a separate thread, isn't it?
>

Ah yeah, then use this as base class
http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/support/SynchronizationAdapter.html

And return false from the allowHandover method
Then its done on the same thread.


> Sent from a mobile device
> Am 27.03.2013 08:14 schrieb "Claus Ibsen" <cl...@gmail.com>:
>
>> You can use this method on the Exchange
>>
>>
>> http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Exchange.html#addOnCompletion(org.apache.camel.spi.Synchronization)
>>
>>
>> On Tue, Mar 26, 2013 at 2:13 PM, davelund <da...@gmail.com> wrote:
>> > We are currently using ThreadLocal to store some information that gets
>> > included in a log4j converter (and is used in other projects that dont
>> have
>> > camel in). Is there a callback I can use that for all routes in my
>> context
>> > so that at the end of the route it will get called (without explicitly
>> > setting it) For example:
>> > from("jms:somequeue")
>> >   .processRef("someRandomProcessor")
>> >   .processRef("someProcessorThatSetsThreadLocalValue")
>> >   ....
>> >   .to("jms:someotherqueue")
>> >
>> >
>> > Ideally I'd like the threadlocal cleared when the exchange has been
>> written
>> > to the queue, without adding a processor that explicitly clears it (as
>> > developers when adding new routes will forget to do it). I know the
>> > onCompletion runs in a seperate thread so isn't fit for my purpose. I've
>> > noticed  syncronisations and unitofworks, but cant seem to find any
>> > documentation on it. Would this be fit for purpose?
>> >
>> >
>> >
>> > --
>> > View this message in context:
>> http://camel.465427.n5.nabble.com/Clearing-ThreadLocal-when-exchange-completes-tp5729849.html
>> > Sent from the Camel - Users mailing list archive at Nabble.com.
>>
>>
>>
>> --
>> Claus Ibsen
>> -----------------
>> Red Hat, Inc.
>> FuseSource is now part of Red Hat
>> Email: cibsen@redhat.com
>> Web: http://fusesource.com
>> Twitter: davsclaus
>> Blog: http://davsclaus.com
>> Author of Camel in Action: http://www.manning.com/ibsen
>>



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: Clearing ThreadLocal when exchange completes

Posted by Christian Müller <ch...@gmail.com>.
Isn't it called in a separate thread, isn't it?

Sent from a mobile device
Am 27.03.2013 08:14 schrieb "Claus Ibsen" <cl...@gmail.com>:

> You can use this method on the Exchange
>
>
> http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Exchange.html#addOnCompletion(org.apache.camel.spi.Synchronization)
>
>
> On Tue, Mar 26, 2013 at 2:13 PM, davelund <da...@gmail.com> wrote:
> > We are currently using ThreadLocal to store some information that gets
> > included in a log4j converter (and is used in other projects that dont
> have
> > camel in). Is there a callback I can use that for all routes in my
> context
> > so that at the end of the route it will get called (without explicitly
> > setting it) For example:
> > from("jms:somequeue")
> >   .processRef("someRandomProcessor")
> >   .processRef("someProcessorThatSetsThreadLocalValue")
> >   ....
> >   .to("jms:someotherqueue")
> >
> >
> > Ideally I'd like the threadlocal cleared when the exchange has been
> written
> > to the queue, without adding a processor that explicitly clears it (as
> > developers when adding new routes will forget to do it). I know the
> > onCompletion runs in a seperate thread so isn't fit for my purpose. I've
> > noticed  syncronisations and unitofworks, but cant seem to find any
> > documentation on it. Would this be fit for purpose?
> >
> >
> >
> > --
> > View this message in context:
> http://camel.465427.n5.nabble.com/Clearing-ThreadLocal-when-exchange-completes-tp5729849.html
> > Sent from the Camel - Users mailing list archive at Nabble.com.
>
>
>
> --
> Claus Ibsen
> -----------------
> Red Hat, Inc.
> FuseSource is now part of Red Hat
> Email: cibsen@redhat.com
> Web: http://fusesource.com
> Twitter: davsclaus
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen
>

Re: Clearing ThreadLocal when exchange completes

Posted by Claus Ibsen <cl...@gmail.com>.
You can use this method on the Exchange

http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Exchange.html#addOnCompletion(org.apache.camel.spi.Synchronization)


On Tue, Mar 26, 2013 at 2:13 PM, davelund <da...@gmail.com> wrote:
> We are currently using ThreadLocal to store some information that gets
> included in a log4j converter (and is used in other projects that dont have
> camel in). Is there a callback I can use that for all routes in my context
> so that at the end of the route it will get called (without explicitly
> setting it) For example:
> from("jms:somequeue")
>   .processRef("someRandomProcessor")
>   .processRef("someProcessorThatSetsThreadLocalValue")
>   ....
>   .to("jms:someotherqueue")
>
>
> Ideally I'd like the threadlocal cleared when the exchange has been written
> to the queue, without adding a processor that explicitly clears it (as
> developers when adding new routes will forget to do it). I know the
> onCompletion runs in a seperate thread so isn't fit for my purpose. I've
> noticed  syncronisations and unitofworks, but cant seem to find any
> documentation on it. Would this be fit for purpose?
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Clearing-ThreadLocal-when-exchange-completes-tp5729849.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen