camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jakub Korab <>
Subject Re: Spring-Boot + Camel + producerTemplate ssh spawning thousands of threads
Date Thu, 12 Nov 2015 16:05:53 GMT
When you use the SSH producer endpoint in a to(..) statement, one 
instance of the endpoint is created within the route, its lifecycle is 
tied to the CamelContext and any resources used by it will be cleaned up 
at shutdown. The endpoint creates an instance of an SshClient 
(underlying library), and closes it when the context shuts down.

When you use it through a ProducerTemplate, each time you access the 
send() method it creates a new instance of the SshEndpoint, allocating 
memory on the way by creating a new underlying SshClient instance. This 
behaviour is dictated by SshEndpoint. isSingleton(), which returns false 
as the underlying client library is not thread-safe. You need to close 
the ProducerTemplate in order to close the SshClient instance (note, 
nothing to do with the SSH connection itself).

As a workaround, you might consider having the CamelContext injected 
into your bean, and for each request do:

ProducerTemplate template = context.createProducerTemplate();

It's expensive, but it will do the job.

I don't think this is a bug, if seems to be a side-effect of how a 
ProducerTemplate works with non-singleton endpoints. If there were 
multiple threads, then the ProducerTemplate would have to create 
multiple endpoints for the underlying library to work correctly, and for 
the threads to not trip over each other. In your case, only one thread 
will ever be using that one ProducerTemplate, so it doesn't make sense. 
The ProducerTemplate can't know in advance how many threads will be 
accessing it.

On 12/11/15 15:10, codan84 wrote:
> Hmm Interesting, this indeed fixes the problem. I am forced to use a
> producerTemplate tho... Any ideas what am I doing wrong?
> --
> View this message in context:
> Sent from the Camel - Users mailing list archive at

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message