简体   繁体   English

java spring rabbit - 优雅地拒绝消息

[英]java spring rabbit - gracefully reject a message

I have the following listener method: 我有以下监听器方法:

@Override
public void onMessage(Message message, Channel channel) {
  try {
    // do something bad :)
  } catch (Exception e){
    try {
      long dt = null != message.getMessageProperties() 
          ? message.getMessageProperties().getDeliveryTag() 
          : 0;
      channel.basicReject(dt, true);
    } catch(IOException io) {
      logger.error("IO-COMMON", io);
    }
  }
}

The issue is basic reject doesn't work, I don't know why. 问题是基本拒绝不起作用,我不知道为什么。 How to reject it gracefully? 如何优雅地拒绝它? I think that if I reject a message, it should be requeued and reside is sth like cache, before going to next worker. 我认为,如果我拒绝一条消息,它应该被重新排队并且驻留就像缓存一样,然后再转到下一个工作者。 But in fact this message just seems to be lost. 但实际上这条消息似乎已经丢失了。

You need to set the acknowledgemode to MANUAL if you are doing your own acks. 如果您正在做自己的行动,则需要将确认模式设置为MANUAL。 I am not sure why it's not working for you; 我不确定为什么它不适合你; DEBUG/TRACE logging might help. DEBUG / TRACE日志记录可能有所帮助。

You should consider letting the container handle the acks - use acknowledgemode=AUTO ; 你应该考虑让容器处理ack - 使用acknowledgemode=AUTO ; the container will normally requeue the message for any exception thrown or ack it if the listener returns normally. 如果侦听器正常返回,容器通常会为抛出的任何异常重新排队消息。

You can set defaultRequeueRejected to false (it is true by default) and the message will be discarded (or routed to a DLX/DLQ). 您可以将defaultRequeueRejected设置为false(默认情况下为true),该消息将被丢弃(或路由到DLX / DLQ)。

You can also throw an AmqpRejectAndDontRequeueException to override the default mechanism of requeuing failed messages. 您还可以抛出AmqpRejectAndDontRequeueException来覆盖重新排列失败消息的默认机制。

If the ack mode is NONE - there are no acks and RabbitMQ automatically acks the message as soon as it's sent. 如果ack模式为NONE - 没有acks,RabbitMQ会在发送消息后立即自动获取消息。

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

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