6

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?

m.eslampnah
  • 169
  • 1
  • 4
  • 10
  • node sample code rabbitmq , multi-Queues , single channel https://github.com/heroku-examples/node-articles-nlp/blob/master/lib/app/index.js#L9 – Robert Rowntree Sep 16 '18 at 07:54
  • Note that that code is not using a single channel, because you *must* use at least one channel per-queue to consume messages. – Luke Bakken Sep 17 '18 at 13:56

1 Answers1

10

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.


NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.

Luke Bakken
  • 8,993
  • 2
  • 20
  • 33
  • You mean I ask my question there again? – m.eslampnah Sep 18 '18 at 16:05
  • 1
    No, that is a footer I include to tell people that the best place to ask RabbitMQ questions is the mailing list, not stack overflow. – Luke Bakken Sep 18 '18 at 23:45
  • 1
    @LukeBakken I'm using the node client and currently using the same channel to consume on multiple queues, everything is running fine as well. I'm sure you're more experienced than me in rabbit, should I move to a multi-channel setup? – Sanket Berde Dec 27 '21 at 06:46
  • @LukeBakken - Luke, the official docs state "...limiting the number of channels used per connection is highly recommended", recommend pooling channels (so it seems re-using with various queues) and don't mention the one channel per queue rule at all (https://www.rabbitmq.com/channels.html). Can you please clarify and give more context to your answer? Thanks in advance. – Dmitri R117 Feb 21 '23 at 03:53