RabbitMQ--发布确认

1.发布确认

当我们把消息队列中的消息发送到消费者那里之后,我们并不知道到底消费者是否真正的接收到了生产者发送的消息,这个时候,我们就可以使用Rabbit的发布确认,来告诉生产者发布的信息是否被接受到,又或者因为RabbitMQ内部原因而导致数据丢失,这些我们都将得知

2.发布确认原理

当我们将信道设置成comfirm之后,此时我们发布的信息就会被加上一个唯一ID,一旦信息被接收boker就会告诉生产者,如果消息和队列是可持久化的,那么确认消息会在将消息写入磁盘之后发出,broker 回传给生产者的确认消息中 delivery-tag 域包含了确认消息的序列号,此外 broker 也可以设置 basic.ack 的 multiple 域,表示到这个序列号之前的所有消息都已经得到了处理。

发布确认一共有三种,分别是单个确认发布,多批确认发布和异步确认发布,其中异步确认发布性能最好。

3.单个确认发布

这是一种简单的确认方式,它是一种同步确认发布的方式,也就是发布一个消息之后只有它被确认发布,后续的消息才能继续发布,waitForConfirmsOrDie(long) 这个方法只有在消息被确认的时候才返回,如果在指定时间范围内这个消息没有被确认那么它将抛出异常。

这种确认方式有一个最大的缺点就是:发布速度特别的慢,因为如果没有确认发布的消息就会阻塞所有后续消息的发布,这种方式最多提供每秒不超过数百条发布消息的吞吐量。当然对于某些应用程序来说这可能已经足够了。

public class ComFirmMessage {

    public static final int MESSAGE_COUNT=1000;

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        SingleBatchProcessing();//单批处理耗时757ms
       
    }

    private static void SingleBatchProcessing() throws IOException, TimeoutException, InterruptedException {
        Channel channel = RabbitMQUtils.getChannel();
        //队列声明
        String queue_name = UUID.randomUUID().toString();
        channel.queueDeclare(queue_name, false, false, false, null);
        //开启确认通知
        channel.confirmSelect();
        long begin = System.currentTimeMillis();
        for (int i = 0; i < MESSAGE_COUNT; i++) {
            String message = i + "";
            channel.basicPublish("", queue_name, null, message.getBytes());
            boolean flag = channel.waitForConfirms();
            if (flag) {
                System.out.println("发送成功");
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("发送" + MESSAGE_COUNT + "条单独确认消息,耗时为" + (end - begin) + "ms");
    }
}

4.多批确认发布

单个确认发布方式非常慢,与单个等待确认消息相比,先发布一批消息然后一起确认可以极大地提高吞吐量,当然这种方式的缺点就是:当发生故障导致发布出现问题时,不知道是哪个消息出问题了,我们必须将整个批处理保存在内存中,以记录重要的信息而后重新发布消息。当然这种方案仍然是同步的,也一样阻塞消息的发布。

public class ComFirmMessage {

    public static final int MESSAGE_COUNT=1000;

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        SingleBatchProcessing();//单批处理耗时757ms
        MultiBatchProcessing();//多批处理耗时136ms


    } 
   private static void MultiBatchProcessing() throws IOException, TimeoutException, InterruptedException {
        Channel channel = RabbitMQUtils.getChannel();
        String queue_name=UUID.randomUUID().toString();
        channel.queueDeclare(queue_name,false,false,false,null);
        //开启确认通知
        channel.confirmSelect();
        long begin=System.currentTimeMillis();
        int Batch_Count=100;//多批一次处理数量
        for (int i = 0; i < MESSAGE_COUNT; i++) {
            String message=i+" ";
            channel.basicPublish("",queue_name,null,message.getBytes());
            if((i+1)%Batch_Count==0){
                channel.waitForConfirms();
            }

        }
}

5.异步发布确认

单个和多批发布确认的处理方式都是,在上一个或者上一批信息发送再返回之后再发送下一个,而异步发布确认的最大优势就在于,等发送完上一个信息之后,无需等待,直接发送下一个数据,通过这样的方式就能够提高性能

public class ComFirmMessage {

    public static final int MESSAGE_COUNT=1000;

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        SingleBatchProcessing();//单批处理耗时757ms
        MultiBatchProcessing();//多批处理耗时136ms
        AsyProcessing();//异步处理耗时88ms


    }
 private static void MultiBatchProcessing() throws IOException, TimeoutException, InterruptedException {
        Channel channel = RabbitMQUtils.getChannel();
        String queue_name=UUID.randomUUID().toString();
        channel.queueDeclare(queue_name,false,false,false,null);
        //开启确认通知
        channel.confirmSelect();
        long begin=System.currentTimeMillis();
        int Batch_Count=100;//多批一次处理数量
        for (int i = 0; i < MESSAGE_COUNT; i++) {
            String message=i+" ";
            channel.basicPublish("",queue_name,null,message.getBytes());
            if((i+1)%Batch_Count==0){
                channel.waitForConfirms();
            }

        }
        long end=System.currentTimeMillis();
        System.out.println("发送" + MESSAGE_COUNT + "条多批确认消息,耗时为" + (end - begin) + "ms");
    }

    private static void AsyProcessing() throws IOException, TimeoutException, InterruptedException {
        Channel channel = RabbitMQUtils.getChannel();
        String queue_name=UUID.randomUUID().toString();
        channel.queueDeclare(queue_name,false,false,false,null);
        //开启发布确认
        channel.confirmSelect();
        /**
         * 线程安全有序的一个哈希表,适用于高并发的情况下
         * 1.轻松的将序号与消息进行关联
         * 2.轻松批量删除条目 只要给到序号
         * 3.支持高并发(多线程)
         */
        ConcurrentSkipListMap<Long,String> outstandingConfirms=
                new ConcurrentSkipListMap<>();
        long begin=System.currentTimeMillis();
        //消息确认回调的函数
        ConfirmCallback ackCallBack=(deliverTag, multiple)->{
            if(multiple) {
                //2.删除掉已经确认的消息 剩下的就是未确认的消息
                ConcurrentNavigableMap<Long, String> confirmed =
                        outstandingConfirms.headMap(deliverTag);
                confirmed.clear();
            }else {
                outstandingConfirms.remove(deliverTag);
            }
            System.out.println("确认的消息:"+deliverTag);
        };
        /*
        * 1.消息的标记
        * 2.是否为批量处理
        * */
        //消息确认失败回调函数
        ConfirmCallback nackCallback =(deliverTag,multiple)->{
            //3.打印一下未确认的消息都有哪些
            String message = outstandingConfirms.remove(deliverTag);
            System.out.println("未确认的消息"+deliverTag);
        };
        //开启异步处理
        channel.addConfirmListener(ackCallBack,nackCallback);
        for (int i = 0; i < MESSAGE_COUNT; i++) {
            String message=i+" ";
            channel.basicPublish("",queue_name,null,message.getBytes());
            //1.此处记录下所有要发送的消息 消息的总和
            outstandingConfirms.put(channel.getNextPublishSeqNo(),message);
        }
        long end=System.currentTimeMillis();
        System.out.println("发送" + MESSAGE_COUNT + "条异步确认消息,耗时为" + (end - begin) + "ms");
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值