消息应答和持久化
优质
小牛编辑
133浏览
2023-12-01
消息应答 ack
>[danger] noAck: false 手动接收消息模式
async consume() {
const ch = await this.app.amqplib.createChannel();
await ch.assertQueue(queueName, { durable: false });
const msg = await new Promise(resolve => ch.consume(queueName, msg => resolve(msg) , { noAck: false } ));
if (msg !== null) {
// 告诉MQ 我已经收到消息并处理完成了,MQ会删除队列中的消息,并从内存中删除
ch.ack(msg);
await ch.close();
this.ctx.status = 200;
this.ctx.body = { msg: msg.content.toString() };
} else {
this.ctx.status = 500;
}
}