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]

Reply via email to