forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(@nestjs/microservices) added rmq transport support
- Loading branch information
Showing
10 changed files
with
5,845 additions
and
7,960 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Channel, Connection } from 'amqplib'; | ||
import { CONNECT_EVENT, ERROR_EVENT, MESSAGE_EVENT, RQM_DEFAULT_URL, SUBSCRIBE, RQM_DEFAULT_QUEUE } from './../constants'; | ||
import { Logger } from '@nestjs/common/services/logger.service'; | ||
import { loadPackage } from '@nestjs/common/utils/load-package.util'; | ||
import { ClientProxy } from './client-proxy'; | ||
import { ClientOptions, RmqOptions } from '../interfaces'; | ||
|
||
let rqmPackage: any = {}; | ||
|
||
export class ClientRMQ extends ClientProxy { | ||
private readonly logger = new Logger(ClientProxy.name); | ||
private client: Connection = null; | ||
private channel: Channel = null; | ||
private url: string; | ||
private queue: string; | ||
|
||
constructor( | ||
private readonly options: ClientOptions) { | ||
super(); | ||
this.url = | ||
this.getOptionsProp<RmqOptions>(this.options, 'url') || RQM_DEFAULT_URL; | ||
this.queue = | ||
this.getOptionsProp<RmqOptions>(this.options, 'queue') || RQM_DEFAULT_QUEUE; | ||
rqmPackage = loadPackage('amqplib', ClientRMQ.name); | ||
this.connect(); | ||
} | ||
|
||
protected async publish(messageObj, callback: (err, result, disposed?: boolean) => void) { | ||
try { | ||
if (!this.client) { | ||
await this.connect(); | ||
} | ||
this.channel.assertQueue('', { exclusive: true }).then(responseQ => { | ||
messageObj.replyTo = responseQ.queue; | ||
this.channel.consume(responseQ.queue, (message) => { | ||
this.handleMessage(message, this.client, callback) | ||
}, { noAck: true }); | ||
this.channel.sendToQueue(this.queue, Buffer.from(JSON.stringify(messageObj))); | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
callback(err, null); | ||
} | ||
} | ||
|
||
private async handleMessage(message, server, callback): Promise<void> { | ||
if(message) { | ||
const { content, fields } = message; | ||
const { err, response, isDisposed } = JSON.parse(content.toString()); | ||
if (isDisposed || err) { | ||
callback({ | ||
err, | ||
response: null, | ||
isDisposed: true, | ||
}); | ||
this.channel.deleteQueue(fields.routingKey); | ||
} | ||
callback({ | ||
err, | ||
response, | ||
}); | ||
} | ||
} | ||
|
||
public close(): void { | ||
this.channel && this.channel.close(); | ||
this.client && this.client.close(); | ||
} | ||
|
||
public handleError(client: Connection): void { | ||
client.addListener(ERROR_EVENT, err => this.logger.error(err)); | ||
} | ||
|
||
public async connect(): Promise<any> { | ||
return new Promise(async (resolve, reject) => { | ||
this.client = await rqmPackage.connect(this.url); | ||
this.channel = await this.client.createChannel(); | ||
this.channel.assertQueue(this.queue, { durable: false }); | ||
resolve(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ export enum Transport { | |
NATS, | ||
MQTT, | ||
GRPC, | ||
RMQ, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Server } from './server'; | ||
import { Channel, Connection } from 'amqplib'; | ||
import { RQM_DEFAULT_URL , RQM_DEFAULT_QUEUE } from './../constants'; | ||
import { CustomTransportStrategy, RmqOptions } from './../interfaces'; | ||
import { MicroserviceOptions } from '../interfaces/microservice-configuration.interface'; | ||
import { loadPackage } from '@nestjs/common/utils/load-package.util'; | ||
import { Observable } from 'rxjs'; | ||
|
||
let rqmPackage: any = {}; | ||
|
||
export class ServerRMQ extends Server implements CustomTransportStrategy { | ||
private server: Connection = null; | ||
private channel: Channel = null; | ||
private url: string; | ||
private queue: string; | ||
|
||
constructor(private readonly options: MicroserviceOptions) { | ||
super(); | ||
this.url = | ||
this.getOptionsProp<RmqOptions>(this.options, 'url') || RQM_DEFAULT_URL; | ||
this.queue = | ||
this.getOptionsProp<RmqOptions>(this.options, 'queue') || RQM_DEFAULT_QUEUE; | ||
rqmPackage = loadPackage('amqplib', ServerRMQ.name); | ||
} | ||
|
||
public async listen(callback: () => void): Promise<void> { | ||
await this.start(callback); | ||
this.channel.consume(this.queue, (msg) => this.handleMessage(msg) , { | ||
noAck: true, | ||
}); | ||
} | ||
|
||
private async start(callback?: () => void) { | ||
try { | ||
this.server = await rqmPackage.connect(this.url); | ||
this.channel = await this.server.createChannel(); | ||
this.channel.assertQueue(this.queue, { durable: false }); | ||
} catch (err) { | ||
this.logger.error(err); | ||
} | ||
} | ||
|
||
public close(): void { | ||
this.channel && this.channel.close(); | ||
this.server && this.server.close(); | ||
} | ||
|
||
private async handleMessage(message): Promise<void> { | ||
const { content } = message; | ||
const messageObj = JSON.parse(content.toString()); | ||
const handlers = this.getHandlers(); | ||
const pattern = JSON.stringify(messageObj.pattern); | ||
if (!this.messageHandlers[pattern]) { | ||
return; | ||
} | ||
const handler = this.messageHandlers[pattern]; | ||
const response$ = this.transformToObservable(await handler(messageObj.data)) as Observable<any>; | ||
response$ && this.send(response$, (data) => this.sendMessage(data, messageObj.replyTo)); | ||
} | ||
|
||
private sendMessage(message, replyTo): void { | ||
const buffer = Buffer.from(JSON.stringify(message)); | ||
this.channel.sendToQueue(replyTo, buffer); | ||
} | ||
} |