You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Jim Talbut <jt...@spudsoft.co.uk> on 2011/07/26 07:39:27 UTC

How to get Exchange from a within a POJO without changing the interface?

Hi,

I have a POJO that benefits from having a non-Camel interface (it means 
I can know that the interface matches that of a given web service).

Internally this POJO needs to extract a couple of properties from the 
Exchange.

Is there any way to get the "currently executing Exchange" without 
adding it as a parameter to the POJO?
Most useful for me would be either a static method or a method on the 
CamelContext, because they would allow me to move the Camel-specific 
processing into a separate bean.

Thanks

Jim

Re: How to get Exchange from a within a POJO without changing the interface?

Posted by Romain Manni-Bucau <rm...@gmail.com>.
I use the occasion to thank you Claus, this improvement is really useful :)

it will save a lot of wrapper files.

- Romain

2011/7/26 Claus Ibsen <cl...@gmail.com>

> On Tue, Jul 26, 2011 at 7:39 AM, Jim Talbut <jt...@spudsoft.co.uk>
> wrote:
> > Hi,
> >
> > I have a POJO that benefits from having a non-Camel interface (it means I
> > can know that the interface matches that of a given web service).
> >
> > Internally this POJO needs to extract a couple of properties from the
> > Exchange.
> >
> > Is there any way to get the "currently executing Exchange" without adding
> it
> > as a parameter to the POJO?
> > Most useful for me would be either a static method or a method on the
> > CamelContext, because they would allow me to move the Camel-specific
> > processing into a separate bean.
> >
>
> On trunk I have improved the bean component, so you can bind the
> parameters in the method name option.
> .to"(bean:myBean?method=myMethod(${body}, ${header.foo},
> ${header.bar}, 'This is a String'")
>
> More details at:
> http://camel.apache.org/bean-binding.html
>
>
>
> > Thanks
> >
> > Jim
> >
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: cibsen@fusesource.com
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/
>

Re: How to get Exchange from a within a POJO without changing the interface?

Posted by Jim Talbut <jt...@spudsoft.co.uk>.
Just submitted CAMEL-4274, with patch, to fix this behaviour.

Jim

