Search code examples
springspring-bootrabbitmqspring-amqpspring-rabbit

Spring RabbitMQ Connection and Resource Management Issues


I need to consume messages from Rabbit HA cluster via HAProxy so I switched to CacheMode.CONNECTION as it is recommended in spring-amqp-documentation. Moreover I need to consume messages from many queues so I create for each queue SimpleMessageListenerContainer with 4 concurrent consumers and I have few questions:

  1. After few tests it looks that my approach is not efficient because each time when a new queue is added also new SimpleMessageListenerContainer is created with 4 threads. So I could set more queues for the given SimpleMessageListenerContainer what looks more efficient but maybe there is another better way?
  2. Why when I have switched to CacheMode.CONNECTION for each consumer in SimpleMessageListenerContainer is created new connection? Can I set in some way one connection for all consumers in a given SimpleMessageListenerContainer or maybe it is not recommended?
  3. How to handle exception

    "org.springframework.amqp.rabbit.connection.AutoRecoverConnectionNotCurrentlyOpenException: Auto recovery connection is not currently open"

    I received it when one RabbitMQ node is down. Even when node is up again SimpleMessageListenerContainer cannot reconnect.

Thanks in advance for help.


Solution

    1. The upcoming 2.0 release has a new DirectMessageListenerContainer that shares threads across containers documentation here. The 2.0.0.M4 milestone is available now; the GA release is expected to be mid July.

    2. If you want a single connection per container use the default cache mode and a separate connection factory for each container.

    3. Disable the client's connection factory auto recovery mechanism; it is enabled by default in the 4.x client; Spring AMQP has its own recovery mechanism that will generally recover faster. Since version 1.7.1 Spring AMQP disables it by default, unless you configure your own Rabbit ConnectionFactory.