DefaultMessageListenerContainer 和 ActiveMQ 线程管理
DefaultMessageListenerContainer and ActiveMQ thread management
我用 ActiveMQ 和 Spring JMS 做了一些测试。我已经使用 WorkManagerTaskExecutor
和 CommonJ 配置了 DefaultMessageListenerContainer
(DMLC) 来管理线程。我想控制服务器中的线程运行。
根据 Juergen Holler 在 Spring 论坛中写的 post,我决定使用 DMLC:http://forum.spring.io/forum/other-spring-related/remoting/24208-what-s-the-best-practice-for-using-jms-in-spring?p=256420#post256420
在这个post里他说"DMLC is the only listener container that does not impose the thread management onto the JMS provider, that is, does not use/block JMS provider threads."所以我想所有的线程都由服务器来管理,不会有ActiveMQ的线程。
但是,使用JConsole分析服务器的线程,我看到了一些我没想到的ActiveMQ线程。
如图所示,有 ActimeMQ 线程(非活动线程、传输线程等)。
当我执行测试时,我在日志中看到 JMS 消息是由 CommonJ 线程处理的,而不是由 ActiveMQ 线程处理的,所以这很好。但是,我不明白为什么不使用 ActiveMQ 线程会创建它们。特别是 "ActiveMQ Transport" 个线程,因为我使用的每个队列都有一个线程 "ActiveMQ Transport"。因此,如果我使用 50 个队列,我就有 50 个 "ActiveMQ Transport" 线程。是为了保持插座打开?这些线程是强制性的吗?
配置详情:
- 使用的 ConnectionFactory:org.apache.activemq.ActiveMQConnectionFactory
- ActiveMQ版本:5.11.1
- 传输协议:TCP
- 使用 DefaultMessageListenerContainer,客户端处于调用 MessageConsumer.receive() 方法的循环中。
这似乎是一个愚蠢的问题,我肯定误解了一些基本概念。
感谢 Rob Davies 在此线程中的解释 http://activemq.2283324.n4.nabble.com/ActimeMQ-Client-s-thread-management-td4705885.html,我了解到与 ActiveMQ 的所有交互都需要每个连接一个 TCP 线程。
但是,可以最小化传输线程的数量,将 ActiveMQConnectionFactory 替换为以下 ConnectionFactories 之一:
- PooledConnectionFactory (ActiveMQ)
- SingleConnectionFactory (spring-jms)
- CachingConnectionFactory (spring-jms)
我用 ActiveMQ 和 Spring JMS 做了一些测试。我已经使用 WorkManagerTaskExecutor
和 CommonJ 配置了 DefaultMessageListenerContainer
(DMLC) 来管理线程。我想控制服务器中的线程运行。
根据 Juergen Holler 在 Spring 论坛中写的 post,我决定使用 DMLC:http://forum.spring.io/forum/other-spring-related/remoting/24208-what-s-the-best-practice-for-using-jms-in-spring?p=256420#post256420
在这个post里他说"DMLC is the only listener container that does not impose the thread management onto the JMS provider, that is, does not use/block JMS provider threads."所以我想所有的线程都由服务器来管理,不会有ActiveMQ的线程。
但是,使用JConsole分析服务器的线程,我看到了一些我没想到的ActiveMQ线程。
如图所示,有 ActimeMQ 线程(非活动线程、传输线程等)。
当我执行测试时,我在日志中看到 JMS 消息是由 CommonJ 线程处理的,而不是由 ActiveMQ 线程处理的,所以这很好。但是,我不明白为什么不使用 ActiveMQ 线程会创建它们。特别是 "ActiveMQ Transport" 个线程,因为我使用的每个队列都有一个线程 "ActiveMQ Transport"。因此,如果我使用 50 个队列,我就有 50 个 "ActiveMQ Transport" 线程。是为了保持插座打开?这些线程是强制性的吗?
配置详情:
- 使用的 ConnectionFactory:org.apache.activemq.ActiveMQConnectionFactory
- ActiveMQ版本:5.11.1
- 传输协议:TCP
- 使用 DefaultMessageListenerContainer,客户端处于调用 MessageConsumer.receive() 方法的循环中。
这似乎是一个愚蠢的问题,我肯定误解了一些基本概念。
感谢 Rob Davies 在此线程中的解释 http://activemq.2283324.n4.nabble.com/ActimeMQ-Client-s-thread-management-td4705885.html,我了解到与 ActiveMQ 的所有交互都需要每个连接一个 TCP 线程。
但是,可以最小化传输线程的数量,将 ActiveMQConnectionFactory 替换为以下 ConnectionFactories 之一:
- PooledConnectionFactory (ActiveMQ)
- SingleConnectionFactory (spring-jms)
- CachingConnectionFactory (spring-jms)