1.首先我对auto delete的理解:
队列自动删除的条件,有消息者订阅本队列,然后所有消费者都解除订阅此队列,autoDelete=true时,此队列会自动删除,即使此队列中还有消息。
2.在rabbitmq管理页面:发现队列持续增长,没有Consumers的队列也在里面。
3.排查问题: 项目代码采用node 的node-amqp, 通过测试发现, 使用rabbitmq的方式是: 链接–创建交换器–创建队列–绑定交换器–订阅
但是如果在订阅时失败(我是把这段代码注释)的话导致这个队列从来没有过消费者, auto delete 就不会删除自己。
4.也就是说auto delete的队列必须是有过消费者并且所有消费者都解除订阅了,才会auto delete
参考的代码:
'use strict';
var amqp = require('amqp');
var corrID = 0;
var clientQueue;
var connection;
var exc;
connection = amqp.createConnection({ host: '127.0.0.1' });
var rpc = function() {
connection.on('ready', function () {
console.log('Connected to rabbitMQ server');
//Create a direct exchange
exc = connection.exchange('exc_test', {type: 'direct'}, function (exchange) {
console.log('Exchange ' + exchange.name + ' is open');
//Create the queue for send messages 订阅消息
clientQueue = connection.queue('', function (q) {
console.log('ClientQueue ' + q.name + ' is open');
clientQueue.bind('exc_test', clientQueue.name);
clientQueue.subscribe(function (message) { //订阅队列的过程失败, 导致队列从来没有过消费者, 所以队列不会被删除
//console.log("receive message", message);
var time = new Date().getTime();
message.times.push(time);
var duration = message.times[2]-message.times[0];
console.log("duraion",duration);
console.log("times", message.times)
});
// 创建队列成功后
var time = new Date().getTime();
corrID += 1;
var message = {times:[time], corrID: corrID, replyTo: clientQueue.name};
exc.publish("q_test", message);
});
});
});
};
rpc();