• rabbitMQ生产者的异步的异步批量发布确认demo


    
    
        //批量发布异步确认
        public static void publishSync() throws Exception {
             //创建链接工厂
            ConnectionFactory connectionFactory = new ConnectionFactory();
    
            //设置链接
            connectionFactory.setHost("192.168.43.37");
            connectionFactory.setUsername("admin");
            connectionFactory.setPassword("admin");
    
            //链接工厂创建链接
            Connection connection = connectionFactory.newConnection();
    
            //获取信道
            Channel channel = connection.createChannel();
    
            //信道声明
            String queueName = UUID.randomUUID().toString();
            channel.queueDeclare(queueName, true, false, false, null);
    
            //开启发布确认
            channel.confirmSelect();
    
    
            /**
             * 线程安全有序的一个哈希表 适用于高并发的情况下
             *
             * 1.轻松的将需要与消息进行关联
             * 2.轻松的批量删除条数
             * 3.支持高并发
             */
            ConcurrentSkipListMap<Long,String> outstandingConfirms = new ConcurrentSkipListMap<>();
    
    
    
            //确认监听器
            ConfirmCallback confirmCallback = ( deliveryTag,multiple)->{
                System.out.println("消息确认监听器成功确认:" +  deliveryTag);
                //删除发送过的消息
                if(multiple){
                    //批量的删除确认的消息 剩下的是未确认的
                    ConcurrentNavigableMap<Long,String> confirmed =
                            outstandingConfirms.headMap(deliveryTag);
                    confirmed.clear();
    
                }else {
                    outstandingConfirms.remove(deliveryTag);
                }
    
            };
    
            ConfirmCallback nconfirmCallback= ( deliveryTag,multiple)->{
    
                //未确认的消息
                String message = outstandingConfirms.get(deliveryTag);
                System.out.println("未确认的消息:"+deliveryTag +"-"+message);
    
            };
    
            channel.addConfirmListener(confirmCallback,nconfirmCallback);
    
    
            //开始时间
            long begin = System.currentTimeMillis();
            for (int i = 0; i < MESSAGE_COUNT; i++) {
                String mes = i + "";
                channel.basicPublish("",queueName,null,mes.getBytes("UTF-8"));
                outstandingConfirms.put(channel.getNextPublishSeqNo(),mes);
    
                //记录所有发布的消息
            }
    
            long end = System.currentTimeMillis();
            System.out.println("发布"+MESSAGE_COUNT+"条用时共计:" + (end-begin) );
    
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
  • 相关阅读:
    三、线程/进程模型
    Elasticsearch 集群和分片使用
    libbpf-tools编译和使用步骤
    前端基础(四十二):SVG入门
    Swift宏
    leetcode做题笔记145. 二叉树的后序遍历
    redis 主从复制
    macOS 15 beta (24A5264n) Boot ISO 原版可引导镜像下载
    MySQL SQL语句与事务执行及日志分析
    bmp图片处理
  • 原文地址:https://blog.csdn.net/qq_17040587/article/details/134420833