我在Apache ServiceMix上部署了一个Webservice,它使用Apache Camel来调用ActiveMQ驱动的路由,使用类似于以下的代码:
context.createProducerTemplate().sendBody("activemq:startComplex", xml);
调用工作正常,但一段时间后我的Linux机器上的文件描述符限制被命中。资源被一大堆(几千个)ActiveMQ线程占用。在jmx控制台下,我可以看到很多类似于以下内容的线程:
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)
和
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)
想法如何摆脱挂线?
答案 0 :(得分:2)
请参阅此常见问题解答 http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html
您不应在每次发送消息时创建新的生产者模板。如果你这样做,那么记得在使用后关闭它。
答案 1 :(得分:1)
我设法通过放弃使用ProducerTemplate和ConsumerTemplate来摆脱泄漏的线程问题。
我现在使用标准JMS API来发送和接收来自ActiveMQ的消息。