Hi

See this FAQ
https://camel.apache.org/manual/faq/why-does-camel-use-too-many-threads-with-producertemplate.html

You do not create a template per message you send, you create it once
/ or better you dependency inject it into the route builder.
Since you use spring you can use @Autowired or @Inject etc

On Wed, Nov 3, 2021 at 12:25 PM Vyacheslav Boyko <[email protected]> wrote:
>
> Hello.
>
> I have faced with important notice in JavaDoc of
> org.apache.camel.CamelContext#createProducerTemplate() and it makes me
> rethink how to use it.
>
> JavaDoc prescribes: Important: Make sure to call ProducerTemplate.stop()
> when you are done using the template, to clean up any resources.
>
> So, when I implement RouteBuilder in SpringBoot project I make it like:
>
> @Component
> class BusinessRouteBuilder extends RouteBuilder {
>    @Override
>    public void configure() {
>      from("seda://start")
>        .to("seda://business-start"); // to process in separated thread
>    }
>    public void sendMessage(Msg msg) {
>      getContext().createProducerTemplate().sendBody("seda://start", msg);
>    }
> }
>
>
> My question and concern is: am I doing it correctly?
>
> It seems I have to stop ProducerTemplate created before. But where to
> close it? Right after it was used in sendMessage() like the following?
>
>    public void sendMessage(Msg msg) {
>      ProducerTemplate producerTemplate =
> getContext().createProducerTemplate();
>      producerTemplate.sendBody("seda://start", msg);
>      producerTemplate.stop();
>    }
>
> Or maybe it will be better to create one ProducerTemplate per Spring
> bean and to stop it in @PreDestroy method like the following?
>
> @Component
> class BusinessRouteBuilder extends RouteBuilder {
>    ProducerTemplate producerTemplate;
>    @Override
>    public void configure() {
>      producerTemplate = getContext().createProducerTemplate();
>      from("seda://start")
>        .to("seda://business-start"); // to process in separated thread
>    }
>    public void sendMessage(Msg msg) {
>      producerTemplate.sendBody("seda://start", msg);
>    }
>    @PreDestroy
>    public void preDestroy() {
>      producerTemplate.stop();
>    }
> }
>
> ___
> Vyacheslav Boyko
> mailto:[email protected]
>


-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Reply via email to