multiple exchange binds in a loop nodejs

368 views
Skip to first unread message

Rambabu

unread,
Aug 25, 2015, 10:15:47 AM8/25/15
to rabbitmq-users
Hi,

I tried to do multiple exchange binds in a loop its always blocking infinitely can anyone suggest?

Thanks

Michael Klishin

unread,
Aug 25, 2015, 10:17:36 AM8/25/15
to rabbitm...@googlegroups.com, Rambabu
On 25 August 2015 at 17:15:51, Rambabu (rambabu...@gmail.com) wrote:
> I tried to do multiple exchange binds in a loop its always blocking
> infinitely can anyone suggest?

Not without seeing your code and server logs .
--
MK

Staff Software Engineer, Pivotal/RabbitMQ


Rambabu

unread,
Aug 25, 2015, 11:39:53 AM8/25/15
to rabbitmq-users, rambabu...@gmail.com

            var http = require('http');
            var connect = require('amqp').createConnection('amqp://localhost');

            connect.on('ready', function() {
                var ex = connect.exchange('ex', {type: 'fanout'});
                q.on('queueDeclareOk', function(args) {
                    q.bind('server_exchange', 'server_exchange');
                    q.on('queueBindOk', function() {
                        q.subscribe(function(message) {
                          if( message.data.toString() == "ok")
                          {
                                (function(i) {
                                      for (var i = 0; i < 5; i++) {   /*blocking in this area */
                                          var numExchange = connect.exchange('test'+i.toString(), {type: 'fanout'});
                                           numExchange.bind('ex', 'key'+i, function(result) {
                                         
                                           });
                                       }
                                })(i);
                          }
                    });
                });
            });
            });

            http.createServer(function(req, res){

            }).listen(8080, 'localhost');

            console.log('Server running at http://localhost:8080/');

Michael Klishin

unread,
Aug 25, 2015, 11:43:11 AM8/25/15
to rabbitm...@googlegroups.com, Rambabu
On 25 August 2015 at 18:39:55, Rambabu (rambabu...@gmail.com) wrote:
> var connect = require('amqp').createConnection('amqp://localhost');

I suspect this is with node-amqp.  Avoid it like a plague. It has been discussed
many times on the list as to why, it is a buggy and completely unmaintained
piece of software.

We highly recommend amqplib [1], this is what our JS tutorials [2] use.


1. https://github.com/squaremo/amqp.node
2. http://www.rabbitmq.com/getstarted.html

Rambabu

unread,
Aug 26, 2015, 6:34:44 AM8/26/15
to rabbitmq-users, rambabu...@gmail.com
Thanks Michael.

Now i am using amqplib as us suggested i observed that publish is not working properly

I am calling publish api as follows

 ch.publish('ex', 'key1', message);


I have binded "ex" with many exchanges with "key1" . But i observed that publish happened to one or two exchanges only. From web client it is working.

Thanks

Michael Klishin

unread,
Aug 26, 2015, 6:39:48 AM8/26/15
to rabbitm...@googlegroups.com, Rambabu, rambabu...@gmail.com
On 26 August 2015 at 13:34:46, Rambabu (rambabu...@gmail.com) wrote:
> I am calling publish api as follows
>
> ch.publish('ex', 'key1', message);
>
> I have binded "ex" with many exchanges with "key1" . But i observed
> that publish happened to one or two exchanges only. From web client
> it is working.

Please do post full code examples. It is impossible to suggest anything from
a single line of code or "is not working properly". 

Rambabu

unread,
Aug 26, 2015, 8:46:51 AM8/26/15
to rabbitmq-users, rambabu...@gmail.com
Thanks for your quick response

