一个通道消耗多个队列

时间:2018-09-16 06:13:29

标签: node.js rabbitmq

我使用RabbitMq来管理和使用队列。我有多个队列。它们的数量不是特定的。我使用直接交换来发布消息。 我如何仅使用一个即可消耗每个队列的所有消息(基于routing_key) 渠道? 目前,我假设我有5个队列。我已经用于循环并为每个队列创建一个通道。像这样:

 stuff=["shoes","pants","hats","jewels","glasses"];

  stuff.forEach(cnt => 
    {
        var ex = 'stuff';
        var cq=cnt;

        amqp
        .connect('amqp://localhost')
        .then(conn => conn.createChannel())
        .then(ch => {

            ch.assertExchange(ex, 'x-delayed-message', { durable: true, 
     arguments: { 'x-delayed-type': 'direct' } })
     return ch
             .assertQueue(cq, { durable: true })
             .then(() =>  {   ch.bindQueue(cq, ex, cq)  /*second cq is routing*/  
                 })
                  .then(() => {
                  ch.consume(cq, (msg) =>
                  {

                   console.log("['%s']  '%s'",cq, msg.content.toString()); 
                   if( msg.content.toString()!=null)
                   console.log(cq);

                          reciveMSG=JSON.parse(msg.content.toString());

                    }, { noAck: true });
               }); 
         }) 


     });

但是我只想通过一个频道来做。因为它更乐观并且使用更少的内存(我不知道它是不是!)。是否有一种方法可以处理队列的不确定计数?

1 个答案:

答案 0 :(得分:2)

每个队列必须使用一个通道来使用消息。这是AMQP规范的一部分,无法解决。


注意: RabbitMQ团队监视the rabbitmq-users mailing list,并且有时仅在StackOverflow上回答问题。

相关问题