Answers for "RabbitMQ error handling"

0

RabbitMQ error handling

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();
Posted by: Guest on April-15-2022

Browse Popular Code Answers by Language