rabbitMQ重复消费(结合死循环重发那一篇看)

/**
 * 重复消费逻辑判断与处理
 */
@Component
public class RepeatMqConsumer {
    /**
     * 服务对象
     */
    private  int count=1;
    @Autowired
    private DispatcherService dispatcherService;

    @RabbitHandler
    @RabbitListener(queues ="dead.direct.queue0")
    public  void messageconsumer(String msg, Channel channel, Envelope envelope, CorrelationData correlationData, @Header(AmqpHeaders.DELIVERY_TAG)long tag) throws IOException {
        if(msg.contains("拒收")){
            //绝收消息
            //requeue:是否重新入队列,true是,false :直接丢弃
            //只有一个消费者时requeue为true会造成重复消费
            channel.basicReject(envelope.getDeliveryTag(),false);
        }else if(msg.contains("异常")){
            //只有一个消费者时requeue为true会造成重复消费
            //requeue重发  fasle 不会重发,会把消息打入死信队列(建立一个死信队列)    true会进入死循环的重发,建议true的情况下,不使用try  catch 否则造成循环
            channel.basicNack(envelope.getDeliveryTag(),true,false);
        }else{
            //手动应答
            //如果不应答,队列中的消息会一直存在,重新连接时会重复消费
            channel.basicAck(tag,true);
        }
    }

}
一点点学习,一丝丝进步。不懈怠,才不会被时代淘汰
原文地址:https://www.cnblogs.com/wangbiaohistory/p/14631010.html