Skip to content

Commit ee2fd56

Browse files
Merge branch 'sergey-telpuk-buxfix/3138-rabbitMQ-consumer-already-set'
2 parents 013f6f1 + cf36b70 commit ee2fd56

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

packages/microservices/client/client-rmq.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { EventEmitter } from 'events';
55
import { fromEvent, merge, Observable } from 'rxjs';
66
import { first, map, share, switchMap } from 'rxjs/operators';
77
import {
8+
DISCONNECTED_RMQ_MESSAGE,
89
DISCONNECT_EVENT,
910
ERROR_EVENT,
1011
RQM_DEFAULT_IS_GLOBAL_PREFETCH_COUNT,
@@ -52,6 +53,8 @@ export class ClientRMQ extends ClientProxy {
5253
public close(): void {
5354
this.channel && this.channel.close();
5455
this.client && this.client.close();
56+
this.channel = null;
57+
this.client = null;
5558
}
5659

5760
public consumeChannel() {
@@ -74,6 +77,7 @@ export class ClientRMQ extends ClientProxy {
7477
}
7578
this.client = this.createClient();
7679
this.handleError(this.client);
80+
this.handleDisconnectError(this.client);
7781

7882
const connect$ = this.connect$(this.client);
7983
this.connection = this.mergeDisconnectEvent(this.client, connect$)
@@ -82,6 +86,7 @@ export class ClientRMQ extends ClientProxy {
8286
share(),
8387
)
8488
.toPromise();
89+
8590
return this.connection;
8691
}
8792

@@ -134,6 +139,15 @@ export class ClientRMQ extends ClientProxy {
134139
client.addListener(ERROR_EVENT, (err: any) => this.logger.error(err));
135140
}
136141

142+
public handleDisconnectError(client: any): void {
143+
client.addListener(DISCONNECT_EVENT, (err: any) => {
144+
this.logger.error(DISCONNECTED_RMQ_MESSAGE);
145+
this.logger.error(err);
146+
147+
this.close();
148+
});
149+
}
150+
137151
public handleMessage(
138152
packet: unknown,
139153
callback: (packet: WritePacket) => any,

packages/microservices/server/server-rmq.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ export class ServerRMQ extends Server implements CustomTransportStrategy {
6969
public async start(callback?: () => void) {
7070
this.server = this.createClient();
7171
this.server.on(CONNECT_EVENT, (_: any) => {
72+
if (this.channel) {
73+
return;
74+
}
7275
this.channel = this.server.createChannel({
7376
json: false,
7477
setup: (channel: any) => this.setupChannel(channel, callback),

0 commit comments

Comments
 (0)