On 26/07/2011 11:40, James Talbut wrote:
> A bit more playing and I've got my POJO working with correlation IDs from MDC.
>
> There is one oddity:
> After my ProducerTemplate returns the MDC map has been cleared - shouldn't it reflect the parent route as that Exchange is still being processed?
> In other words, shouldn't the MDC map members be a stack, rather than a simple variable?
>
> To make my code work I manually reset the MDC correlation ID after the producer template returns.
>
> Thanks.
>
> Jim
>
>
> On Tue, Jul 26, 2011 at 07:56:15AM +0100, Jim Talbut wrote:
>> Just read the 2.8.0 release notes, could I use MDC for this?
>> It's not currently enabled on my context, but I'll be using either Pax
>> or logback so I can enable it.
>>
>> If so, what excellent timing, thank you :)
>>
>> Jim
>>
>> On 26/07/2011 07:43, Jim Talbut wrote:
>>> Claus,
>>>
>>> That's useful, it would simplify my interface, but it wouldn't let me
>>> leave it alone.
>>>
>>> My actual problem is that I need to be able to correlate the exchange
>>> that called my POJO with exchanges generated by calls from my POJO.
>>> To do this I need to access the correlation ID (if it's been set) or
>>> the exchange ID (if it hasn't) and then set them as headers.
>>>
>>> The class that actually calls the other routes uses ProducerTemplate
>>> and I'm quite happy for that class to have Camel-specific code in it,
>>> but the business logic class shouldn't need to know anything about the
>>> correlation.
>>>
>>> I've been looking at writing a custom InflightRepository, but that
>>> would have to map from thread ID, which won't work with the
>>> asynchronous processing (nor will any other thread-local approach).
>>> If I try to set the correlation ID as a property on my busines logic
>>> POJO I'll run into threading issues (it may be called concurrently).
>>>
>>> Thanks
>>>
>>> Jim
>>>
>>> On 26/07/2011 07:06, Claus Ibsen wrote:
>>>> On Tue, Jul 26, 2011 at 7:39 AM, Jim Talbut<jt...@spudsoft.co.uk>
>>>> wrote:
>>>>> Hi,
>>>>>
>>>>> I have a POJO that benefits from having a non-Camel interface (it
>>>>> means I
>>>>> can know that the interface matches that of a given web service).
>>>>>
>>>>> Internally this POJO needs to extract a couple of properties from the
>>>>> Exchange.
>>>>>
>>>>> Is there any way to get the "currently executing Exchange" without
>>>>> adding it
>>>>> as a parameter to the POJO?
>>>>> Most useful for me would be either a static method or a method on the
>>>>> CamelContext, because they would allow me to move the Camel-specific
>>>>> processing into a separate bean.
>>>>>
>>>> On trunk I have improved the bean component, so you can bind the
>>>> parameters in the method name option.
>>>> .to"(bean:myBean?method=myMethod(${body}, ${header.foo},
>>>> ${header.bar}, 'This is a String'")
>>>>
>>>> More details at:
>>>> http://camel.apache.org/bean-binding.html
>>>>
>>>>
>>>>
>>>>> Thanks
>>>>>
>>>>> Jim
>>>>>
>>>>


Re: How to get Exchange from a within a POJO without changing the interface?

Posted by James Talbut <jt...@tardis.spudsoft>.
A bit more playing and I've got my POJO working with correlation IDs from MDC.

There is one oddity:
After my ProducerTemplate returns the MDC map has been cleared - shouldn't it reflect the parent route as that Exchange is still being processed?
In other words, shouldn't the MDC map members be a stack, rather than a simple variable?

To make my code work I manually reset the MDC correlation ID after the producer template returns.

Thanks.

Jim


On Tue, Jul 26, 2011 at 07:56:15AM +0100, Jim Talbut wrote:
> 
> Just read the 2.8.0 release notes, could I use MDC for this?
> It's not currently enabled on my context, but I'll be using either Pax 
> or logback so I can enable it.
> 
> If so, what excellent timing, thank you :)
> 
> Jim
> 
> On 26/07/2011 07:43, Jim Talbut wrote:
> > Claus,
> >
> > That's useful, it would simplify my interface, but it wouldn't let me 
> > leave it alone.
> >
> > My actual problem is that I need to be able to correlate the exchange 
> > that called my POJO with exchanges generated by calls from my POJO.
> > To do this I need to access the correlation ID (if it's been set) or 
> > the exchange ID (if it hasn't) and then set them as headers.
> >
> > The class that actually calls the other routes uses ProducerTemplate 
> > and I'm quite happy for that class to have Camel-specific code in it, 
> > but the business logic class shouldn't need to know anything about the 
> > correlation.
> >
> > I've been looking at writing a custom InflightRepository, but that 
> > would have to map from thread ID, which won't work with the 
> > asynchronous processing (nor will any other thread-local approach).
> > If I try to set the correlation ID as a property on my busines logic 
> > POJO I'll run into threading issues (it may be called concurrently).
> >
> > Thanks
> >
> > Jim
> >
> > On 26/07/2011 07:06, Claus Ibsen wrote:
> >> On Tue, Jul 26, 2011 at 7:39 AM, Jim Talbut<jt...@spudsoft.co.uk>  
> >> wrote:
> >>> Hi,
> >>>
> >>> I have a POJO that benefits from having a non-Camel interface (it 
> >>> means I
> >>> can know that the interface matches that of a given web service).
> >>>
> >>> Internally this POJO needs to extract a couple of properties from the
> >>> Exchange.
> >>>
> >>> Is there any way to get the "currently executing Exchange" without 
> >>> adding it
> >>> as a parameter to the POJO?
> >>> Most useful for me would be either a static method or a method on the
> >>> CamelContext, because they would allow me to move the Camel-specific
> >>> processing into a separate bean.
> >>>
> >> On trunk I have improved the bean component, so you can bind the
> >> parameters in the method name option.
> >> .to"(bean:myBean?method=myMethod(${body}, ${header.foo},
> >> ${header.bar}, 'This is a String'")
> >>
> >> More details at:
> >> http://camel.apache.org/bean-binding.html
> >>
> >>
> >>
> >>> Thanks
> >>>
> >>> Jim
> >>>
> >>
> >>
> >
> 

Re: How to get Exchange from a within a POJO without changing the interface?

Posted by Jim Talbut <jt...@spudsoft.co.uk>.
Just read the 2.8.0 release notes, could I use MDC for this?
It's not currently enabled on my context, but I'll be using either Pax 
or logback so I can enable it.

If so, what excellent timing, thank you :)

Jim

On 26/07/2011 07:43, Jim Talbut wrote:
> Claus,
>
> That's useful, it would simplify my interface, but it wouldn't let me 
> leave it alone.
>
> My actual problem is that I need to be able to correlate the exchange 
> that called my POJO with exchanges generated by calls from my POJO.
> To do this I need to access the correlation ID (if it's been set) or 
> the exchange ID (if it hasn't) and then set them as headers.
>
> The class that actually calls the other routes uses ProducerTemplate 
> and I'm quite happy for that class to have Camel-specific code in it, 
> but the business logic class shouldn't need to know anything about the 
> correlation.
>
> I've been looking at writing a custom InflightRepository, but that 
> would have to map from thread ID, which won't work with the 
> asynchronous processing (nor will any other thread-local approach).
> If I try to set the correlation ID as a property on my busines logic 
> POJO I'll run into threading issues (it may be called concurrently).
>
> Thanks
>
> Jim
>
> On 26/07/2011 07:06, Claus Ibsen wrote:
>> On Tue, Jul 26, 2011 at 7:39 AM, Jim Talbut<jt...@spudsoft.co.uk>  
>> wrote:
>>> Hi,
>>>
>>> I have a POJO that benefits from having a non-Camel interface (it 
>>> means I
>>> can know that the interface matches that of a given web service).
>>>
>>> Internally this POJO needs to extract a couple of properties from the
>>> Exchange.
>>>
>>> Is there any way to get the "currently executing Exchange" without 
>>> adding it
>>> as a parameter to the POJO?
>>> Most useful for me would be either a static method or a method on the
>>> CamelContext, because they would allow me to move the Camel-specific
>>> processing into a separate bean.
>>>
>> On trunk I have improved the bean component, so you can bind the
>> parameters in the method name option.
>> .to"(bean:myBean?method=myMethod(${body}, ${header.foo},
>> ${header.bar}, 'This is a String'")
>>
>> More details at:
>> http://camel.apache.org/bean-binding.html
>>
>>
>>
>>> Thanks
>>>
>>> Jim
>>>
>>
>>
>


Re: How to get Exchange from a within a POJO without changing the interface?

Posted by Jim Talbut <jt...@spudsoft.co.uk>.
Claus,

That's useful, it would simplify my interface, but it wouldn't let me 
leave it alone.

My actual problem is that I need to be able to correlate the exchange 
that called my POJO with exchanges generated by calls from my POJO.
To do this I need to access the correlation ID (if it's been set) or the 
exchange ID (if it hasn't) and then set them as headers.

The class that actually calls the other routes uses ProducerTemplate and 
I'm quite happy for that class to have Camel-specific code in it, but 
the business logic class shouldn't need to know anything about the 
correlation.

I've been looking at writing a custom InflightRepository, but that would 
have to map from thread ID, which won't work with the asynchronous 
processing (nor will any other thread-local approach).
If I try to set the correlation ID as a property on my busines logic 
POJO I'll run into threading issues (it may be called concurrently).

Thanks

Jim

On 26/07/2011 07:06, Claus Ibsen wrote:
> On Tue, Jul 26, 2011 at 7:39 AM, Jim Talbut<jt...@spudsoft.co.uk>  wrote:
>> Hi,
>>
>> I have a POJO that benefits from having a non-Camel interface (it means I
>> can know that the interface matches that of a given web service).
>>
>> Internally this POJO needs to extract a couple of properties from the
>> Exchange.
>>
>> Is there any way to get the "currently executing Exchange" without adding it
>> as a parameter to the POJO?
>> Most useful for me would be either a static method or a method on the
>> CamelContext, because they would allow me to move the Camel-specific
>> processing into a separate bean.
>>
> On trunk I have improved the bean component, so you can bind the
> parameters in the method name option.
> .to"(bean:myBean?method=myMethod(${body}, ${header.foo},
> ${header.bar}, 'This is a String'")
>
> More details at:
> http://camel.apache.org/bean-binding.html
>
>
>
>> Thanks
>>
>> Jim
>>
>
>


Re: How to get Exchange from a within a POJO without changing the interface?

Posted by Claus Ibsen <cl...@gmail.com>.
On Tue, Jul 26, 2011 at 7:39 AM, Jim Talbut <jt...@spudsoft.co.uk> wrote:
> Hi,
>
> I have a POJO that benefits from having a non-Camel interface (it means I
> can know that the interface matches that of a given web service).
>
> Internally this POJO needs to extract a couple of properties from the
> Exchange.
>
> Is there any way to get the "currently executing Exchange" without adding it
> as a parameter to the POJO?
> Most useful for me would be either a static method or a method on the
> CamelContext, because they would allow me to move the Camel-specific
> processing into a separate bean.
>

On trunk I have improved the bean component, so you can bind the
parameters in the method name option.
.to"(bean:myBean?method=myMethod(${body}, ${header.foo},
${header.bar}, 'This is a String'")

More details at:
http://camel.apache.org/bean-binding.html



> Thanks
>
> Jim
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/