m.eslampnah
m.eslampnah

Reputation: 309

multiple queues consuming in one channel

I use rabbitMq for manage and work with queues. I have multiple queues. the count of them is n't specific. I use direct exchange for publishing messages. how can I consume all messages of each queues (based on routing_key) using only one channel? at this time I assume i have 5 queues. I've used for loop and create a channel per queue. like this:

 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 });
               }); 
         }) 


     });

but I wanna do it only with one channel. because its more optimistic and use less memory(i do n't know it is true or not!).is there a way for handle unspecific count of queues?

Upvotes: 9

Views: 9145

Answers (1)

Luke Bakken
Luke Bakken

Reputation: 9657

You can use one channel to consume from several queues, but you'll receive messages one-by-one, even if they are coming from different queues. I'm pretty sure a channel exception on one queue will stop consuming from all queues.

Upvotes: 12

Related Questions