【问题标题】:How to get number of messages in queue with node-amqp如何使用 node-amqp 获取队列中的消息数
【发布时间】:2013-12-12 17:35:24
【问题描述】:

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

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

rabbitmqctl list_queues

这为我提供了我需要的确切信息,但我想知道是否可以从 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);

    });

}

【问题讨论】:

    标签: node.js rabbitmq amqp


    【解决方案1】:

    我参加聚会可能有点晚了,但万一将来有人偶然发现这个问题...在当前版本的 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);
      });
    });
    

    【讨论】:

    • 不知道为什么,但我使用的是 0.5.2 版本,它没有公开这些属性。
    • @HoangTrinh 抱歉,NPM 上的 node-amqp 库曾经指向这个存储库,我的评论指的是它:github.com/postwait/node-amqp
    • 这行得通,但我需要在队列选项中设置“被动:真”。
    【解决方案2】:

    所以 RabbitMQ 支持 AMQP 到 0.9.1。可以直接here看文档。

    快速浏览文档会发现 AMQP 仅涵盖连接、交换和基本排队/出队等内容。然而,rabbitmqctl 中的许多功能根本无法通过 AMQP 规范访问。这样rabbitmqctl 就更接近于“管理”工具,其中 AMQP 连接基本上是“消费者”工具。

    也就是说,你可能很幸运。看看connection.queue()method。

    var q = connection.queue('my-queue', function (queue) {
      console.log('Queue ' + queue.name + ' is open');
    });
    

    在 .NET 实现中,queue 变量看起来不仅包括名称,还包括消费者和消息的计数。我不清楚node.js中有什么可用的,您可能需要深入研究。

    【讨论】:

    • 谢谢,查看 node-amqp 的源代码并检查队列对象,遗憾的是没有消息或消费者计数(我可以看到)
    • 是的,我查看了 node-AMQP 中的代码,但没有填充。也就是说,如果 .NET 和 Java 库能够提取它,那么数据很可能存在并且根本没有被公开。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 2017-09-07
    • 2020-11-17
    • 1970-01-01
    相关资源
    最近更新 更多