添加多个侦听器将侦听不同的RabbitMQ队列无法正常工作 [英] Adding multiple listeners which will listen to different RabbitMQ queue not working

查看:123
本文介绍了添加多个侦听器将侦听不同的RabbitMQ队列无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循spring xml配置

I hava following spring xml configuration

<bean id="connectionFactory"
          class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
 <constructor-arg value="xxxxxxxx"/>
        <property name="username" value="xxxxx"/>
        <property name="password" value="xxxxx"/>
        <property name="channelCacheSize" value="25"/>
        <property name="virtualHost" value="/"/>
        <property name="port" value="3453"/>
 </bean>

 <rabbit:template id="tutorialTemplate" connection-factory="connectionFactory"/>
 <!-- 1st queue -->             
 <rabbit:queue id="veliteQueue" name="ES_queue" durable="true" auto-delete="false" exclusive="false"/>

 <rabbit:direct-exchange id="myExchange" durable="true" name="ES_exchange">
     <rabbit:bindings>
        <rabbit:binding queue="veliteQueue" key="logstash"></rabbit:binding>
     </rabbit:bindings>
 </rabbit:direct-exchange>

  <!-- 2nd Queue -->
 <rabbit:queue id="veliteQueue1" name="ES_queue_Temp" durable="true" auto-delete="false" exclusive="false"/> 

  <rabbit:direct-exchange id="myExchange1" durable="true" name="ES_exchange_temp">
    <rabbit:bindings>
        <rabbit:binding queue="ES_queue_Temp" key="logstash_temp"></rabbit:binding>
    </rabbit:bindings>
   </rabbit:direct-exchange> 
   <!-- 2 Listeners for 2 queue's mentioned above --> 
   <bean id="aListener" class="com.vzw.es.cosumer.SpringMessageListener" autowire="byName"/>
<bean id="aListener1" class="com.vzw.es.cosumer.SpringMessageListener1" autowire="byName"/>

<rabbit:listener-container id="myListenerContainer" connection-factory="connectionFactory" acknowledge="auto" prefetch="750" concurrency="1">
    <rabbit:listener ref="aListener" queues="veliteQueue"/>
    <rabbit:listener ref="aListener1" queues="veliteQueue1"/>
</rabbit:listener-container>

现在我的Java代码中有2个监听器类com.vzw.es.cosumer.SpringMessageListener和com .vzw.es.cosumer.SpringMessageListener1。现在,当我运行我的主类时,只有1个侦听器的onMessage方法被调用,即SpringMessageListener1,我确实从RabbitMQ检查了,并且队列中有足够的消息要消耗。
Spring / Rabbitmq高手可以帮我解决这个问题。此外,当我从xml注释掉第二个队列及其监听器时SpringMessageListener工作正常。

Now in my Java code I have 2 Listener classes com.vzw.es.cosumer.SpringMessageListener and com.vzw.es.cosumer.SpringMessageListener1. Now When I am running my main class only 1 listener's onMessage method is getting invoked i.e. SpringMessageListener1, I did check from RabbitMQ prespective and bothe queues have enough messages to consume. Spring / Rabbitmq master can you please help me solving this problem. Also when I comment out the 2nd queue and its listener from xml SpringMessageListener works perfectly.

推荐答案

这是容器解析器中的一个错误,每个侦听器都有自己的容器(命名空间只是指定公共属性的便捷方式)。如果删除 id =myListenerContainer,它将起作用 - 因为每个容器都会获得(不同的)生成的名称。在指定了id的情况下,两个bean都获得了相同的名称,最后一个定义替换了第一个。

It's a bug in the container parser, each listener gets its own container (the namespace is just a convenient way to specify common attributes). If you remove the id="myListenerContainer", it will work - because each container gets a (different) generated name. With the id specified, both beans get the same name, and the last definition replaces the first.

或者,声明两个单独的容器元素,具有不同的ID,每个都有只有一个听众。

Alternatively, declare two separate container elements, with different IDs, and each having just one listener.

感谢您找到这个。

请打开 JIRA问题

编辑:此问题是在版本1.2.1中得到解决

这篇关于添加多个侦听器将侦听不同的RabbitMQ队列无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