【发布时间】:2021-06-26 08:56:12
【问题描述】:
我正在学习 RabbitMQ (https://www.rabbitmq.com/tutorials/tutorial-two-python.html) 上的教程 #2,但我想稍微改变一下。
我希望我的队列在 100% 被消耗后自动删除,但事实并非如此。我哪里错了?
我的生产者代码:
var amqp = require('amqplib/callback_api');
const RABBIT_MQ_HOST = 'amqp://localhost'
amqp.connect(RABBIT_MQ_HOST, function(error0, connection){
if(error0){
throw error0;
}
connection.createChannel(function(error1, channel){
if(error1){
throw error1;
}
const queue = 'task_queue';
const msg = process.argv.slice(2).join(' ') || "Hello World!";
channel.assertQueue(queue, {
durable: true,
auto_delete: true,
});
channel.sendToQueue(queue, Buffer.from(msg), {
persistent: true,
});
console.log('[x] Sent %s', msg);
});
setTimeout(function() {
connection.close();
process.exit(0)
}, 500);
});
我的消费者代码:
var amqp = require('amqplib/callback_api');
const RABBIT_MQ_HOST = 'amqp://localhost'
amqp.connect(RABBIT_MQ_HOST, function(error, connection) {
connection.createChannel(function(error, channel) {
var queue = 'task_queue';
channel.assertQueue(queue, {
durable: true,
auto_delete: true,
});
channel.prefetch(1);
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", queue);
channel.consume(queue, function(msg) {
var secs = msg.content.toString().split('.').length - 1;
console.log(" [x] Received %s", msg.content.toString());
setTimeout(function() {
console.log(" [x] Done");
channel.ack(msg);
}, secs * 1000);
}, {
noAck: false
});
});
});
在启动生产者代码之后,消费者代码,最后我的队列是空的,但没有被删除。
我做错了什么?
谢谢
【问题讨论】: