Search code examples
javaxmlspring-amqpspring-rabbit

How to configure RabbitMQ in XML file to have 1) listener listening from multiple queues 2) 2 listeners listening to same queue?


I am trying to understand Spring RabbitMQ XML configuration. So far, I never came across xml configuration where a listener is listnening(subscribed) to multiple queues. Like wise, I never found codes wherein 2 listeners are subscribed to the same queue in the same xml file. After going through the XSD documentation where in I found that the attribute- "queue-names" is a comma separated list of queue names. So, I believe xml configuration can also be like -

<rabbit:queue id="springQueue1" name="spring.queue1" auto-delete="true" durable="false"/>

<rabbit:queue id="springQueue2" name="spring.queue1" auto-delete="true" durable="false"/>


<rabbit:listener-container connection-factory="connectionFactory">
    <rabbit:listener queues="springQueue1, springQueue2" ref="messageListener"/>
</rabbit:listener-container>

<bean id="messageListener" class="com.ndpar.spring.rabbitmq.MessageHandler"/>

Is this correct? Is the syntax correct in "queues" attribute?? Also, if two listeners are receiving from same queue, then

<rabbit:listener-container connection-factory="connectionFactory">
   <rabbit:listener queues="springQueue1" ref="messageListener1"/>
   <rabbit:listener queues="springQueue1" ref="messageListener2"/>
</rabbit:listener-container>
<bean id="messageListener1" class="com.ndpar.spring.rabbitmq.MessageHandler1"/>
<bean id="messageListener2" class="com.ndpar.spring.rabbitmq.MessageHandler2"/>

Is this correct?? The "ref" attribute always refers to bean id??? or can it refer to bean name?? Is this valid -

<rabbit:listener queues="springQueue1" ref="messageListener1, messageListener2"/>

Please help.Thanks.


Solution

  • Is this correct? Is the syntax correct in "queues" attribute??

    Yes, that is correct; a single consumer consumes from 2 queues.

    <rabbit:listener-container connection-factory="connectionFactory">
       <rabbit:listener queues="springQueue1" ref="messageListener1"/>
       <rabbit:listener queues="springQueue1" ref="messageListener2"/>
    </rabbit:listener-container>
    

    It's functionally the same as a single listener with concurrency="2" - in that case, the container will create 2 consumers against the queue. In your case, we'll get 2 containers, each with one consumer.

    The ref has to reference a single bean id. For normal <bean/>s, name is a synonym for id.

    This is invalid:

    <rabbit:listener queues="springQueue1" ref="messageListener1, messageListener2"/>