chore: add documentation comments and stylecop rules
This commit is contained in:
@@ -22,17 +22,21 @@ public sealed class RabbitMqMessagePublisher(RabbitMqConnectionFactory connectio
|
||||
/// <inheritdoc />
|
||||
public Task PublishAsync<T>(string routingKey, T message, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 确保通道可用
|
||||
EnsureChannel();
|
||||
var options = optionsMonitor.CurrentValue;
|
||||
|
||||
var channel = _channel ?? throw new InvalidOperationException("RabbitMQ channel is not available.");
|
||||
// 2. 声明交换机
|
||||
channel.ExchangeDeclare(options.Exchange, options.ExchangeType, durable: true, autoDelete: false);
|
||||
// 3. 序列化消息并设置属性
|
||||
var body = serializer.Serialize(message);
|
||||
var props = channel.CreateBasicProperties();
|
||||
props.ContentType = "application/json";
|
||||
props.DeliveryMode = 2;
|
||||
props.MessageId = Guid.NewGuid().ToString("N");
|
||||
|
||||
// 4. 发布消息
|
||||
channel.BasicPublish(options.Exchange, routingKey, props, body);
|
||||
logger.LogDebug("发布消息到交换机 {Exchange} RoutingKey {RoutingKey}", options.Exchange, routingKey);
|
||||
return Task.CompletedTask;
|
||||
|
||||
@@ -21,16 +21,19 @@ public sealed class RabbitMqMessageSubscriber(RabbitMqConnectionFactory connecti
|
||||
/// <inheritdoc />
|
||||
public async Task SubscribeAsync<T>(string queue, string routingKey, Func<T, CancellationToken, Task<bool>> handler, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 确保通道可用
|
||||
EnsureChannel();
|
||||
var options = optionsMonitor.CurrentValue;
|
||||
|
||||
var channel = _channel ?? throw new InvalidOperationException("RabbitMQ channel is not available.");
|
||||
|
||||
// 2. 声明交换机、队列及绑定
|
||||
channel.ExchangeDeclare(options.Exchange, options.ExchangeType, durable: true, autoDelete: false);
|
||||
channel.QueueDeclare(queue, durable: true, exclusive: false, autoDelete: false);
|
||||
channel.QueueBind(queue, options.Exchange, routingKey);
|
||||
channel.BasicQos(0, options.PrefetchCount, global: false);
|
||||
|
||||
// 3. 设置消费者回调
|
||||
var consumer = new AsyncEventingBasicConsumer(channel);
|
||||
consumer.Received += async (_, ea) =>
|
||||
{
|
||||
@@ -61,6 +64,7 @@ public sealed class RabbitMqMessageSubscriber(RabbitMqConnectionFactory connecti
|
||||
}
|
||||
};
|
||||
|
||||
// 4. 开始消费
|
||||
channel.BasicConsume(queue, autoAck: false, consumer);
|
||||
await Task.CompletedTask.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user