【问题标题】:Node.js amqplib - not able to implement the reconnect in case of connection closeNode.js amqplib - 在连接关闭的情况下无法实现重新连接
【发布时间】:2019-02-16 21:03:52
【问题描述】:

当与rabbitmq队列服务器的连接失败时,我正在尝试实现重新连接机制。此代码仅用于消费消息,下面是我的代码(通道初始化函数负责初始化消费者并绑定到队列) .

connect() {
    let conn = amqp.connect(queueConfig.QUEUE_SERVER_URL + "?heartbeat=60");
    return conn;
}

createConnection(){
    console.log("Trying to connect amqp");
    let self = this;
    self.connection = this.connect()
    .then(function(connection){
        console.log("[AMQP] connected");
        connection.on("error",function(err){
            if (err.message !== "Connection closing") {
                console.error("[AMQP] conn error", err.message);
            }
        });
        connection.on("close", function() {
            console.error("[AMQP] reconnecting");
            return setTimeout(createConnection, 1000);
        });
        return connection.createConfirmChannel();
    })
    .then(self.channelInit);
}

在连接失败时,我成功收到提示“[AMQP] reconnecting”,但在该队列未重新连接后,控制台日志中没有其他提示。

请帮忙。

【问题讨论】:

  • 应该是return setTimeout(createConnection, 1000);
  • 我刚刚改了那个错字,试了试,还是不行。
  • 试试这个:return setTimeout(self.createConnection, 1000);

标签: node.js rabbitmq node-amqp node-amqplib


【解决方案1】:

您的方法中有错字。您需要使用 setTimeout(createConnection, 1000); 之类的东西,而不是您的 setTimeout(createConnection(), 1000);

【讨论】:

  • 感谢您的回答。我刚刚尝试了您建议的更改,但仍然无法正常工作。
  • Mmmmmm...setTimeout(createConnection,1000)setTimeout(createConnection.bind(self),1000)
  • 关于我可以在哪里注入代码以重新初始化通道并重新绑定队列的任何想法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
  • 2019-03-21
  • 1970-01-01
相关资源
最近更新 更多