zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    
    }
    
    一点点学习,一丝丝进步。不懈怠,才不会被时代淘汰
  • 相关阅读:
    springmvc视图解析
    mysql外键是多个id组成的字符串,查询方法
    mysql服务无法启动(1067错误)时数据备份的经验
    springboot(5) freemarker
    springboot(4) Log之Logbak
    springboot(3) junit单元测试
    集合类基础知识
    springboot(2) 数据库操作
    springboot(1)
    linux命令
  • 原文地址:https://www.cnblogs.com/wangbiaohistory/p/14631010.html
Copyright © 2011-2022 走看看