rabbitmq-ConfirmListener

当broker开启confirm时,可以添加此监听器。

public void testWaitForConfirms()
        throws IOException, InterruptedException
    {

         // 创建Channel时开启:channel.confirmSelect();

 

        // 记录客户端发送消息的序号1、2、3、4、。。。
        final SortedSet<Long> unconfirmedSet = Collections.synchronizedSortedSet(new TreeSet<Long>());
        channel.addConfirmListener(new ConfirmListener() {
                public void handleAck(long seqNo, boolean multiple) {
                    if (!unconfirmedSet.contains(seqNo)) {
                        fail("got duplicate ack: " + seqNo);
                    }

                    // 这里multile=true时,我理解为broker批量发送ack确认。

                    // 客户端就把此次消息序号之前的记录全删除,代表这个消息值钱的消息broker都已接收到
                    if (multiple) {
                        unconfirmedSet.headSet(seqNo + 1).clear();
                    } else { // broker单个返回ack确认
                        unconfirmedSet.remove(seqNo);
                    }
                }

                public void handleNack(long seqNo, boolean multiple) {
                    fail("got a nack");
                }
            });

        for (long i = 0; i < NUM_MESSAGES; i++) {
            unconfirmedSet.add(channel.getNextPublishSeqNo());
            publish("", "confirm-test", true, false);
        }

        channel.waitForConfirmsOrDie();
        if (!unconfirmedSet.isEmpty()) {
            fail("waitForConfirms returned with unconfirmed messages");
        }
    }

你可能感兴趣的:(rabbitmq-ConfirmListener)