如何在node.js amqp中发出请求后保持队列处于活动状态?

时间:2012-05-10 13:18:54

标签: node.js rabbitmq amqp

每次我想使用它时,是否真的需要实例化队列“queue = connection.queue(...)”?

服务器(node.js):

 queue = connection.queue('incoming', { // <--- DO I REALLY NEED THAT FOR EVERY REQUEST?
        durable : true,
        exclusive : false }, function() {});

 queue.subscribe(function(msg) {
      // Unsubcribe here. Maybe there is something like a once listener?
     console.log("RECEIVED: "+msg)
      //res.send(msg.data);
      queue.unsubscribe(ctag);
     res.redirect('/home');

   }).addCallback(function(ok) { ctag = ok.consumerTag; });

   exchange.publish('msg_queue', 'functional!', {  // request
      replyTo: 'incoming'
   }); 

如果queue = connection.queue(...);与服务器实例化,则使用队列的第一个请求成功,但以下请求会引发错误:

Error: NOT_FOUND - no queue 'incoming' in vhost '/'
at Queue._onMethod (/Users/cassiomelo/code/cubix/cubix_nodejs/node_modules/amqp/amqp.js:1720:15)

1 个答案:

答案 0 :(得分:1)

对于一个让回复队列保持不变的rpc示例,请查看How to create REP/REQ on Rabbit.js