0

There is nice property spring.rabbitmq.listener.simple.missing-queues-fatal=true it makes SimpleMessageListenerContainer and whole application fail when I set it a non existent queue - which is what I want. I don't want to have an application running in invalid state.

I can't find similar solution for exchanges like

spring.rabbitmq.listener.simple.missing-exchanges-fatal

I get in logs several

ERROR 432430 --- [ 127.0.0.1:5672] o.s.a.r.c.CachingConnectionFactory       : Shutdown Signal: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'some-non-existent-exchange' in vhost '/', class-id=40, method-id=30)

and application starts. I would like it to fail. How can I do it?

How can I make spring boot/rabbit fail when trying to bind to any of non existing queue or exchange?

bastiat
  • 1,799
  • 2
  • 19
  • 38

1 Answers1

3

The exchange does nothing with listener. We need it along side with a routing key when we produce data into AMQP. The listener has that option to fail because it is out of end-user control and starts automatically when application is ready. The exchange is used in the RabbitTemplate when you send a data. See publisherReturns option on the CachingConnectionFactory for use-case like yours to handle such an error in case of missed exchange:

https://docs.spring.io/spring-amqp/docs/current/reference/html/#cf-pub-conf-ret https://www.rabbitmq.com/confirms.html

You also add a ReturnsCallback into your RabbitTemplate to catch an unrouted message and its reason and handle such an error respectively: in your case stop the app, e.g. System.exit(1);.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Thx for reply, I will study it. How would you find idea of creating a subclass of Binding which would validate in constructor with some rabbitAdminTemplate if its queue and exchange params exists? I would like to stop my app earlier, on boot time. RabbitTemplate can be used much later. – bastiat Dec 01 '21 at 15:12
  • It is not a Binding responsibility to do that. You probably can use a RabbitMQ REST API to check for entities on the broker: https://docs.spring.io/spring-amqp/docs/current/reference/html/#management-rest-api – Artem Bilan Dec 01 '21 at 15:17
  • You can attempt to passively declare the exchange in an `ApplicationRunner` bean - if the exchange doesn't exist, throw an exception. See https://stackoverflow.com/questions/25201931/check-if-the-exchange-with-a-specified-name-exist-in-rabbitmq/25204215#25204215 – Gary Russell Dec 02 '21 at 15:43
  • Thx for help. Could you please see https://stackoverflow.com/questions/70212347/rabbitmq-cachingconnectionfactory-and-publisherreturns-configuration – bastiat Dec 03 '21 at 09:58