4

I've got a Webservice deployed on Apache ServiceMix which uses Apache Camel to invoke an ActiveMQ driven route using code similar to the following:

 context.createProducerTemplate().sendBody("activemq:startComplex", xml);

The invocation works fine but after some time the file descriptor limit on my Linux machine gets hit. The resources are eaten up by a whole bunch (a few thousand) of ActiveMQ threads. Under the jmx console I can see a lot of threads similar to the following:

Name: ActiveMQ Transport: tcp://localhost/127.0.0.1:61616
State: RUNNABLE
Total blocked: 0  Total waited: 0

Stack trace: 
 java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(SocketInputStream.java:129)
org.apache.activemq.transport.tcp.TcpBufferedInputStream.fill(TcpBufferedInputStream.java:5    0)
org.apache.activemq.transport.tcp.TcpTransport$2.fill(TcpTransport.java:589)
org.apache.activemq.transport.tcp.TcpBufferedInputStream.read(TcpBufferedInputStream.java:5    8)
org.apache.activemq.transport.tcp.TcpTransport$2.read(TcpTransport.java:574)
java.io.DataInputStream.readInt(DataInputStream.java:370)
org.apache.activemq.openwire.OpenWireFormat.unmarshal(OpenWireFormat.java:275)
org.apache.activemq.transport.tcp.TcpTransport.readCommand(TcpTransport.java:222)
org.apache.activemq.transport.tcp.TcpTransport.doRun(TcpTransport.java:214)
org.apache.activemq.transport.tcp.TcpTransport.run(TcpTransport.java:197)
java.lang.Thread.run(Thread.java:662)

and

Name: ActiveMQ Transport: tcp:///127.0.0.1:46420
State: RUNNABLE
Total blocked: 0  Total waited: 2

Stack trace: 
 java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(SocketInputStream.java:129)
org.apache.activemq.transport.tcp.TcpBufferedInputStream.fill(TcpBufferedInputStream.java:50)
org.apache.activemq.transport.tcp.TcpTransport$2.fill(TcpTransport.java:589)
org.apache.activemq.transport.tcp.TcpBufferedInputStream.read(TcpBufferedInputStream.java:58)
org.apache.activemq.transport.tcp.TcpTransport$2.read(TcpTransport.java:574)
java.io.DataInputStream.readInt(DataInputStream.java:370)
org.apache.activemq.openwire.OpenWireFormat.unmarshal(OpenWireFormat.java:275)
org.apache.activemq.transport.tcp.TcpTransport.readCommand(TcpTransport.java:222)
org.apache.activemq.transport.tcp.TcpTransport.doRun(TcpTransport.java:214)
org.apache.activemq.transport.tcp.TcpTransport.run(TcpTransport.java:197)
java.lang.Thread.run(Thread.java:662)

And ideas how to get rid of the hanging threads?

рüффп
  • 5,172
  • 34
  • 67
  • 113
  • Are you using connection pooling? How many clients are running parallelly? It would be good if you can give more details of the issue. – Buchi May 11 '12 at 13:31
  • I tried with connection pooling enabled and disabled that doesn't really matter. The issues is "invocation" bound. Every invocation leaves some "hanging" threads. I could reproduce it by just invoking the webservice a few times in succesion (serial). I have also tried to enable soTimeout for connections as described here: http://activemq.apache.org/tcp-transport-reference.html. Of course it had no effect. – Tomasz Jędzierowski May 11 '12 at 13:47
  • Does your client send disconnect at the end of communication? Are you creating only producers or you have consumers as well? – Buchi May 11 '12 at 14:19
  • I have consumers as well. I noticed that calling createProducerTemplate frequently is not encouraged according to https://cwiki.apache.org/CAMEL/why-does-camel-use-too-many-threads-with-producertemplate.html. I got rid of it by keeping a reference to ProducerTemplate (and of course ConsumerTemplate) but nothing really changed the threads are still leaking. – Tomasz Jędzierowski May 11 '12 at 14:45

2 Answers2

2

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

You should not create a new producer template on every message send. And if you do, then remember to close it after usage.

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • 1
    Actually I did make the mistake but just as mentioned in one of the replies to the original question changing it (so that only one ProducerTemplate and ConsumerTemplate is created) didn't have any impact on the number of threads being created. – Tomasz Jędzierowski May 14 '12 at 09:22
2

I managed to get rid of the leaking threads issue by dropping all use of ProducerTemplate and ConsumerTemplate.

I am now using standard JMS APIs to send and receive messages from ActiveMQ.