amqp.connect无法永远保持连接状态

时间:2018-08-23 19:03:46

标签: node.js rabbitmq amqp

我的代码如下所示:

rabbitmq.js

const connectRabbitMq = () => {
    amqp.connect(process.env.CLOUDAMQP_MQTT_URL, function (err, conn) {
        if (err) {
            console.error(err);
            console.log('[AMQP] reconnecting in 1s');
            setTimeout(connectRabbitMq, 1);
            return;
        }
        conn.createChannel((err, ch) => {
            if (!err) {
                console.log('Channel created');
                channel = ch;
                connection = conn;
            }
        });

        conn.on("error", function (err) {
            if (err.message !== "Connection closing") {
                console.error("[AMQP] conn error", err.message);
            }
        });

        conn.on("close", function () {
            console.error("[AMQP] reconnecting");
            connectRabbitMq();
        });
    })

};


const sendMessage = () => {


    let data = {
        user_id: 1,
        test_id: 2
    };

    if (channel) {
        channel.sendToQueue(q, new Buffer(JSON.stringify(data)), {
            persistent: true
        });
    }

    else {

        connectRabbitMq(() => {
            channel.sendToQueue(q, new Buffer(JSON.stringify(data)), {
                persistent: true
            });
        })
    }
};


const receiveMessage = () => {

    if (channel) {

        channel.consume(q, function (msg) {
            // ch.ack(msg);
            console.log(" [x] Received %s", msg.content.toString());
        });
    }

    else {

        connectRabbitMq(() => {
            channel.consume(q, function (msg) {
                // ch.ack(msg);
                console.log(" [x] Received %s", msg.content.toString());
            });
        })
    }
}

scheduler.js

let cron = require('node-cron');


const callMethodForeverRabbitMq = () => {


    cron.schedule('*/1 * * * * *', function () {

        rabbitMqClientPipeline.receiveMessage();
    });


};

app.js

rabbitmq.sendMessage();

现在这里发生的是,代码无法永远维持连接。所以有什么办法可以让它永远活着吗?

1 个答案:

答案 0 :(得分:0)

我不确定您使用的是Promise api还是回调API。

使用Promise API,您可以这样做:

const amqp = require('amqplib');

const delay = (ms) => new Promise((resolve => setTimeout(resolve, ms)));

const connectRabbitMq = () => amqp.connect('amqp://127.0.0.1:5672')
  .then((conn) => {
    conn.on('error', function (err) {
      if (err.message !== 'Connection closing') {
        console.error('[AMQP] conn error', err.message);
      }
    });

    conn.on('close', function () {
      console.error('[AMQP] reconnecting');
      connectRabbitMq();
    });

    //connection = conn;

    return conn.createChannel();
  })
  .then(ch => {
    console.log('Channel created');
    //channel = ch;
  })
  .catch((error) => {
    console.error(error);
    console.log('[AMQP] reconnecting in 1s');
    return delay(1000).then(() => connectRabbitMq())
  });

connectRabbitMq();

使用这样的回调API:

const amqp = require('amqplib/callback_api');

const connectRabbitMq = () => {
  amqp.connect('amqp://127.0.0.1:5672', function (err, conn) {
    if (err) {
      console.error(err);
      console.log('[AMQP] reconnecting in 1s');
      setTimeout(connectRabbitMq, 1000);
      return;
    }
    conn.createChannel((err, ch) => {
      if (!err) {
        console.log('Channel created');
        //channel = ch;
        //connection = conn;
      }
    });

    conn.on("error", function (err) {
      if (err.message !== "Connection closing") {
        console.error("[AMQP] conn error", err.message);
      }
    });

    conn.on("close", function () {
      console.error("[AMQP] reconnecting");
      connectRabbitMq();
    });
  })
};

connectRabbitMq();

更新具有请求缓冲的新代码

const buffer = [];
let connection = null;
let channel = null;

const connectRabbitMq = () => {
  amqp.connect('amqp://127.0.0.1:5672', function (err, conn) {
    if (err) {
      console.error(err);
      console.log('[AMQP] reconnecting in 1s');
      setTimeout(connectRabbitMq, 1000);
      return;
    }
    conn.createChannel((err, ch) => {
      if (!err) {
        console.log('Channel created');
        channel = ch;
        connection = conn;

        while (buffer.length > 0) {
          const request = buffer.pop();
          request();
        }
      }
    });

    conn.once("error", function (err) {
      channel = null;
      connection = null;

      if (err.message !== "Connection closing") {
        console.error("[AMQP] conn error", err.message);
      }
    });

    conn.once("close", function () {
      channel = null;
      connection = null;

      console.error("[AMQP] reconnecting");
      connectRabbitMq();
    });
  })
};

const sendMessage = () => {
  let data = {
    user_id: 1,
    test_id: 2
  };

  if (channel) {
    channel.sendToQueue(q, new Buffer(JSON.stringify(data)), {
      persistent: true
    });
  }
  else {
    buffer.push(() => {
      channel.sendToQueue(q, new Buffer(JSON.stringify(data)), {
        persistent: true
      });
    });
  }
};

const receiveMessage = () => {
  if (channel) {
    channel.consume(q, function (msg) {
      // ch.ack(msg);
      console.log(" [x] Received %s", msg.content.toString());
    });
  }
  else {
    buffer.push(() => {
      channel.consume(q, function (msg) {
        // ch.ack(msg);
        console.log(" [x] Received %s", msg.content.toString());
      });
    })
  }
};

在某些极端情况下,此代码无法使用-例如,除非明确调用,否则它不会重新建立queue.consume。但是总的来说,这希望能给您带来实现正确恢复的想法...