如何使用node-amqp获取队列中的消息数

Mik*_*e P 4 amqp rabbitmq node.js

我正在使用node-amqp作为节点应用程序中的排队系统.我希望能够监视队列的状态,以确定我们是否有足够的工作人员运行,即如果队列大小增加,我们知道我们开始落后.

我知道从命令行你可以使用类似的东西;

rabbitmqctl list_queues
Run Code Online (Sandbox Code Playgroud)

这给了我所需的确切信息,但我想知道是否还有从node-amqp本身做到这一点?

提前致谢.

编辑

最后,我只是使用命令行工具rabbitmqctl来获取我需要的信息,这不是一个很好的解决方案,但这就是我所做的;

var Logger = require('arsenic-logger');

getQueueMeta(function(info){
    Logger.info(info);
});

/**
* Returns a sparse array with the queue names as the indices
* and the number of messages as the value, e.g.;
*
* info = [ my-queue: 9, my-other-queue: 1 ]
* 
* @param callback
*/
function getQueueMeta(callback){

    var sys = require('sys')
    var exec = require('child_process').exec;

    exec("/usr/local/sbin/rabbitmqctl list_queues", function(error, stdout, stderr) {

        var info = [];

        if (!error){

            var lines = stdout.split(/\n/);

            if (lines.length > 1){

                for (var i=1; i<lines.length-2; i++){
                    var temp = lines[i].split(/\s/);
                    info[temp[0].trim()] = parseInt(temp[1]);
                }

            }

        }

        callback(info);

    });

}
Run Code Online (Sandbox Code Playgroud)

jko*_*icz 7

我可能有点迟到了,但如果将来有人遇到这个问题...在当前版本的node-amqp(0.2.4)中,您可以在声明时检查消息计数和当前订阅的消费者数量一个队列.

var connection = require("amqp").createConnection();
connection.exchange("exampleExchange", {/*...*/}, function(exchange) {
  connection.queue("exampleQueue", {/*...*/}, function(queue, messageCount, consumerCount){
    console.log("Message count", messageCount);
    console.log("Consumer count", consumerCount);
  });
});
Run Code Online (Sandbox Code Playgroud)