Search code examples
rabbitmqspring-rabbit

Queue declaration on all nodes in RabbitMQ


I have a Rabbitmq cluster setup (without HA). My consumers are spring applications and it provides a failover mechanism out of the box where it connects to the next available node.

Since the queues are not mirrored, is it okay if I declare the queues up front and when the first node goes down, the connection will be established to the second node. Does this make sense?

Another question, lets say I have a load balance on top of Rabbitmq cluster. My applications connect using the load balance. Will the queues be declared on all nodes or will it be declared on the node based on the routing strategy by LB.


Solution

  • For the first scenario, yes, the queues will be declared on the failover broker instance when the connection is established.

    If you want to pre-declare on all nodes you will need a connection factory for each node, and a RabbitAdmin for each connection factory.

    You will also need something to cause a connection to be opened on each (the RabbitAdmins register themselves as connection listeners).

    You can do that by adding a bean the implements SmartLifecycle and call createConnection() on each connection factory.

    You can also selectively declare elements. See Conditional Declaration.

    By default, all queues, exchanges, and bindings are declared by all RabbitAdmin instances (that have auto-startup="true") in the application context.

    Starting with the 1.2 release, it is possible to conditionally declare these elements. This is particularly useful when an application connects to multiple brokers and needs to specify with which broker(s) a particular element should be declared.