简体   繁体   English

消费者问题中的春兔异常

[英]spring rabbit exception in consumer issue

I have a spring rabbit consumer: 我有一个春兔消费者:

public class SlackIdle1Consumer extends AbstractMessageConsumer {

    @Override public void process(Message amqpMessage, Channel channel)
            throws Exception {

    /*very bad exception goes here. 
it causes amqp message to be rejected and if no other consumer is available and error 
still persists, the message begins looping over and over.
And when the error is fixed,
those messages are being processed but the result of this procession may be harmful. 
     */

        }
    }
}

And somewhere inside an exception happens. 并且异常发生在某处。 Lets imagine this is a bad exception - development logic error. 让我们想象这是一个错误的例外 - 开发逻辑错误。 So amqp message begins to spin indefinitely, and when error is fixed and consumer restarted, all old messages are being processed, and it's bad, because logic and data may change since those messages were sent. 所以amqp消息开始无限期地旋转,当错误被修复并且消费者重新启动时,所有旧消息都被处理,这很糟糕,因为逻辑和数据可能会因为发送这些消息而改变。 How to handle it properly? 如何妥善处理?

So the question is: how to fix this situation properly? 所以问题是:如何正确解决这种情况? Should I wrap all my code to try-catch clause or will I have to develop 'checks' in each consumer to prevent consistency issues in my app? 我应该将所有代码包装到try-catch子句中,还是必须在每个使用者中开发“检查”以防止我的应用程序出现一致性问题?

There are several options: 有几种选择:

  1. Set the container's defaultRequeueRejected property to false so failed messages are always rejected (discarded or sent to a dead letter exchange depending on queue configuration). 将容器的defaultRequeueRejected属性设置为false因此始终拒绝失败的消息(丢弃或发送到死信交换,具体取决于队列配置)。

  2. If you want some exceptions to be retried and others not, then add a try catch and throw an AmqpRejectAndDontRequeueException to reject those you don't want retried. 如果你想要重试一些异常而不想重试其他异常,那么添加一个try catch并抛出一个AmqpRejectAndDontRequeueException来拒绝那些你不想重试的AmqpRejectAndDontRequeueException

  3. Add a custom ErrorHandler to the container, to do the same thing as #2 - determine which exceptions you want retried - documentation here . 将自定义ErrorHandler添加到容器中,以执行与#2相同的操作 - 确定要重试的异常 - 此处提供文档

  4. Add a retry advice with a recoverer - the default recoverer simply logs the error, the RejectAndDontRequeueRecoverer causes the message to be rejected after retries are exhausted, the RepublishMessageRecoverer is used to write to a queue with additional diagnostics in headers - documentation here . 使用recoverer添加重试建议 - 默认的recoverer只记录错误, RejectAndDontRequeueRecoverer导致在重试耗尽后拒绝消息, RepublishMessageRecoverer用于写入带有标题中其他诊断的队列 - 此处为文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM