微服务RabbitMQ回调API

时间:2020-09-04 13:07:21

标签: typescript rabbitmq

你好,我对如何在编排场景中使用Rabbitmq感到怀疑,我也想拥有一个错误处理程序,所以我有以下疑问:

我应该为Rabbitmq服务器创建一个容器以进行回调 还是我会在每个服务中使用回调API创建Rabbitmq服务器?

RabbitMq服务器

export default class RabbitmqServer {
  private connenction: Connection;
  private channel: Channel;
  private logger = logger;
  constructor(private uri: string) {}

  startRabbitMQ() {
    connect(this.uri, (error: Error, connection: Connection) => {
      if (error) {
        this.logger.error(error.message);
        setTimeout(this.startRabbitMQ, 10000);
      }
      //listener on error
      connection.on('error', (err: Error) => {
        this.logger.error(err.message);
        setTimeout(this.startRabbitMQ, 10000);
      });
      //listener on close
      connection.on('close', () => {
        this.logger.error('connection to RabbitQM closed!');
        setTimeout(this.startRabbitMQ, 10000);
      });
      this.connenction = connection;
    });
  }
  createChannel = () => {
    this.connenction.createChannel((error: Error, channel: Channel) => {
      if (error) this.logger.error(error.message);
      channel.on('error', (errorOnChannel: Error) => {
        this.logger.error('[AMQP] channel error', errorOnChannel.message);
      });
      channel.on('close', () => {
        this.logger.error('[AMQP] channel closed');
      });
      this.channel = channel;
    });
  };
}

0 个答案:

没有答案