Nest.js ClientProxy 重新连接

编程入门 行业动态 更新时间:2024-10-04 19:32:16

<a href=https://www.elefans.com/category/jswz/34/1771005.html style=Nest.js ClientProxy 重新连接"/>

Nest.js ClientProxy 重新连接

我正在使用 Nest.js 的微服务,但我遇到了一个问题。

我的需求是向 RabbitMQ 发送消息并将消息日志保存到 mongo,如果 ClientProxy 已连接,mongo 中的 sendMessageStatus 为“ok”,如果 ClientProxy 未连接,mongo 中的 sendMessageStatus 为“fail”。

当我测试我的应用程序时,我关闭了 RabbitMQ 服务器并发送消息,mongo 可以使用 sendMessageStatus 获取此日志:“fail”,之后我重新启动 RabbitMQ 服务器并再次执行,mongo 应该获取 sendMessageStatus:“ok”,但是它不会,仍然“失败”。

这意味着即使 RabbitMQ 服务器重新启动,ClientProxy 也不会重新连接,我注意到 Nest.js 官方文档说“ClientProxy 是懒惰的。”,那么我该如何重新连接 ClientProxy?

jobQueue.module.ts

return ClientProxyFactory.create({
            transport: Transport.RMQ,
            options: {
              urls: [`amqp://${account}:${password}@${IP}:${port}`],
              queue: outputQueueName,
              serializer: {
                serialize: value => value.data,
              },
              noAck: false,
              persistent: true,
              queueOptions: {
                durable: true,
              }
            }
          });

jobQueue.service.ts

constructor(
        @Inject(CONNECTION_NAME)
        private readonly client: ClientRMQ,
    ) {
    };
async sendMessage(data: SendMessageDto) {
        try {
            this.logger.serviceDebug(SENDMESSAGE_METHOD);
            data.id = this.messageID++;
            return await this.client.connect()
                .then(() => {
                    return this.client.emit('', data)
                }).catch(err => {
                    return this.client.emit('', data)
                        .pipe(
                            catchError(connectionError => {
                                throw connectionError;
                            })
                        );
                });
        } catch (err) {
            console.log('catch in job', err);
            throw err;
        };
    };

client.connect() 无济于事。

myService.service.ts

const messageObserver = await this.jobQueueService.sendMessage(MQCLI);
                            const createdLog: CreateScheduleExecutionLogDto = {
                                ...data,
                                scheduleID: scheduleID,
                                schedule: item,
                                processDatetime: new Date(),
                            };
                            messageObserver.subscribe({
                                next: x => {
                                    console.log(x);
                                    createdLog.processStatus = OK;
                                    this.scheduleExecutionLogModel.create(createdLog);
                                },
                                error: e => {
                                    console.log(e);
                                    createdLog.processStatus = ERROR;
                                    this.scheduleExecutionLogModel.create(createdLog);
                                },
                            })
回答如下:

this.client.close() 方法允许您删除旧实例。对 this.client 的新请求将创建一个新连接

try{
    const recponse = this.client.emit('').pipe(timeout(5000)).toPromise()
}catch(e){
    if(e.err.code == 'ECONNREFUSED'){
        this.client.close()
    }
}

更多推荐

Nest.js ClientProxy 重新连接

本文发布于:2024-05-30 22:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Nest   js   ClientProxy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!