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



我正试图在节点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
});
});
});

使用这种方式:

channel.consume(queue, (msg) => {
callAPI(data).then((result)=>{  
if(result.statusCode == 200)
channel.ack(msg);
});
}, { noAck: false });

根据您的api调用,更改result.statusCode部分,并注意noAck:false以防止自动确认消息

最新更新