From 55feaed3610b1a791abddb78dc414de9d9505a01 Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 23 Jul 2026 15:53:57 +0200 Subject: [PATCH] Make RabbitMQ configuration dynamic Updated `RabbitMqConfiguration` to include properties for queue and exchange names, replacing hardcoded constants in `RabbitMqEmailQueue`. All RabbitMQ operations now use dynamic values from the configuration object, improving flexibility and configurability. Updated logging to reflect these changes. --- .../Messaging/RabbitMqConfiguration.cs | 7 +++ .../Queue/RabbitMqEmailQueue.cs | 45 ++++++++----------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/DigitalData.EmailProfiler.Infrastructure/Messaging/RabbitMqConfiguration.cs b/src/DigitalData.EmailProfiler.Infrastructure/Messaging/RabbitMqConfiguration.cs index 52ac45e..251c883 100644 --- a/src/DigitalData.EmailProfiler.Infrastructure/Messaging/RabbitMqConfiguration.cs +++ b/src/DigitalData.EmailProfiler.Infrastructure/Messaging/RabbitMqConfiguration.cs @@ -44,4 +44,11 @@ public class RabbitMqConfiguration /// Network recovery interval in seconds /// public int NetworkRecoveryIntervalSeconds { get; set; } = 10; + + public string QueueName { get; set; } = null!; + public string ExchangeName { get; set; } = null!; + public string RoutingKey { get; set; } = null!; + public string DlqQueueName { get; set; } = null!; + public string DlqExchangeName { get; set; } = null!; + public string DlqRoutingKey { get; set; } = null!; } diff --git a/src/DigitalData.EmailProfiler.Infrastructure/Queue/RabbitMqEmailQueue.cs b/src/DigitalData.EmailProfiler.Infrastructure/Queue/RabbitMqEmailQueue.cs index 3d326ec..8eef114 100644 --- a/src/DigitalData.EmailProfiler.Infrastructure/Queue/RabbitMqEmailQueue.cs +++ b/src/DigitalData.EmailProfiler.Infrastructure/Queue/RabbitMqEmailQueue.cs @@ -26,13 +26,6 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable // Lazy ensures InitAsync is called only ONCE (thread-safe) private readonly Lazy _initializationTask; - private const string QueueName = "emailprofiler.email.outbox"; - private const string ExchangeName = "emailprofiler.emails"; - private const string RoutingKey = "email.outbox"; - private const string DlqQueueName = "emailprofiler.email.outbox.dlq"; - private const string DlqExchangeName = "emailprofiler.emails.dlq"; - private const string DlqRoutingKey = "email.outbox.dlq"; - #pragma warning disable CS8618 // channel and connection are initialized in InitAsync, not in constructor public RabbitMqEmailQueue(IOptions config, ILogger logger) #pragma warning restore CS8618 @@ -68,14 +61,14 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable // Declare Dead Letter Queue (DLQ) exchange await _channel.ExchangeDeclareAsync( - exchange: DlqExchangeName, + exchange: _config.DlqExchangeName, type: ExchangeType.Direct, durable: true, autoDelete: false); // Declare Dead Letter Queue (DLQ) await _channel.QueueDeclareAsync( - queue: DlqQueueName, + queue: _config.DlqQueueName, durable: true, exclusive: false, autoDelete: false, @@ -83,13 +76,13 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable // Bind DLQ to DLQ exchange await _channel.QueueBindAsync( - queue: DlqQueueName, - exchange: DlqExchangeName, - routingKey: DlqRoutingKey); + queue: _config.DlqQueueName, + exchange: _config.DlqExchangeName, + routingKey: _config.DlqRoutingKey); // Declare main exchange (Direct type for routing) await _channel.ExchangeDeclareAsync( - exchange: ExchangeName, + exchange: _config.ExchangeName, type: ExchangeType.Direct, durable: true, autoDelete: false); @@ -97,12 +90,12 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable // Declare main queue (durable for persistence) with DLQ arguments var queueArgs = new Dictionary { - { "x-dead-letter-exchange", DlqExchangeName }, - { "x-dead-letter-routing-key", DlqRoutingKey } + { "x-dead-letter-exchange", _config.DlqExchangeName }, + { "x-dead-letter-routing-key", _config.DlqRoutingKey } }; await _channel.QueueDeclareAsync( - queue: QueueName, + queue: _config.QueueName, durable: true, exclusive: false, autoDelete: false, @@ -110,11 +103,11 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable // Bind main queue to exchange with routing key await _channel.QueueBindAsync( - queue: QueueName, - exchange: ExchangeName, - routingKey: RoutingKey); + queue: _config.QueueName, + exchange: _config.ExchangeName, + routingKey: _config.RoutingKey); - _logger.LogInformation("RabbitMQ initialized successfully: Queue={QueueName}, DLQ={DlqQueueName}", QueueName, DlqQueueName); + _logger.LogInformation("RabbitMQ initialized successfully: Queue={QueueName}, DLQ={DlqQueueName}", _config.QueueName, _config.DlqQueueName); } /// @@ -141,8 +134,8 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable }; await _channel!.BasicPublishAsync( - exchange: ExchangeName, - routingKey: RoutingKey, + exchange: _config.ExchangeName, + routingKey: _config.RoutingKey, mandatory: false, basicProperties: properties, body: body, @@ -153,7 +146,7 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable { await EnsureInitializedAsync(); // Initialize on first call - var result = await _channel!.BasicGetAsync(QueueName, false, cancellationToken); + var result = await _channel!.BasicGetAsync(_config.QueueName, false, cancellationToken); if (result == null) return null; @@ -180,7 +173,7 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable { await EnsureInitializedAsync(); // Initialize on first call - var queueInfo = await _channel!.QueueDeclarePassiveAsync(QueueName, cancellationToken); + var queueInfo = await _channel!.QueueDeclarePassiveAsync(_config.QueueName, cancellationToken); return (int)queueInfo.MessageCount; } @@ -250,12 +243,12 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable // Start consuming messages (event-driven, non-blocking) await _channel.BasicConsumeAsync( - queue: QueueName, + queue: _config.QueueName, autoAck: false, consumer: consumer, cancellationToken: cancellationToken); - _logger.LogInformation("RabbitMQ consumer started for queue: {QueueName}", QueueName); + _logger.LogInformation("RabbitMQ consumer started for queue: {QueueName}", _config.QueueName); } public void Dispose()