【问题标题】:How to check for the existence of an exchange in RabbitMQ from node.js?如何从 node.js 检查 RabbitMQ 中是否存在交换?
【发布时间】:2014-01-07 06:00:44
【问题描述】:

我想从 node.js 检查是否存在特定的 RabbitMQ 交换。我正在使用 Mocha 作为测试框架。我已经编写了相同的代码,但我的期望似乎不正确。当没有交换时,我希望交换变量的值未定义,但事实并非如此。我正在使用 amqp 模块与 RabbitMQ 进行交互。以下是代码:

var should = require('should');
var amqp = require('amqp');

//Configuration
var amqpConnectionDetails = {
    'host':'localhost',
    'port':5672,
    'login':'guest',
    'password':'guest'
};

describe('AMQP Objects', function(){
    describe('Exchanges', function(){
        it('There should exist an exchange', function(done){
            var amqpConnection = amqp.createConnection(amqpConnectionDetails);
            amqpConnection.on('ready', function(){
                var exchange = amqpConnection.exchange('some_exchange', {'passive':true, 'noDeclare':true});
                exchange.should.not.be.equal(undefined);
                exchange.should.not.be.equal(null);
                done();
            });
        });
    });
});

检查交易所是否存在的正确方法是什么?

谢谢。

【问题讨论】:

    标签: node.js rabbitmq node-amqp


    【解决方案1】:

    如果交换不存在,则会抛出错误(“未捕获错误:NOT_FOUND - no exchange 'some_exchange' in vhost '/'”)。这意味着您应该添加一个“on error”方法来交换以捕获它在交换不存在时将抛出的错误。

    其次,您应该从选项中删除 'noDeclare':true。

    以下应该可以工作(如果交换不存在,它将正常退出,如果交换存在,则会抛出异常):

    var amqp = require('amqp');
    
    //Configuration
    var amqpConnectionDetails = {
      'host':'localhost',
      'port':5672,
      'login':'guest',
      'password':'guest'
    };
    
    describe('AMQP Objects', function(){
      describe('Exchanges', function(){
        it('There should not exist an exchange', function(done){
          var amqpConnection = amqp.createConnection(amqpConnectionDetails);
          amqpConnection.on('ready', function(){
            var exchange = amqpConnection.exchange('some_exchange', {'passive':true});
            exchange.on('error', function(error) {
              done();
             });
             exchange.on('open', function(exchange) {
               throw("exchange exists!")
               done();
             });
           });
         });
       });
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 2023-04-08
      • 2013-07-15
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多