3

当与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”,但在该队列没有重新连接之后,控制台日志中没有其他提示。

请帮忙。

4

1 回答 1

1

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

于 2018-09-12T09:12:40.867 回答