问题描述
我确实阅读了此参考:https://www.rabbitmq.com/dlx.html,但它并没有解决我的疑问,即:
如果接受消息没有问题 - spring-rabbitmq
发送确认并且一切都很好,DLX
不知道确认的消息.
I did read this reference: https://www.rabbitmq.com/dlx.html, however it doesn't resolve my doubts, namely:
In case of accepting message there is no problem - spring-rabbitmq
send ack and everthing is fine, DLX
doesn't know about acked message.
问题是如果拒绝回答,即抛出 MessageConverterException
怎么办?此消息已删除或移至 DLX
?
The problem is in case rejecting answer, namely what about throwing MessageConverterException
? This message is removed or moved to DLX
?
如果有其他异常怎么办?例如 Exception
?它被删除/重新排队/移动到 DLX
?
And what about in case other exception ? For example Exception
? It is removed/requeued/moved to DLX
?
@Gary 回答后编辑
我认为,在回答@Gary 之后,我应该添加更多关于我的案例的详细信息以及@Gary 回答的一些摘要.@Gary 完全掌握了我的用例.
Edit after answer of @Gary
I think, that after answer's @Gary I should add more details about my case and some summary of @Gary's answer. @Gary exactly grasped my use case.
我不喜欢重新排队 - 从不(我害怕循环),但我不希望在抛出异常时丢失消息(例如失去与数据库的连接) - 此消息应该重新发送到 DLX代码>.另一方面,消息的转换应该被视为致命错误 - 没有重新排队,没有重新发送到 DLX - 只是永久删除消息.通常,取决于异常拒绝(=重新发送到 DLX,如果已配置)或接受,从不重新排队.
I wouldn't like requeue - never (I am afraid of looping), but I wouldn't like to lose messages when an exception was thrown (for example lost connection to database) - this message should be resend to DLX
. On the other hand, conversion of message should be treated as fatal error - no requeue, no resend to DLX - simply permanent removing message. Generally, in depends on exception either reject (=resend to DLX if configured) or accept, never requeue.
简而言之,@Gary 提出的方法.
首先:我们可以重写ExceptionHandler
来管理发送nack/ack,这给了我们一个完全的控制权.
第二:IMO更简单,解决方案是设置defaultRequeueRejected=false
并在转换器中抛出ImmediateAcknowledgeAmqpException
.它使 RabbitMQ
认为答案已被接受(与第一个解决方案的情况相同),而且不会调用侦听器.来自您的转换器.
If you want to DLX/DLQ all business exceptions but drop conversion exceptions, throw AmqpRejectAndDontRequeueException
(or set defaultRequeueRejected
to false), and throw ImmediateAcknowledgeAmqpException
from your converter.
这篇关于rabbitmq 和 spring-rabbitmq 中的 DLX - 拒绝消息的一些注意事项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!