問題描述
我有以下 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 進行了檢查,并且兩個隊列都有足夠的消息來消費.
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.
此外,當我從 xml SpringMessageListener 注釋掉第二個隊列及其偵聽器時,效果也很好.
Also when I comment out the 2nd queue and its listener from xml SpringMessageListener works perfectly.
推薦答案
這是容器解析器的一個bug,每個監聽器都有自己的容器(命名空間只是一種方便的方式來指定公共屬性).如果您刪除 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 隊列不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!