@@ -3,7 +3,7 @@ import { RABBITMQ_HEADER_ROUTING_KEY } from '../const';
33import { IMessagingConsumer } from '@nestjstools/messaging' ;
44import { ConsumerMessageDispatcher } from '@nestjstools/messaging' ;
55import { ConsumerMessage } from '@nestjstools/messaging' ;
6- import { Injectable , OnModuleDestroy } from '@nestjs/common' ;
6+ import { Injectable } from '@nestjs/common' ;
77import { MessageConsumer } from '@nestjstools/messaging' ;
88import { ConsumerDispatchedMessageError } from '@nestjstools/messaging' ;
99import { RabbitmqMigrator } from '../migrator/rabbitmq.migrator' ;
@@ -12,82 +12,63 @@ import { Buffer } from 'buffer';
1212@Injectable ( )
1313@MessageConsumer ( AmqpChannel )
1414export class RabbitmqMessagingConsumer
15- implements IMessagingConsumer < AmqpChannel > , OnModuleDestroy
15+ implements IMessagingConsumer < AmqpChannel >
1616{
1717 private channel ?: AmqpChannel = undefined ;
18- private amqpChannel : any ;
1918
2019 constructor ( private readonly rabbitMqMigrator : RabbitmqMigrator ) { }
2120
2221 async consume (
2322 dispatcher : ConsumerMessageDispatcher ,
2423 channel : AmqpChannel ,
2524 ) : Promise < void > {
26- await channel . init ( ) ;
27- this . channel = channel ;
2825 await this . rabbitMqMigrator . run ( channel ) ;
26+ this . channel = channel ;
2927
30- const amqpChannel = await this . channel . connection . createChannel ( ) ;
31- this . amqpChannel = amqpChannel ;
32-
33- if ( ! amqpChannel ) {
34- throw new Error ( 'AMQP channel not initialized' ) ;
35- }
36-
37- await amqpChannel . prefetch ( 1 ) ;
38- await amqpChannel . consume (
39- channel . config . queue ,
40- async ( msg ) => {
41- if ( ! msg ) return ;
28+ channel . connection . createConsumer (
29+ {
30+ queue : channel . config . queue ,
31+ queueOptions : { durable : true } ,
32+ requeue : false ,
33+ } ,
34+ async ( msg ) : Promise < void > => {
35+ const rabbitMqMessage = msg as RabbitMQMessage ;
4236
43- let message : any = msg . content ;
37+ let message = rabbitMqMessage . body ;
4438 if ( Buffer . isBuffer ( message ) ) {
45- message = JSON . parse ( message . toString ( ) ) ;
39+ const messageContent = message . toString ( ) ;
40+ message = JSON . parse ( messageContent ) ;
4641 }
4742
4843 const routingKey =
49- msg . properties . headers ?. [ RABBITMQ_HEADER_ROUTING_KEY ] ??
50- msg . fields . routingKey ;
44+ rabbitMqMessage . headers ?. [ RABBITMQ_HEADER_ROUTING_KEY ] ??
45+ rabbitMqMessage . routingKey ;
5146
52- if ( dispatcher . isReady ( ) ) {
53- await dispatcher . dispatch ( new ConsumerMessage ( message , routingKey ) ) ;
54- amqpChannel . ack ( msg ) ;
55- return ;
56- }
57-
58- amqpChannel . nack ( msg , false , true ) ;
47+ dispatcher . dispatch ( new ConsumerMessage ( message , routingKey ) ) ;
5948 } ,
60- { noAck : false } ,
6149 ) ;
50+
51+ return Promise . resolve ( ) ;
6252 }
6353
6454 async onError (
6555 errored : ConsumerDispatchedMessageError ,
6656 channel : AmqpChannel ,
6757 ) : Promise < void > {
68- if ( channel . config . deadLetterQueueFeature && this . amqpChannel ) {
69- const exchange = 'dead_letter.exchange' ;
70- const routingKey = `${ channel . config . queue } _dead_letter` ;
71-
72- this . amqpChannel . publish (
73- exchange ,
74- routingKey ,
75- Buffer . from ( JSON . stringify ( errored . dispatchedConsumerMessage . message ) ) ,
76- {
77- headers : {
78- 'messaging-routing-key' :
79- errored . dispatchedConsumerMessage . routingKey ,
80- } ,
58+ if ( channel . config . deadLetterQueueFeature ) {
59+ const publisher = channel . connection . createPublisher ( ) ;
60+ const envelope = {
61+ headers : {
62+ 'messaging-routing-key' : errored . dispatchedConsumerMessage . routingKey ,
8163 } ,
82- ) ;
64+ exchange : 'dead_letter.exchange' ,
65+ routingKey : `${ channel . config . queue } _dead_letter` ,
66+ } ;
67+ await publisher . send ( envelope , errored . dispatchedConsumerMessage . message ) ;
68+ await publisher . close ( ) ;
8369 }
84- }
8570
86- async onModuleDestroy ( ) : Promise < void > {
87- if ( this . channel ?. connection ) {
88- await this . channel . connection . close ( ) ;
89- }
90- this . channel = undefined ;
71+ return Promise . resolve ( ) ;
9172 }
9273}
9374
0 commit comments