JMS队列无负载时消费者数量不缩减

Number of consumer not shrinking when there is not load in JMS queue

我正在编写一个 JMS 消费者,使用 spring-jms 来消费来自 ActiveMQ Artemis 代理的消息。我正在调查 3 个属性的行为:concurrentConsumersmaxConcurrentConsumersidleConsumerLimit.

预期行为是当队列中的负载增加时,消费者应扩大到 maxConcurrentConsumers,而当负载减少时,消费者应缩小到 idleConsumerLimit

但是 spring-jms 和 ActiveMQ Artemis 不会发生这种情况。当队列中没有消息时,消费者计数不会下降,而是保留所有 maxConcurrentConsumers.

我正在使用 spring DMLC 容器

    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destinationName" value="TestingInQueue" />
        <property name="messageListener" ref="messageListener" />
        <property name="concurrentConsumers" value="1"/>
        <property name="maxConcurrentConsumers" value="20"/>
        <property name="idleConsumerLimit" value="5"/>
    </bean>

完整上下文 XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <props>
                <prop key="java.naming.factory.initial">org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory</prop>
                <prop key="java.naming.provider.url">tcp://localhost:61616</prop>
                <prop key="java.naming.security.principal">admin</prop>
                <prop key="java.naming.security.credentials">admin</prop>
            </props>
        </property>
    </bean>

    <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiTemplate" ref="jndiTemplate"/>
        <property name="jndiName" value="ConnectionFactory"/>
    </bean>

    <bean id="destinationQueue" class="org.apache.activemq.artemis.jms.client.ActiveMQQueue">
        <constructor-arg index="0" value="TestingInQueue" />
    </bean>

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="defaultDestination" ref="destinationQueue" />
    </bean>

    <bean id="messageListener" class="com.practise.SampleListener">
        <property name="jmsTemplate" ref="jmsTemplate" />
        <property name="queue" ref="destinationQueue" />
    </bean>

    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destinationName" value="TestingInQueue" />
        <property name="messageListener" ref="messageListener" />
        <property name="concurrentConsumers" value="1"/>
        <property name="maxConcurrentConsumers" value="20"/>
        <property name="idleConsumerLimit" value="5"/>
    </bean>
</beans>

注:

  • maxConcurrentConsumers:负载下的最大消费者数
  • concurrentConsumers : 闲来无事终会到此。 (到达这里所需的时间取决于接下来的 3 个参数)

以下 3 个是为了避免消费者在 maxConcurrentConsumersconcurrentConsumers 之间急剧扩大和缩小,没有任何时间延迟。

  • idleConsumerLimit
  • maxMessagesPerTask
  • idleTaskExecutionLimit

例子

如果从重负载开始,您将有 20 (maxConcurrentConsumers) 个消费者。然后,如果您使队列为空,它将缩小到 5 (idleConsumerLimit) 。然后它将等待达到 idleTaskExecutionLimit 次尝试,然后缩小到 1 (concurrentConsumers)。 idleTaskExecutionLimit 是导致空获取的获取尝试次数,其中每次获取需要 receiveTimeout(默认 1000 毫秒)时间单位。

问题:

maxMessagesPerTask 的默认值为 -1,如果 maxMessagesPerTask 不大于零

,则 idleConsumerLimit 不会生效

解法:

指定idleConsumerLimit时,也指定maxMessagesPerTask

Specify a number of 10 to 100 messages to balance between rather long-lived and rather short-lived tasks here for maxMessagesPerTask

参考