使 Spring RabbitMQ 在缺少交换时失败

Make Spring RabbitMQ fail on missing exchange

有不错属性spring.rabbitmq.listener.simple.missing-queues-fatal=true 当我将它设置为不存在的队列时,它会使 SimpleMessageListenerContainer 和整个应用程序失败——这正是我想要的。我不想让应用程序 运行 处于无效状态。

我找不到类似的交换解决方案,例如

spring.rabbitmq.listener.simple.missing-交换-致命

我得到了几个日志

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)

应用程序启动。我希望它失败。我该怎么做?

如何在尝试绑定到任何不存在的队列或交换时使 spring boot/rabbit 失败?

交换对监听器没有任何作用。当我们将数据生成到 AMQP 中时,我们需要它和路由键。侦听器可以选择失败,因为它不受最终用户控制,并在应用程序准备就绪时自动启动。当您发送数据时,交换在 RabbitTemplate 中使用。请参阅 CachingConnectionFactory 上的 publisherReturns 选项,了解像您这样的用例,以在错过交换的情况下处理此类错误:

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

您还可以在 RabbitTemplate 中添加 ReturnsCallback 以捕获未路由的消息及其原因并分别处理此类错误:在您的情况下停止应用程序,例如System.exit(1);.