添加多个侦听器将侦听不同的 RabbitMQ 队列不起作用

Adding multiple listeners which will listen to different RabbitMQ queue not working(添加多个侦听器将侦听不同的 RabbitMQ 队列不起作用)
本文介绍了添加多个侦听器将侦听不同的 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 进行了检查,并且两个队列都有足够的消息来消费.

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 队列不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to implement RecyclerView with section header depending on category?(如何根据类别实现带有节标题的 RecyclerView?)
How to generate JNI header file in Eclipse(如何在 Eclipse 中生成 JNI 头文件)
Setting a custom HTTP header dynamically with Spring-WS client(使用 Spring-WS 客户端动态设置自定义 HTTP 标头)
Could you technically call the string[] anything in the main method?(从技术上讲,您可以在 main 方法中调用 string[] 吗?)
What is the proper way of setting headers in a URLConnection?(在 URLConnection 中设置标头的正确方法是什么?)
How to overwrite http-header quot;Hostquot; in a HttpURLConnection?(如何覆盖 http-header “主机在 HttpURLConnection 中?)