我想知道当我们收到 ACK 时会发生什么。我们是在单个线程中还是在多个线程中接收 ACK?
handleAck和handleNack方法是由单个线程还是多个线程使用?如果它们被单个线程使用那么就可以了。但如果它们被多个线程使用,那么我们必须以线程安全的方式构造我们的代码。
最佳答案
您不需要将 ConfirmListener
代码编写为线程安全的,但这不是因为 ack
和 nack
方法不会被调用来自多个线程,但因为您不应该在线程之间共享 Channel
。
documentation特别指出这一点:
While some operations on channels are safe to invoke concurrently, some are not and will result in incorrect frame interleaving on the wire. Sharing channels between threads will also interfere with * Publisher Confirms.
当您向代理发布时,请不要共享 channel 。 channel 是轻量级的,创建起来并不昂贵。这样你也不必担心确认。
如果您确实共享 channel ,您的确认将根据上述引用受到干扰。
关于java - Java RabbitMQ 客户端中的ConfirmListener是否必须同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39900431/