在RabbitMq重启消费者不消耗

时间:2016-02-17 16:06:06

标签: java spring tomcat spring-rabbit

我们正在使用Spring Rabbit来生成和使用来自同一应用程序的消息,但是,我们注意到当RabbitMq重新启动时,队列会继续增长,直到我们重新启动Tomcat。最初,我们认为拥有高可用性队列可以解决这个问题,但事实并非如此。这是Spring-Rabbit配置的messaging.xml:

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

    <!--  RabbitMQ -->
    <rabbit:connection-factory id="rabbitConnectionFactory" addresses="${rabbitMq.hostName}" username="${rabbitMq.userName}" password="${rabbitMq.password}" virtual-host="${rabbitMq.vHost}" />
    <rabbit:admin connection-factory="rabbitConnectionFactory"/>

    <!-- RabbitMQ Template -->
    <rabbit:template id="rabbitTemplate" connection-factory="rabbitConnectionFactory" message-converter="messageConverter" exchange="${rabbitMq.myExchange}" />

    <!-- RabbitMQ Exchange and Binding Definition -->
    <rabbit:direct-exchange name="${rabbitMq.yEmxchange}">
        <rabbit:bindings>
            <rabbit:binding key="${rabbitMq.routing.key.events}" queue="${rabbitMq.queue.events.name}"/>
        </rabbit:bindings>
    </rabbit:direct-exchange>

    <!-- RabbitMQ Queue Definitions -->
    <rabbit:queue name="${rabbitMq.queue.events.name}" >
        <rabbit:queue-arguments>
            <entry key="x-ha-policy" value="all"/>
        </rabbit:queue-arguments>
    </rabbit:queue>

    <!-- Listeners -->
    <rabbit:listener-container id="listener" connection-factory="rabbitConnectionFactory" concurrency="3" max-concurrency="5" requeue-rejected="false">
        <rabbit:listener ref="eventNotificationAdapter" queues="${rabbitMq.queue.events.name}" priority="${rabbitMq.queue.events.priority}" />
    </rabbit:listener-container>

    <!-- Message Listener Adapters -->
    <bean id="eventNotificationAdapter" class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
        <constructor-arg name="delegate" ref="eventConsumer"/>
        <constructor-arg name="messageConverter" ref="messageConverter"/>
        <property name="defaultListenerMethod" value="process"/>
    </bean>

    <bean id="messageConverter" class="org.springframework.amqp.support.converter.JsonMessageConverter"/>

</beans>

这是简单的消费者:

@Component("eventConsumer")
public class EventQueueConsumer {

    @Autowired
    EventsClient eventsClient;

    private static Logger logger = LoggerFactory.getLogger(EventQueueConsumer.class);

    public void process(MyEvent myEvent) {
        logger.info("processing myEvent: {}", myEvent);
        try{
            eventsClient.sendEvent(myEvent);
        } catch(Exception e) {
            logger.error("Exception occurred trying to send event.", e);
        }
    }
}

基于这种配置,是否有人碰巧知道为什么队列继续增长并且在重启RabbitMq服务器后不被消耗?我们总是必须重新启动应用程序运行的网络服务器(Tomcat),以使其再次开始消费。

0 个答案:

没有答案