[英]How can I force a RabbitMQ broker to NACK a message for testing?
I'm working on a Spring Boot project using RabbitMQ via Spring AMQP. 我正在通过Spring AMQP使用RabbitMQ开发Spring Boot项目。 We'd like to be able to test our app's behavior when a message publish is NACK'd (specifically when the
ack
argument of PublisherConfirmCallback#confirm
is false
). 当消息发布为NACK时(特别是当
PublisherConfirmCallback#confirm
的ack
参数为false
),我们希望能够测试应用程序的行为。 However we're having a hard time trying to set up this scenario in an integration test (that is, with the app running, not a unit test). 但是,我们很难尝试在集成测试中设置此场景(即,应用程序正在运行,而不是单元测试)。
Just to be clear, we're testing around publisher acknowledgements, not consumer acknowledgements. 为了清楚起见,我们正在测试发布者的确认,而不是消费者的认可。
We know you can get access to a channel with the RabbitTemplate's execute
method, but our RabbitMQ auto-acks, so the message is already gone by the time the channel callback is executed. 我们知道您可以使用RabbitTemplate的
execute
方法访问通道,但是我们的RabbitMQ自动execute
,因此在执行通道回调时消息已经消失。 We're thinking we might be able to either disable auto-ack at runtime (so as not to interrupt the test suite) or to simply block the channel and receive the message on it manually in the test, but we can't figure out how to do either of these. 我们认为我们可以在运行时禁用auto-ack(以免中断测试套件)或者只是阻止通道并在测试中手动接收它,但是我们无法弄清楚怎么做其中任何一个。 But really, any way to cause a NACK during a test would be great.
但实际上,在测试过程中任何导致NACK的方法都会很棒。
tl;dr how to nack a message from client code tl; dr如何从客户端代码中取消消息
you will have to turn off auto-ack, to start with. 你将不得不关闭自动确认,开始。 with that turned on, it will not be possible to nack a message.
打开它后,就无法收到消息。
after that, you're test setup should include a message consumer that is coded specifically to do what you want, when you want... don't use your real code to test the nack handling. 在那之后,你的测试设置应该包括一个消息使用者,它被专门编写为你想要的,当你想要的时候......不要使用真实的代码来测试nack处理。 set up a consumer that only does a nack... or one that knows when it receives certain messages, it should nack
设置一个只做一个nack的消费者......或者知道什么时候收到某些消息的消费者,它应该是nack
将消息发布到不存在的交换将导致发布者确认回调以接收nack!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.