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.
This commit is contained in:
@@ -44,4 +44,11 @@ public class RabbitMqConfiguration
|
||||
/// Network recovery interval in seconds
|
||||
/// </summary>
|
||||
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!;
|
||||
}
|
||||
|
||||
@@ -26,13 +26,6 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable
|
||||
// Lazy<T> ensures InitAsync is called only ONCE (thread-safe)
|
||||
private readonly Lazy<Task> _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<RabbitMqConfiguration> config, ILogger<RabbitMqEmailQueue> 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<string, object?>
|
||||
{
|
||||
{ "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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user