11import { Server } from './server' ;
2- import { Channel , Connection } from 'amqplib' ;
3- import { RQM_DEFAULT_URL , RQM_DEFAULT_QUEUE } from './../constants' ;
2+ import { Channel , Connection , Options } from 'amqplib' ;
3+ import { RQM_DEFAULT_URL , RQM_DEFAULT_QUEUE , RQM_DEFAULT_PREFETCH_COUNT , RQM_DEFAULT_IS_GLOBAL_PREFETCH_COUNT , RQM_DEFAULT_QUEUE_OPTIONS } from './../constants' ;
44import { CustomTransportStrategy , RmqOptions } from './../interfaces' ;
55import { MicroserviceOptions } from '../interfaces/microservice-configuration.interface' ;
66import { loadPackage } from '@nestjs/common/utils/load-package.util' ;
@@ -13,53 +13,63 @@ export class ServerRMQ extends Server implements CustomTransportStrategy {
1313 private channel : Channel = null ;
1414 private url : string ;
1515 private queue : string ;
16+ private prefetchCount : number ;
17+ private queueOptions : Options . AssertQueue
18+ private isGlobalPrefetchCount : boolean ;
1619
1720 constructor ( private readonly options : MicroserviceOptions ) {
1821 super ( ) ;
1922 this . url =
2023 this . getOptionsProp < RmqOptions > ( this . options , 'url' ) || RQM_DEFAULT_URL ;
2124 this . queue =
2225 this . getOptionsProp < RmqOptions > ( this . options , 'queue' ) || RQM_DEFAULT_QUEUE ;
26+ this . prefetchCount =
27+ this . getOptionsProp < RmqOptions > ( this . options , 'prefetchCount' ) || RQM_DEFAULT_PREFETCH_COUNT ;
28+ this . isGlobalPrefetchCount =
29+ this . getOptionsProp < RmqOptions > ( this . options , 'isGlobalPrefetchCount' ) || RQM_DEFAULT_IS_GLOBAL_PREFETCH_COUNT ;
30+ this . queueOptions =
31+ this . getOptionsProp < RmqOptions > ( this . options , 'queueOptions' ) || RQM_DEFAULT_QUEUE_OPTIONS ;
2332 rqmPackage = loadPackage ( 'amqplib' , ServerRMQ . name ) ;
24- }
33+ }
2534
26- public async listen ( callback : ( ) => void ) : Promise < void > {
27- await this . start ( callback ) ;
28- this . channel . consume ( this . queue , ( msg ) => this . handleMessage ( msg ) , {
29- noAck : true ,
30- } ) ;
31- }
35+ public async listen ( callback : ( ) => void ) : Promise < void > {
36+ await this . start ( callback ) ;
37+ this . channel . consume ( this . queue , ( msg ) => this . handleMessage ( msg ) , {
38+ noAck : true ,
39+ } ) ;
40+ }
3241
33- private async start ( callback ?: ( ) => void ) {
34- try {
35- this . server = await rqmPackage . connect ( this . url ) ;
36- this . channel = await this . server . createChannel ( ) ;
37- this . channel . assertQueue ( this . queue , { durable : false } ) ;
38- } catch ( err ) {
39- this . logger . error ( err ) ;
40- }
41- }
42+ private async start ( callback ?: ( ) => void ) {
43+ try {
44+ this . server = await rqmPackage . connect ( this . url ) ;
45+ this . channel = await this . server . createChannel ( ) ;
46+ this . channel . assertQueue ( this . queue , this . queueOptions ) ;
47+ await this . channel . prefetch ( this . prefetchCount , this . isGlobalPrefetchCount ) ;
48+ } catch ( err ) {
49+ this . logger . error ( err ) ;
50+ }
51+ }
4252
43- public close ( ) : void {
44- this . channel && this . channel . close ( ) ;
45- this . server && this . server . close ( ) ;
46- }
53+ public close ( ) : void {
54+ this . channel && this . channel . close ( ) ;
55+ this . server && this . server . close ( ) ;
56+ }
4757
48- private async handleMessage ( message ) : Promise < void > {
49- const { content, properties } = message ;
50- const messageObj = JSON . parse ( content . toString ( ) ) ;
51- const handlers = this . getHandlers ( ) ;
52- const pattern = JSON . stringify ( messageObj . pattern ) ;
53- if ( ! this . messageHandlers [ pattern ] ) {
54- return ;
58+ private async handleMessage ( message ) : Promise < void > {
59+ const { content, properties } = message ;
60+ const messageObj = JSON . parse ( content . toString ( ) ) ;
61+ const handlers = this . getHandlers ( ) ;
62+ const pattern = JSON . stringify ( messageObj . pattern ) ;
63+ if ( ! this . messageHandlers [ pattern ] ) {
64+ return ;
65+ }
66+ const handler = this . messageHandlers [ pattern ] ;
67+ const response$ = this . transformToObservable ( await handler ( messageObj . data ) ) as Observable < any > ;
68+ response$ && this . send ( response$ , ( data ) => this . sendMessage ( data , properties . replyTo , properties . correlationId ) ) ;
5569 }
56- const handler = this . messageHandlers [ pattern ] ;
57- const response$ = this . transformToObservable ( await handler ( messageObj . data ) ) as Observable < any > ;
58- response$ && this . send ( response$ , ( data ) => this . sendMessage ( data , properties . replyTo , properties . correlationId ) ) ;
59- }
6070
61- private sendMessage ( message , replyTo , correlationId ) : void {
62- const buffer = Buffer . from ( JSON . stringify ( message ) ) ;
63- this . channel . sendToQueue ( replyTo , buffer , { correlationId : correlationId } ) ;
64- }
71+ private sendMessage ( message , replyTo , correlationId ) : void {
72+ const buffer = Buffer . from ( JSON . stringify ( message ) ) ;
73+ this . channel . sendToQueue ( replyTo , buffer , { correlationId : correlationId } ) ;
74+ }
6575}
0 commit comments