You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by Christian Schneider <ch...@die-schneider.net> on 2012/09/18 15:12:27 UTC

Problem with ProducerTemplate on context shutdown

Hi all,

we had a problem with the camel shutdown at a Talend ESB Camel example 
after updating to camel 2.10-SNAPSHOT.

The example uses a ProducerTemplate with a camel jms producer that is 
injected using an annotation:
@EndpointInject
ProducerTemplate producer;

At the end of the example main the spring context and with it the camel 
context is shut down.
Before the update the update the main then exited normally. After the 
update it did not exit.

I found that the ProducerTemplate created a JMSProducer which with a 
DefaultMessageListenerContainer for the replies. This was still active 
and so the main did not exit.
When I issues a producer.close(); the main exited normally.

So the question is: Do I need to close a ProducerTemplate myself or 
should it be closed when the camel context that created it is shut down?

Christian

---
Here is the full example:
https://github.com/Talend/tesb-rt-se/tree/master/examples/camel/jaxrs-jms-http/client
and the change that made it work again:
https://github.com/Talend/tesb-rt-se/commit/7a9ae3502ca6f75ff6e38aba9dd94d86578cc028


-- 
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com


Re: Problem with ProducerTemplate on context shutdown

Posted by Christian Schneider <ch...@die-schneider.net>.
I think that is not necessary. I only wanted to check if it is normal 
behaviour or if we have a bug.
Strangely it seemed to not hang in earlier versions. Not sure why though.

Christian

Am 18.09.2012 15:39, schrieb Claus Ibsen:
> As the templates (their default impls) is associated with a
> CamelContext, we could eventually also add logic, to enlist the
> template in a list of active template stored on the camel context. So
> when camel context shutdown, it could shutdown the templates as well -
> as a fail safe.
>
> And if a client is calling stop on a template manually, we would then
> need to unregistered it from the list on camel context. To avoid the
> list to grow large if people create a lot of templates and stop them
> again.
>
> So it would be doable to build-in this "failsafe" for the end users.
> But ideally its best if users call stop when they no longer need the
> template. For example if its just a one-stop usage.
>
>

-- 
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com


Re: Problem with ProducerTemplate on context shutdown

Posted by Claus Ibsen <cl...@gmail.com>.
As the templates (their default impls) is associated with a
CamelContext, we could eventually also add logic, to enlist the
template in a list of active template stored on the camel context. So
when camel context shutdown, it could shutdown the templates as well -
as a fail safe.

And if a client is calling stop on a template manually, we would then
need to unregistered it from the list on camel context. To avoid the
list to grow large if people create a lot of templates and stop them
again.

So it would be doable to build-in this "failsafe" for the end users.
But ideally its best if users call stop when they no longer need the
template. For example if its just a one-stop usage.



On Tue, Sep 18, 2012 at 3:26 PM, Claus Ibsen <cl...@gmail.com> wrote:
> On Tue, Sep 18, 2012 at 3:12 PM, Christian Schneider
> <ch...@die-schneider.net> wrote:
>> Hi all,
>>
>> we had a problem with the camel shutdown at a Talend ESB Camel example after
>> updating to camel 2.10-SNAPSHOT.
>>
>> The example uses a ProducerTemplate with a camel jms producer that is
>> injected using an annotation:
>> @EndpointInject
>> ProducerTemplate producer;
>>
>> At the end of the example main the spring context and with it the camel
>> context is shut down.
>> Before the update the update the main then exited normally. After the update
>> it did not exit.
>>
>> I found that the ProducerTemplate created a JMSProducer which with a
>> DefaultMessageListenerContainer for the replies. This was still active and
>> so the main did not exit.
>> When I issues a producer.close(); the main exited normally.
>>
>> So the question is: Do I need to close a ProducerTemplate myself or should
>> it be closed when the camel context that created it is shut down?
>>
>
> Yes its always the clients responsibility to invoke the stop method
> when you are done with the template.
> See this FAQ:
> http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html
>
> I have polished the javadoc of the createProducerTemplate methods on
> CamelContext to make this more clear.
>
>
>> Christian
>>
>> ---
>> Here is the full example:
>> https://github.com/Talend/tesb-rt-se/tree/master/examples/camel/jaxrs-jms-http/client
>> and the change that made it work again:
>> https://github.com/Talend/tesb-rt-se/commit/7a9ae3502ca6f75ff6e38aba9dd94d86578cc028
>>
>>
>> --
>> Christian Schneider
>> http://www.liquid-reality.de
>>
>> Open Source Architect
>> Talend Application Integration Division http://www.talend.com
>>
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: cibsen@fusesource.com
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen



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

Re: Problem with ProducerTemplate on context shutdown

Posted by Christian Schneider <ch...@die-schneider.net>.
Hi Claus,

thanks for the clarification. It is also great to have this in Javadoc.

Christian

