Node js中来自rabbitmq的基于确认的消费消息

muk1a3rh  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(118)

我尝试在node js中使用rabbitmq的消息。它在正常消费。这里我在消费消息时做一些操作(API调用)。现在我想承认一旦API响应200状态只。否则,消息将不会双端排队。我怎么能这么做呢?提前致谢

let config = {
protocol: 'amqp',
hostname: '10.25.8.5',
username: '****',
password: '******'
};
amqp.connect(config, function(error0, connection) {
if (error0) {
    throw error0;
}
connection.createChannel(function(error1, channel) {
    if (error1) {throw error1;}

    var queue = 'test_queue';

    channel.assertQueue(queue, {
        durable: false
    });

    console.log(" [*] Waiting for messages", queue);

    channel.consume(queue, function(msg) {

        let consumedData = msg.content.toString();

        // Other process by calling api .

        console.log(" [x] Received ", consumedData);

    }, {
        noAck: true
    });
});
});

字符串

8cdiaqws

8cdiaqws1#

使用这种方式:

channel.consume(queue, (msg) => {

  callAPI(data).then((result)=>{  
        if(result.statusCode == 200)
            channel.ack(msg);
  });

}, { noAck: false });

字符串
基于你的API调用,改变result.statusCode部分,并注意noAck:false防止自动确认消息

相关问题