RabbitMQ ConfirmChannel发布方法

5ssjco0h  于 7个月前  发布在  RabbitMQ
关注(0)|答案(1)|浏览(70)

我正在用JS中的“Hello World”代码测试RabbitMQ
publisher.js

var amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost:5672', function(error0, connection) {
  if (error0) {
    throw error0;
  }
  connection.createConfirmChannel(function(error1, channel) {
    if (error1) {
      throw error1;
    }
    var msg = 'Hello world';
    channel.assertExchange('exchange2', 'fanout', {
        durable: false
      });
    channel.publish('exchange2','', Buffer.from(msg), {},publishCallback);
    channel.waitForConfirms(function(err) {if (err) console.log(err); else console.log('success');})
    console.log(" [x] Sent %s", msg);
  });
});

function publishCallback(err, ok) {
    if (err !== null) {
      // Handle the error if there was a problem with publishing
      console.error('Error:', err);
    } else {
      // The message was successfully published
      console.log('Message published successfully:', ok);
    }
  }

字符串
receive.js

var amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost:5672', function(error0, connection) {
  if (error0) {
    throw error0;
  }
  connection.createChannel(function(error1, channel) {
    if (error1) {
      throw error1;
    }
    var queue = 'hello';

    channel.assertExchange('exchange2', 'fanout', {
        durable: false
      });
  
      channel.assertQueue('', {
        exclusive: true
      }, function(error2, q) {
        if (error2) {
          throw error2;
        }
        console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q.queue);
        channel.bindQueue(q.queue, 'exchange2', '');
  
        channel.consume(q.queue, function(msg) {
          if(msg.content) {
              console.log(" [x] %s", msg.content.toString());
            //   channel.nack(msg, false, false)
            }
        }, {
          noAck: false
        });
      });
    });
});


我想要的是让发布者确认消息的确认。但我的问题是,即使队列中仍然有未确认的消息,'channel.waitForConfirms'也总是打印“success”。
有人知道发生了什么吗?
我尝试在publish方法中使用回调函数,但是“err”参数为null,“ok”参数未定义,无论消息是否被确认。

2w3rbyxf

2w3rbyxf1#

Publisher Confirms:客户端发布的消息会被Broker异步确认,这意味着这些消息已经在服务器端得到处理。
未确认消息:消费者未确认消息

发布者确认是关于发布者-服务器(rabbitMQ)。未确认是关于服务器-消费者
因此,在这种情况下,服务器确认消息存储在队列中(成功),但尚未被消费者确认

相关问题