Here is my sample code. In the consumer i am trying to create bindings with different exchanges and publishing message to all with that key.


        var amqp = require('amqplib/callback_api');
        amqp.connect('amqp://localhost', function(err, conn) {
          conn.createChannel(function(err, ch) {
             ch.assertExchange('exm', 'topic',  {durable: true});
             ch.assertQueue('exm', {exclusive: true}, function(err, q) {
                console.log(' [*] Waiting for messages. To exit press CTRL+C');
                ch.bindQueue('exm', 'amq.topic', 'exm');
                ch.consume('exm', function(msg) { /* consuming message from queue */
                    var i;
                    console.log(msg.content.toString());
                    (function(i) {
                        for (var i = 0; i <10; i++) {
                            ch.bindExchange(''+(i+1), 'exm', 'key'); /*binding multiple exchanges with key */
                        }
                        if(i == 10){
                            var val = "Hello World!!";
                            ch.publish('exm','key', new Buffer(val));  /* publish the message */
                        }
                    })(i);
              }, {noAck: true});
            });
          });
        });

Michael Klishin

unread,
Aug 26, 2015, 8:58:40 AM8/26/15
to rabbitm...@googlegroups.com, Rambabu
On 26 August 2015 at 15:46:54, Rambabu (rambabu...@gmail.com) wrote:
> ch.consume('exm', function(msg) { /* consuming message from
> queue */
> var i;
> console.log(msg.content.toString());
> (function(i) {
> for (var i = 0; i <10; i++) {
> ch.bindExchange(''+(i+1), 'exm', 'key'); /*binding multiple
> exchanges with key */
> }
> if(i == 10){
> var val = "Hello World!!";
> ch.publish('exm','key', new Buffer(val)); /* publish the
> message */
> }
> })(i);
> }, {noAck: true});

Why are you trying to modify bindings for queue "exm" from a consumer on the same queue?
That's race condition and error prone.

Bindings are meant to be set up before you begin consuming. This is not a technical limitation
but reasoning about what messages your consumer will receive gets a lot more
complicated when it messes with bindings. 

Michael Klishin

unread,
Aug 26, 2015, 10:11:27 AM8/26/15
to Rambabu Kodati, rabbitm...@googlegroups.com
+rabbitmq-users, please keep the list on CC  

On 26 August 2015 at 17:06:17, Rambabu Kodati (rambabu...@gmail.com) wrote:
> Can i know whether exchange is exists or not with amqplib?

Simply declare it with the arguments you want or do a passive declare (does
nothing if the exchange exists, throws a channel error otherwise).

Rambabu

unread,
Aug 26, 2015, 10:28:08 AM8/26/15
to rabbitmq-users, rambabu...@gmail.com
Sorry for disturbing you. Can you send me sample example?

I used
 ch.assertExchange('121', 'fanout',  {durable: true, passive:true}, function(error, ex){
                                         console.log(error);
                                         console.log(ex);
                                     });
its not giving any error its simply creating new exchange. As per my requirement i should not create unwanted exchanges.

Michael Klishin

unread,
Aug 26, 2015, 10:37:42 AM8/26/15
to rabbitm...@googlegroups.com, Rambabu
 On 26 August 2015 at 17:28:11, Rambabu (rambabu...@gmail.com) wrote:
> Sorry for disturbing you. Can you send me sample example?
>
> I used
> ch.assertExchange('121', 'fanout', {durable: true, passive:true},
> function(error, ex){
> console.log(error);
> console.log(ex);
> });
> its not giving any error its simply creating new exchange. As
> per my requirement i should not create unwanted exchanges.

I couldn't (quickly) find any passive declare examples, so chances are, amqplib
doesn't bother supporting them — feel free to file an issue (they use issues for questions).

AMQP 0-9-1 philosophy is that apps declare the resources they need. If you are not
sure if an exchange exists or not, simply declare it. And if exchange name is known,
you can declare it e.g. during system provisioning/deployment and don't have to declare
it over and over in your code.

Alvaro Videla

unread,
Aug 26, 2015, 11:09:05 AM8/26/15
to rabbitm...@googlegroups.com, Rambabu
I think the passive methods are called checkExchange and checkQueue, see here for more info:


--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send an email to rabbitm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages