@@ -14,18 +14,25 @@ import {RunMQLogger} from "@src/core/logging/RunMQLogger";
1414import { DefaultDeserializer } from "@src/core/serializers/deserializer/DefaultDeserializer" ;
1515import { ConsumerCreatorUtils } from "@src/core/consumer/ConsumerCreatorUtils" ;
1616import { RunMQPublisherCreator } from "@src/core/publisher/RunMQPublisherCreator" ;
17- import { AMQPClient } from "@src/types" ;
17+ import { AMQPClient , RabbitMQManagementConfig } from "@src/types" ;
18+ import { RunMQTTLPolicyManager } from "@src/core/management/Policies/RunMQTTLPolicyManager" ;
19+ import { RunMQException } from "@src/core/exceptions/RunMQException" ;
20+ import { Exceptions } from "@src/core/exceptions/Exceptions" ;
1821
1922export class RunMQConsumerCreator {
23+ private ttlPolicyManager : RunMQTTLPolicyManager ;
24+
2025 constructor (
2126 private defaultChannel : Channel ,
2227 private client : AMQPClient ,
2328 private logger : RunMQLogger ,
29+ managementConfig ?: RabbitMQManagementConfig
2430 ) {
31+ this . ttlPolicyManager = new RunMQTTLPolicyManager ( logger , managementConfig ) ;
2532 }
2633
27-
2834 public async createConsumer < T > ( consumerConfiguration : ConsumerConfiguration < T > ) {
35+ await this . ttlPolicyManager . initialize ( ) ;
2936 await this . assertQueues < T > ( consumerConfiguration ) ;
3037 await this . bindQueues < T > ( consumerConfiguration ) ;
3138 for ( let i = 0 ; i < consumerConfiguration . processorConfig . consumersCount ; i ++ ) {
@@ -78,16 +85,43 @@ export class RunMQConsumerCreator {
7885 deadLetterExchange : Constants . DEAD_LETTER_ROUTER_EXCHANGE_NAME ,
7986 deadLetterRoutingKey : consumerConfiguration . processorConfig . name
8087 } ) ;
81- await this . defaultChannel . assertQueue ( ConsumerCreatorUtils . getRetryDelayTopicName ( consumerConfiguration . processorConfig . name ) , {
82- durable : true ,
83- deadLetterExchange : Constants . ROUTER_EXCHANGE_NAME ,
84- messageTtl : consumerConfiguration . processorConfig . attemptsDelay ?? DEFAULTS . PROCESSING_RETRY_DELAY ,
85- } ) ;
8688 await this . defaultChannel . assertQueue ( ConsumerCreatorUtils . getDLQTopicName ( consumerConfiguration . processorConfig . name ) , {
8789 durable : true ,
8890 deadLetterExchange : Constants . ROUTER_EXCHANGE_NAME ,
8991 deadLetterRoutingKey : consumerConfiguration . processorConfig . name
9092 } ) ;
93+
94+ const retryDelayQueueName = ConsumerCreatorUtils . getRetryDelayTopicName ( consumerConfiguration . processorConfig . name ) ;
95+ const messageDelay = consumerConfiguration . processorConfig . attemptsDelay ?? DEFAULTS . PROCESSING_RETRY_DELAY
96+
97+
98+ const policiesForTTL = consumerConfiguration . processorConfig . usePoliciesForTTL ?? false ;
99+ if ( ! policiesForTTL ) {
100+ await this . defaultChannel . assertQueue ( retryDelayQueueName , {
101+ durable : true ,
102+ deadLetterExchange : Constants . ROUTER_EXCHANGE_NAME ,
103+ messageTtl : messageDelay ,
104+ } ) ;
105+ return ;
106+ }
107+
108+ const result = await this . ttlPolicyManager . apply (
109+ retryDelayQueueName ,
110+ messageDelay
111+ ) ;
112+ if ( result ) {
113+ await this . defaultChannel . assertQueue ( retryDelayQueueName , {
114+ durable : true ,
115+ deadLetterExchange : Constants . ROUTER_EXCHANGE_NAME
116+ } ) ;
117+ return ;
118+ }
119+ throw new RunMQException (
120+ Exceptions . FAILURE_TO_DEFINE_TTL_POLICY ,
121+ {
122+ error : "Failed to apply TTL policy to queue: " + retryDelayQueueName
123+ }
124+ ) ;
91125 }
92126
93127
0 commit comments