Search code examples
javaspring-bootrabbitmqspring-amqpcircuit-breaker

How to 'check' before picking off the Amqp/ RabbitMQ Queue (Spring AMQP)


Scenario: A microservice picks up a message from a RabbitMQ Queue, it's converted to an object and then the microservice makes a REST call to external service.

It's going to deal with thousands 'n thousands of these messages, is there a way of telling my consumer not to pick up a message off the Queue if we know the external Rest service is down?

I know I can do retries for an individual message once it's picked up, but I dont want to even pick it up if I know its down. I dont want to deal with thousands of messages in DLQ.

Also it feels like a Circuit Breaker design pattern, but I cant find any specific examples of how to implement it with AMQP.

Extra info: SpringBoot app, taking to RabbitMQ using spring amqp.

Thanks in advance


Solution

  • You can stop and start the message listener container.

    If you are using discrete containers you can stop/start the container bean.

    If you are using @RabbitListener, provide an id attribute and wire in the RabbitListenerEndpointRegistry bean.

    Then registry.getMessageListenerContainer(myId).stop();.