ProducerTemplate must be only one instance.

We use Spring and Camel in our web application. In one of our controllers, we use ProducerTemplate to send a message along a route, and now I want to add another controller that sends a message along a separate route. I'm trying to figure out if we assume that there is one ProducerTemplate for the entire application?

I ask about it because I was a little embarrassed after reading this .

+4
source share
2 answers

Quote from the link you gave:

You do not have to create a ProducerTemplate for every call to the message; You must create one instance at startup and save it.

Also, when you are finished using ProducerTemplate, you must call the stop () method to close all the resources that it used.

So, in a typical web application, you do not create a ProducerTemplate for each request, for example:

 ProducerTemplate template = camelContext.createProducerTemplate(); 

This is considered bad practice, for obvious reasons.

Instead, an endpoint should be entered, for example. using @EndpointInject annotation

 @EndpointInject(uri = "file:{{file.inbox}}") private ProducerTemplate inbox; 

as described here .

+4
source

In general, make sure that you do not create a new ProducerTemplate for each message you process. However, creating multiple of them on different routes / beans / processors is also great ... just cache them for subsequent message requests.

+2
source

All Articles