Apache Camel JMS端点concurrentConsumers属性未按预期生效问题咨询
问题:JMS端点
concurrentConsumers设置值与实际消费者数量不符 我在Camel路由的JMS端点中设置了concurrentConsumers=20属性,但重启路由/上下文/bundle后,实际生成的消费线程数随机在1到20之间波动。测试环境覆盖Camel 3.4.5、3.21.0.SNAPSHOT版本,搭配ActiveMQ Artemis 2.19.0、2.28.0版本。路由配置如下:
<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> <bean id="artemisConnectionFactory" class="org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory"> <argument index="0" value="tcp://localhost:61616"/> <argument index="1" value="karaf"/> <argument index="2" value="karaf"/> </bean> <bean id="jms" class="org.apache.camel.component.jms.JmsComponent"> <property name="connectionFactory" ref="artemisConnectionFactory"/> </bean> <camelContext id="test-jms-context" xmlns="http://camel.apache.org/schema/blueprint"> <route id="messages-consumer-route" autoStartup="true"> <from uri="jms:queue:concurrent1?concurrentConsumers=20"/> <log loggingLevel="INFO" message="start"/> <delay> <constant>10000</constant> </delay> <log loggingLevel="INFO" message="finish"/> </route> </camelContext> </blueprint>
原因分析
- 原生连接工厂缺少会话池支持:你使用的
ActiveMQJMSConnectionFactory是原生工厂,默认不提供连接/会话池。Camel的JMS组件每个并发消费者需要独立的JMS会话,当同时初始化20个消费者时,底层无法快速创建足够的会话,导致部分消费者初始化失败,最终数量随机波动。 - 未限制消费者数量上限:仅设置
concurrentConsumers仅定义了初始消费者数,Camel默认会根据消息负载动态调整,但如果没有配套设置maxConcurrentConsumers,结合会话资源不足的情况,会出现消费者数量低于预期值的问题。 - Artemis服务端资源限制:Artemis默认对连接数、会话数有隐式限制,若服务端无法分配足够的会话资源给Camel,也会导致消费者无法全部启动。
解决方案
1. 启用连接池
替换为ActiveMQPooledConnectionFactory提供连接和会话池,确保有足够资源支撑20个并发消费者:
<bean id="artemisConnectionFactory" class="org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory"> <argument index="0" value="tcp://localhost:61616"/> <argument index="1" value="karaf"/> <argument index="2" value="karaf"/> </bean> <!-- 新增连接池工厂 --> <bean id="pooledConnectionFactory" class="org.apache.activemq.artemis.jms.client.ActiveMQPooledConnectionFactory"> <property name="connectionFactory" ref="artemisConnectionFactory"/> <property name="maxConnections" value="20"/> <!-- 至少等于并发消费者数 --> <property name="sessionPoolSize" value="20"/> <!-- 每个连接的会话数 --> </bean> <bean id="jms" class="org.apache.camel.component.jms.JmsComponent"> <property name="connectionFactory" ref="pooledConnectionFactory"/> </bean>
2. 固定消费者数量范围
在JMS端点同时设置maxConcurrentConsumers=20,强制Camel维持固定的消费者数量:
<from uri="jms:queue:concurrent1?concurrentConsumers=20&maxConcurrentConsumers=20"/>
3. 检查Artemis服务端配置
确保Artemis的broker.xml中没有过度限制连接/会话的配置,比如:
- 确认
max-connections值足够大(默认是1000,一般足够) - 检查
session-ttl是否过短导致会话被提前回收
内容的提问来源于stack exchange,提问作者Sergey Kryuchkov
相关产品推荐
相关产品推荐