Am 18.09.2012 15:26, schrieb Claus Ibsen:
> On Tue, Sep 18, 2012 at 3:12 PM, Christian Schneider
> <ch...@die-schneider.net> wrote:
>> Hi all,
>>
>> we had a problem with the camel shutdown at a Talend ESB Camel example after
>> updating to camel 2.10-SNAPSHOT.
>>
>> The example uses a ProducerTemplate with a camel jms producer that is
>> injected using an annotation:
>> @EndpointInject
>> ProducerTemplate producer;
>>
>> At the end of the example main the spring context and with it the camel
>> context is shut down.
>> Before the update the update the main then exited normally. After the update
>> it did not exit.
>>
>> I found that the ProducerTemplate created a JMSProducer which with a
>> DefaultMessageListenerContainer for the replies. This was still active and
>> so the main did not exit.
>> When I issues a producer.close(); the main exited normally.
>>
>> So the question is: Do I need to close a ProducerTemplate myself or should
>> it be closed when the camel context that created it is shut down?
>>
> Yes its always the clients responsibility to invoke the stop method
> when you are done with the template.
> See this FAQ:
> http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html
>
> I have polished the javadoc of the createProducerTemplate methods on
> CamelContext to make this more clear.
>
>
>> Christian
>>
>> ---
>> Here is the full example:
>> https://github.com/Talend/tesb-rt-se/tree/master/examples/camel/jaxrs-jms-http/client
>> and the change that made it work again:
>> https://github.com/Talend/tesb-rt-se/commit/7a9ae3502ca6f75ff6e38aba9dd94d86578cc028
>>
>>
>> --
>> Christian Schneider
>> http://www.liquid-reality.de
>>
>> Open Source Architect
>> Talend Application Integration Division http://www.talend.com
>>
>
>


-- 
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com


Re: Problem with ProducerTemplate on context shutdown

Posted by Claus Ibsen <cl...@gmail.com>.
On Tue, Sep 18, 2012 at 3:12 PM, Christian Schneider
<ch...@die-schneider.net> wrote:
> Hi all,
>
> we had a problem with the camel shutdown at a Talend ESB Camel example after
> updating to camel 2.10-SNAPSHOT.
>
> The example uses a ProducerTemplate with a camel jms producer that is
> injected using an annotation:
> @EndpointInject
> ProducerTemplate producer;
>
> At the end of the example main the spring context and with it the camel
> context is shut down.
> Before the update the update the main then exited normally. After the update
> it did not exit.
>
> I found that the ProducerTemplate created a JMSProducer which with a
> DefaultMessageListenerContainer for the replies. This was still active and
> so the main did not exit.
> When I issues a producer.close(); the main exited normally.
>
> So the question is: Do I need to close a ProducerTemplate myself or should
> it be closed when the camel context that created it is shut down?
>

Yes its always the clients responsibility to invoke the stop method
when you are done with the template.
See this FAQ:
http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html

I have polished the javadoc of the createProducerTemplate methods on
CamelContext to make this more clear.


> Christian
>
> ---
> Here is the full example:
> https://github.com/Talend/tesb-rt-se/tree/master/examples/camel/jaxrs-jms-http/client
> and the change that made it work again:
> https://github.com/Talend/tesb-rt-se/commit/7a9ae3502ca6f75ff6e38aba9dd94d86578cc028
>
>
> --
> Christian Schneider
> http://www.liquid-reality.de
>
> Open Source Architect
> Talend Application Integration Division http://www.talend.com
>



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

Re: Problem with ProducerTemplate on context shutdown

Posted by Christian Schneider <ch...@die-schneider.net>.
Hi Babak,

that sounds good.

Thanks

Christian

Am 18.09.2012 16:32, schrieb Babak Vahdat:
> Hi
>
> IMHO instead of the "stop" workaround better let the IoC container do the
> job for you. That's instead of declaring it like:
>
> @EndpointInject
> ProducerTemplate producer;
>
> Simply do:
>
> @Autowired
> ProducerTemplate producer;
>
> And declare the ProducerTemplate the way from the Wiki (see the XML-snippet
> of what Claus already sent):
>
>   <camelContext id="camelContext"
> xmlns="http://camel.apache.org/schema/spring" trace="true">
>     <template id="..."/>
>
> And then Spring will take care of the rest (no need for an explicit stop
> call anymore). The same also applies if you would make use of Blueprint
> instead of Spring.
>
> Under the cover the following FactoryBeans do the Job (see the destroy
> method implementation which they both share):
>
> https://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelProducerTemplateFactoryBean.java
> https://svn.apache.org/repos/asf/camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelProducerTemplateFactoryBean.java
>
> Babak
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Problem-with-ProducerTemplate-on-context-shutdown-tp5719525p5719533.html
> Sent from the Camel Development mailing list archive at Nabble.com.


-- 
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com


Re: Problem with ProducerTemplate on context shutdown

Posted by Babak Vahdat <ba...@swissonline.ch>.
Hi

IMHO instead of the "stop" workaround better let the IoC container do the
job for you. That's instead of declaring it like:

@EndpointInject
ProducerTemplate producer;

Simply do:

@Autowired
ProducerTemplate producer;

And declare the ProducerTemplate the way from the Wiki (see the XML-snippet
of what Claus already sent):

 <camelContext id="camelContext"
xmlns="http://camel.apache.org/schema/spring" trace="true">
   <template id="..."/>

And then Spring will take care of the rest (no need for an explicit stop
call anymore). The same also applies if you would make use of Blueprint
instead of Spring.

Under the cover the following FactoryBeans do the Job (see the destroy
method implementation which they both share):

https://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelProducerTemplateFactoryBean.java
https://svn.apache.org/repos/asf/camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelProducerTemplateFactoryBean.java

Babak




--
View this message in context: http://camel.465427.n5.nabble.com/Problem-with-ProducerTemplate-on-context-shutdown-tp5719525p5719533.html
Sent from the Camel Development mailing list archive at Nabble.com.