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
|
/// Network recovery interval in seconds
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int NetworkRecoveryIntervalSeconds { get; set; } = 10;
|
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)
|
// Lazy<T> ensures InitAsync is called only ONCE (thread-safe)
|
||||||
private readonly Lazy<Task> _initializationTask;
|
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
|
#pragma warning disable CS8618 // channel and connection are initialized in InitAsync, not in constructor
|
||||||
public RabbitMqEmailQueue(IOptions<RabbitMqConfiguration> config, ILogger<RabbitMqEmailQueue> logger)
|
public RabbitMqEmailQueue(IOptions<RabbitMqConfiguration> config, ILogger<RabbitMqEmailQueue> logger)
|
||||||
#pragma warning restore CS8618
|
#pragma warning restore CS8618
|
||||||
@@ -68,14 +61,14 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable
|
|||||||
|
|
||||||
// Declare Dead Letter Queue (DLQ) exchange
|
// Declare Dead Letter Queue (DLQ) exchange
|
||||||
await _channel.ExchangeDeclareAsync(
|
await _channel.ExchangeDeclareAsync(
|
||||||
exchange: DlqExchangeName,
|
exchange: _config.DlqExchangeName,
|
||||||
type: ExchangeType.Direct,
|
type: ExchangeType.Direct,
|
||||||
durable: true,
|
durable: true,
|
||||||
autoDelete: false);
|
autoDelete: false);
|
||||||
|
|
||||||
// Declare Dead Letter Queue (DLQ)
|
// Declare Dead Letter Queue (DLQ)
|
||||||
await _channel.QueueDeclareAsync(
|
await _channel.QueueDeclareAsync(
|
||||||
queue: DlqQueueName,
|
queue: _config.DlqQueueName,
|
||||||
durable: true,
|
durable: true,
|
||||||
exclusive: false,
|
exclusive: false,
|
||||||
autoDelete: false,
|
autoDelete: false,
|
||||||
@@ -83,13 +76,13 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable
|
|||||||
|
|
||||||
// Bind DLQ to DLQ exchange
|
// Bind DLQ to DLQ exchange
|
||||||
await _channel.QueueBindAsync(
|
await _channel.QueueBindAsync(
|
||||||
queue: DlqQueueName,
|
queue: _config.DlqQueueName,
|
||||||
exchange: DlqExchangeName,
|
exchange: _config.DlqExchangeName,
|
||||||
routingKey: DlqRoutingKey);
|
routingKey: _config.DlqRoutingKey);
|
||||||
|
|
||||||
// Declare main exchange (Direct type for routing)
|
// Declare main exchange (Direct type for routing)
|
||||||
await _channel.ExchangeDeclareAsync(
|
await _channel.ExchangeDeclareAsync(
|
||||||
exchange: ExchangeName,
|
exchange: _config.ExchangeName,
|
||||||
type: ExchangeType.Direct,
|
type: ExchangeType.Direct,
|
||||||
durable: true,
|
durable: true,
|
||||||
autoDelete: false);
|
autoDelete: false);
|
||||||
@@ -97,12 +90,12 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable
|
|||||||
// Declare main queue (durable for persistence) with DLQ arguments
|
// Declare main queue (durable for persistence) with DLQ arguments
|
||||||
var queueArgs = new Dictionary<string, object?>
|
var queueArgs = new Dictionary<string, object?>
|
||||||
{
|
{
|
||||||
{ "x-dead-letter-exchange", DlqExchangeName },
|
{ "x-dead-letter-exchange", _config.DlqExchangeName },
|
||||||
{ "x-dead-letter-routing-key", DlqRoutingKey }
|
{ "x-dead-letter-routing-key", _config.DlqRoutingKey }
|
||||||
};
|
};
|
||||||
|
|
||||||
await _channel.QueueDeclareAsync(
|
await _channel.QueueDeclareAsync(
|
||||||
queue: QueueName,
|
queue: _config.QueueName,
|
||||||
durable: true,
|
durable: true,
|
||||||
exclusive: false,
|
exclusive: false,
|
||||||
autoDelete: false,
|
autoDelete: false,
|
||||||
@@ -110,11 +103,11 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable
|
|||||||
|
|
||||||
// Bind main queue to exchange with routing key
|
// Bind main queue to exchange with routing key
|
||||||
await _channel.QueueBindAsync(
|
await _channel.QueueBindAsync(
|
||||||
queue: QueueName,
|
queue: _config.QueueName,
|
||||||
exchange: ExchangeName,
|
exchange: _config.ExchangeName,
|
||||||
routingKey: RoutingKey);
|
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>
|
/// <summary>
|
||||||
@@ -141,8 +134,8 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable
|
|||||||
};
|
};
|
||||||
|
|
||||||
await _channel!.BasicPublishAsync(
|
await _channel!.BasicPublishAsync(
|
||||||
exchange: ExchangeName,
|
exchange: _config.ExchangeName,
|
||||||
routingKey: RoutingKey,
|
routingKey: _config.RoutingKey,
|
||||||
mandatory: false,
|
mandatory: false,
|
||||||
basicProperties: properties,
|
basicProperties: properties,
|
||||||
body: body,
|
body: body,
|
||||||
@@ -153,7 +146,7 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable
|
|||||||
{
|
{
|
||||||
await EnsureInitializedAsync(); // Initialize on first call
|
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)
|
if (result == null)
|
||||||
return null;
|
return null;
|
||||||
@@ -180,7 +173,7 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable
|
|||||||
{
|
{
|
||||||
await EnsureInitializedAsync(); // Initialize on first call
|
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;
|
return (int)queueInfo.MessageCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,12 +243,12 @@ public class RabbitMqEmailQueue : IEmailQueue, IDisposable
|
|||||||
|
|
||||||
// Start consuming messages (event-driven, non-blocking)
|
// Start consuming messages (event-driven, non-blocking)
|
||||||
await _channel.BasicConsumeAsync(
|
await _channel.BasicConsumeAsync(
|
||||||
queue: QueueName,
|
queue: _config.QueueName,
|
||||||
autoAck: false,
|
autoAck: false,
|
||||||
consumer: consumer,
|
consumer: consumer,
|
||||||
cancellationToken: cancellationToken);
|
cancellationToken: cancellationToken);
|
||||||
|
|
||||||
_logger.LogInformation("RabbitMQ consumer started for queue: {QueueName}", QueueName);
|
_logger.LogInformation("RabbitMQ consumer started for queue: {QueueName}", _config.QueueName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|||||||
Reference in New Issue
Block a user