rabbitMQ的发布确认,三种发布确认

rabbitMQ的发布确认,三种发布确认

 * 发布确认模式           使用的时间比较那种方式是最好的
 * 
 * 1.单个确认            发布1000个单独确认消息,耗时31920ms
 * 
 * 2.批量确认            发布1000个批量确认消息,耗时460ms
 * 
 * 3.异步确认         发布1000个批量确认消息,耗时97ms
 *                      发布1000个批量确认消息,耗时96ms
package four;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmCallback;
import utils.RabbitMqUtils;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.TimeoutException;

/**
 * 发布确认 模式      使用的时间比较那种方式是最好的
 * 1.单个确认
 * 2.批量确认
 * 3.异步确认
 */
public class ConfirmMessage {

    //批量消息的个数
    public static final int MESSAGE_COUNT = 1000;

    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
        //单个确认
        //发布1000个单独确认消息,耗时31920ms
//        publishMessageIndividually();
        //批量确认
        //发布1000个批量确认消息,耗时460ms
//        publishMessageBatch();
        //异步批量确认
        //发布1000个批量确认消息,耗时97ms
        //发布1000个批量确认消息,耗时96ms
        publishMessageAsync();

    }

    //单个确认
    public static void publishMessageIndividually() throws IOException, TimeoutException, InterruptedException {

        Channel channel = RabbitMqUtils.getChannel();
        //队列的声明
        String queueName = UUID.randomUUID().toString();
        channel.queueDeclare(queueName, true, false, false, null);
        //开启发布确认
        channel.confirmSelect();
        //开始时间
        long begin = System.currentTimeMillis();

        //批量发消息
        for (int i = 0; i < MESSAGE_COUNT; i++) {

            String message = i + "";
            channel.basicPublish("", queueName, null, message.getBytes());
            //单个消息就马上进行发布确认
            boolean flag = channel.waitForConfirms();
            if (flag) {
                System.out.println("消息发送成功");
            }
        }
        //结束时间
        long end = System.currentTimeMillis();
        System.out.println("发布" + MESSAGE_COUNT + "个单独确认消息,耗时" + (end - begin) + "ms");
    }

    //批量确认
    public static void publishMessageBatch() throws IOException, TimeoutException, InterruptedException {

        Channel channel = RabbitMqUtils.getChannel();
        //队列的声明
        String queueName = UUID.randomUUID().toString();
        channel.queueDeclare(queueName, true, false, false, null);
        //开启发布确认
        channel.confirmSelect();
        //开始时间
        long begin = System.currentTimeMillis();

        //批量发布消息的大小
        int batchSize = 100;
        //未确认的个数

        //批量发消息
        for (int i = 0; i < MESSAGE_COUNT; i++) {

            String message = i + "";
            channel.basicPublish("", queueName, null, message.getBytes());

            //判断达到100条消息的时候批量确认一次
            if (i % batchSize == 0) {
                //发布确认
                boolean flag = channel.waitForConfirms();
            }
        }
        //结束时间
        long end = System.currentTimeMillis();
        System.out.println("发布" + MESSAGE_COUNT + "个批量确认消息,耗时" + (end - begin) + "ms");
    }

    //异步确认
    public static void publishMessageAsync() throws IOException, TimeoutException, InterruptedException {

        Channel channel = RabbitMqUtils.getChannel();
        //队列的声明
        String queueName = UUID.randomUUID().toString();
        channel.queueDeclare(queueName, true, false, false, null);
        //开启发布确认
        channel.confirmSelect();
        /**
         * 线程安全 有序的哈希表 适用于高并发的情况下
         *1.轻松的将序号与消息进行map关联
         *2.轻松的批量删除条目 只要给到序号
         *3.支持高并发(多线程)
         */
        ConcurrentSkipListMap<Long, String> outstandingConfirms =
                new ConcurrentSkipListMap<>();


        //消息确认成功  回调函数
        ConfirmCallback ackCallback = (deliveryTag, multiple) -> {

            //如果是批量的删除 就全部清理掉
            if (multiple) {
                //2.删除已经确认的消息  剩下的就是未确认的消息
                ConcurrentNavigableMap<Long, String> confirmed =
                        outstandingConfirms.headMap(deliveryTag);
                confirmed.clear();
            }
            //如果是单个的删除 就全部清理掉
            else {
                outstandingConfirms.remove(deliveryTag);
            }

            System.out.println("确认的消息:" + deliveryTag);
        };
        //消息确认失败  回调函数
        /**
         * 1.消息的标记
         * 2.是否为批量确认
         */
        ConfirmCallback nackCallback = (deliveryTag, multiple) -> {
            //3.打印下未确认的消息都有哪些
            String message = outstandingConfirms.get(deliveryTag);
            System.out.println("未确认的消息是:" + message + ":::::::::未确认的标记是:" + deliveryTag);
        };
        //消息的监听器 哪些成功 哪些失败
        /**
         * 1.哪些成功
         * 2.哪些失败
         */
        channel.addConfirmListener(ackCallback, nackCallback);//异步监听

        //开始时间
        long begin = System.currentTimeMillis();
        //批量发消息
        for (int i = 0; i < MESSAGE_COUNT; i++) {

            String message = i + "";
            channel.basicPublish("", queueName, null, message.getBytes());

            //1.此处记录下所有要发送的消息
            outstandingConfirms.put(channel.getNextPublishSeqNo() - 1, message);
        }

        //结束时间
        long end = System.currentTimeMillis();
        System.out.println("发布" + MESSAGE_COUNT + "个批量确认消息,耗时" + (end - begin) + "ms");
    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值