在Linux上的Tomcat中运行的Java Spring servlet应用程序。
我有一个简单的消费者,它从ActiveMQ抓取消息并将数据处理到数据库中。在几乎所有情况下,消费者的数量都是2,在某些情况下可能会达到3。但是,在一些罕见的情况下,我看到消费者的数量达到maxConcurrentConsumers(设置为10)。在此之后的某个时刻,不再继续处理消息。消息大约每秒生成一次(最多),所以这并不是每秒排队1000个消息的情况。
我能够重现行为的唯一一次是从我的IDE以调试模式运行应用程序,然后暂停执行一会儿,然后重新开始。这将导致应用程序恢复,我可以点击各种端点,例如/ status,它返回当前状态并且它正在运行。但是,在Tomcat实例被终止并重新启动之前,不会消耗更多的消息并且消费者的数量会被停留。
接收者类:
@Component
public class Receiver implements MessageListener
{
private static final Logger logger = LoggerFactory.getLogger(Receiver.class);
@Override
public void onMessage(Message message)
{
logger.trace("entering onMessage");
if (message instanceof TextMessage)
{
try
{
String textMessage = ((TextMessage) message).getText();
processMessage(textMessage);
}
catch (Exception e)
{
logger.error("failed to process message", e);
}
}
}
}
servlet-context.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-3.2.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.10.0.xsd">
<mvc:annotation-driven />
<context:property-placeholder />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"/>
</bean>
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="app.consumer"/>
</bean>
<bean id="messageReceiver" class="com.myapp.consumer.Receiver" />
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="destination"/>
<property name="messageListener" ref="messageReceiver" />
<property name="sessionTransacted" value="false"/>
<property name="concurrentConsumers" value="1"/>
<property name="maxConcurrentConsumers" value="10"/>
</bean>
<context:component-scan base-package="com.myapp.consumer" />
</beans>
我发现这篇文章:ActiveMQ Consumer Hangs但我不认为它适用于此。当发生挂起时,我发现系统或jvm中的内存使用量没有显着增加。我没有尝试增加消费者的数量,因为我不认为这是一个解决方案,因为它不是处理量问题。