Rabbitmq确认监听器不起作用

时间:2014-07-30 12:39:49

标签: java rabbitmq

我正在尝试使用ConfirmListener,但执行永远不会到达其方法。我是这样做的:

    channel.addConfirmListener(new ConfirmListener() {

        public void handleNack(long deliveryTag, boolean multiple) throws IOException {
            System.out.println("Not ack received");
        }

        public void handleAck(long deliveryTag, boolean multiple) throws IOException {
            System.out.println("Ack received");
        }
    });

    channel.exchangeDeclare(directExchangeName, directExchangeType, DURABLE, AUTO_DELETE, arguments);

    channel.queueBind(directQueueName, directExchangeName, routingKey);

    // AUTO_ACK = false
    channel.basicConsume(directQueueName, AUTO_ACK, routingKey, directConsumer);

然后我用这个发表:

        channel.basicPublish(directExchangeName, routingKey, MANDATORY, properties, message.getBytes());

后来我用这个消费了:

            @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

            // Send ack
            channel.basicAck(envelope.getDeliveryTag(), false);
        }

我做错了什么?

谢谢,欢呼。

1 个答案:

答案 0 :(得分:5)

呼叫 channel.confimSelect(); 启用发布者确认。

但是我想知道你是否也希望在消费者处理消息时调用addConfirmListener。用于发布的ConfirmListener在接受消息时由服务器触发,它与消费者的确认无关。