Remove Autofac from the EventBusRabbitMQ project

Refactor the EventBusRabbitMQ class
This commit is contained in:
Georgy Sayganov 2021-05-24 14:33:22 +03:00
parent 1cbd218dc4
commit 2edb81974e
2 changed files with 102 additions and 87 deletions

View File

@ -1,45 +1,47 @@
using Autofac; using System;
using System.Net.Sockets;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Polly; using Polly;
using Polly.Retry;
using RabbitMQ.Client; using RabbitMQ.Client;
using RabbitMQ.Client.Events; using RabbitMQ.Client.Events;
using RabbitMQ.Client.Exceptions; using RabbitMQ.Client.Exceptions;
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
{ {
public class EventBusRabbitMQ : IEventBus, IDisposable public class EventBusRabbitMQ : IEventBus, IDisposable
{ {
const string BROKER_NAME = "eshop_event_bus"; private const string BrokerName = "eshop_event_bus";
const string AUTOFAC_SCOPE_NAME = "eshop_event_bus";
private readonly IRabbitMQPersistentConnection _persistentConnection; private readonly IRabbitMQPersistentConnection _persistentConnection;
private readonly ILogger<EventBusRabbitMQ> _logger; private readonly ILogger<EventBusRabbitMQ> _logger;
private readonly IEventBusSubscriptionsManager _subsManager; private readonly IEventBusSubscriptionsManager _subsManager;
private readonly ILifetimeScope _autofac; private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly int _retryCount; private readonly int _retryCount;
private IModel _consumerChannel; private IModel _consumerChannel;
private string _queueName; private string _queueName;
public EventBusRabbitMQ(IRabbitMQPersistentConnection persistentConnection, ILogger<EventBusRabbitMQ> logger, public EventBusRabbitMQ(IRabbitMQPersistentConnection persistentConnection,
ILifetimeScope autofac, IEventBusSubscriptionsManager subsManager, string queueName = null, int retryCount = 5) ILogger<EventBusRabbitMQ> logger,
IServiceScopeFactory serviceScopeFactory,
IEventBusSubscriptionsManager subsManager,
string queueName = null,
int retryCount = 5)
{ {
_persistentConnection = persistentConnection ?? throw new ArgumentNullException(nameof(persistentConnection)); _persistentConnection = persistentConnection ?? throw new ArgumentNullException(nameof(persistentConnection));
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
_subsManager = subsManager ?? new InMemoryEventBusSubscriptionsManager(); _subsManager = subsManager ?? new InMemoryEventBusSubscriptionsManager();
_queueName = queueName; _queueName = queueName;
_consumerChannel = CreateConsumerChannel(); _consumerChannel = CreateConsumerChannel();
_autofac = autofac; _serviceScopeFactory = serviceScopeFactory;
_retryCount = retryCount; _retryCount = retryCount;
_subsManager.OnEventRemoved += SubsManager_OnEventRemoved; _subsManager.OnEventRemoved += SubsManager_OnEventRemoved;
} }
@ -53,15 +55,15 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
using (var channel = _persistentConnection.CreateModel()) using (var channel = _persistentConnection.CreateModel())
{ {
channel.QueueUnbind(queue: _queueName, channel.QueueUnbind(_queueName, BrokerName, eventName);
exchange: BROKER_NAME,
routingKey: eventName);
if (_subsManager.IsEmpty) if (!_subsManager.IsEmpty)
{ {
_queueName = string.Empty; return;
_consumerChannel.Close();
} }
_queueName = string.Empty;
_consumerChannel.Close();
} }
} }
@ -72,23 +74,27 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
_persistentConnection.TryConnect(); _persistentConnection.TryConnect();
} }
var policy = RetryPolicy.Handle<BrokerUnreachableException>() var policy = Policy.Handle<BrokerUnreachableException>()
.Or<SocketException>() .Or<SocketException>()
.WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) => .WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
{ (ex, time) =>
_logger.LogWarning(ex, "Could not publish event: {EventId} after {Timeout}s ({ExceptionMessage})", @event.Id, $"{time.TotalSeconds:n1}", ex.Message); {
}); _logger.LogWarning(ex,
"Could not publish event: {EventId} after {Timeout}s ({ExceptionMessage})", @event.Id,
$"{time.TotalSeconds:n1}", ex.Message);
});
var eventName = @event.GetType().Name; var eventName = @event.GetType().Name;
_logger.LogTrace("Creating RabbitMQ channel to publish event: {EventId} ({EventName})", @event.Id, eventName); _logger.LogTrace("Creating RabbitMQ channel to publish event: {EventId} ({EventName})", @event.Id,
eventName);
using (var channel = _persistentConnection.CreateModel()) using (var channel = _persistentConnection.CreateModel())
{ {
_logger.LogTrace("Declaring RabbitMQ exchange to publish event: {EventId}", @event.Id); _logger.LogTrace("Declaring RabbitMQ exchange to publish event: {EventId}", @event.Id);
channel.ExchangeDeclare(exchange: BROKER_NAME, type: "direct"); channel.ExchangeDeclare(BrokerName, "direct");
var body = JsonSerializer.SerializeToUtf8Bytes(@event, @event.GetType(), new JsonSerializerOptions var body = JsonSerializer.SerializeToUtf8Bytes(@event, @event.GetType(), new JsonSerializerOptions
{ {
WriteIndented = true WriteIndented = true
@ -97,86 +103,81 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
policy.Execute(() => policy.Execute(() =>
{ {
var properties = channel.CreateBasicProperties(); var properties = channel.CreateBasicProperties();
properties.DeliveryMode = 2; // persistent properties.DeliveryMode = 2;
_logger.LogTrace("Publishing event to RabbitMQ: {EventId}", @event.Id); _logger.LogTrace("Publishing event to RabbitMQ: {EventId}", @event.Id);
channel.BasicPublish( channel.BasicPublish(
exchange: BROKER_NAME, BrokerName,
routingKey: eventName, eventName,
mandatory: true, true,
basicProperties: properties, properties,
body: body); body);
}); });
} }
} }
public void SubscribeDynamic<TH>(string eventName) public void SubscribeDynamic<TH>(string eventName) where TH : IDynamicIntegrationEventHandler
where TH : IDynamicIntegrationEventHandler
{ {
_logger.LogInformation("Subscribing to dynamic event {EventName} with {EventHandler}", eventName, typeof(TH).GetGenericTypeName()); _logger.LogInformation("Subscribing to dynamic event {EventName} with {EventHandler}", eventName,
typeof(TH).GetGenericTypeName());
DoInternalSubscription(eventName); DoInternalSubscription(eventName);
_subsManager.AddDynamicSubscription<TH>(eventName); _subsManager.AddDynamicSubscription<TH>(eventName);
StartBasicConsume(); StartBasicConsume();
} }
public void Subscribe<T, TH>() public void Subscribe<T, TH>() where T : IntegrationEvent where TH : IIntegrationEventHandler<T>
where T : IntegrationEvent
where TH : IIntegrationEventHandler<T>
{ {
var eventName = _subsManager.GetEventKey<T>(); var eventName = _subsManager.GetEventKey<T>();
DoInternalSubscription(eventName); DoInternalSubscription(eventName);
_logger.LogInformation("Subscribing to event {EventName} with {EventHandler}", eventName, typeof(TH).GetGenericTypeName()); _logger.LogInformation("Subscribing to event {EventName} with {EventHandler}", eventName,
typeof(TH).GetGenericTypeName());
_subsManager.AddSubscription<T, TH>(); _subsManager.AddSubscription<T, TH>();
StartBasicConsume(); StartBasicConsume();
} }
private void DoInternalSubscription(string eventName) private void DoInternalSubscription(string eventName)
{ {
var containsKey = _subsManager.HasSubscriptionsForEvent(eventName); var containsKey = _subsManager.HasSubscriptionsForEvent(eventName);
if (!containsKey)
{
if (!_persistentConnection.IsConnected)
{
_persistentConnection.TryConnect();
}
using (var channel = _persistentConnection.CreateModel()) if (containsKey)
{ {
channel.QueueBind(queue: _queueName, return;
exchange: BROKER_NAME, }
routingKey: eventName);
} if (!_persistentConnection.IsConnected)
{
_persistentConnection.TryConnect();
}
using (var channel = _persistentConnection.CreateModel())
{
channel.QueueBind(_queueName, BrokerName, eventName);
} }
} }
public void Unsubscribe<T, TH>() public void Unsubscribe<T, TH>() where T : IntegrationEvent where TH : IIntegrationEventHandler<T>
where T : IntegrationEvent
where TH : IIntegrationEventHandler<T>
{ {
var eventName = _subsManager.GetEventKey<T>(); var eventName = _subsManager.GetEventKey<T>();
_logger.LogInformation("Unsubscribing from event {EventName}", eventName); _logger.LogInformation("Unsubscribing from event {EventName}", eventName);
_subsManager.RemoveSubscription<T, TH>(); _subsManager.RemoveSubscription<T, TH>();
} }
public void UnsubscribeDynamic<TH>(string eventName) public void UnsubscribeDynamic<TH>(string eventName) where TH : IDynamicIntegrationEventHandler
where TH : IDynamicIntegrationEventHandler
{ {
_subsManager.RemoveDynamicSubscription<TH>(eventName); _subsManager.RemoveDynamicSubscription<TH>(eventName);
} }
public void Dispose() public void Dispose()
{ {
if (_consumerChannel != null) _consumerChannel?.Dispose();
{
_consumerChannel.Dispose();
}
_subsManager.Clear(); _subsManager.Clear();
} }
@ -191,9 +192,9 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
consumer.Received += Consumer_Received; consumer.Received += Consumer_Received;
_consumerChannel.BasicConsume( _consumerChannel.BasicConsume(
queue: _queueName, _queueName,
autoAck: false, false,
consumer: consumer); consumer);
} }
else else
{ {
@ -223,7 +224,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
// Even on exception we take the message off the queue. // Even on exception we take the message off the queue.
// in a REAL WORLD app this should be handled with a Dead Letter Exchange (DLX). // in a REAL WORLD app this should be handled with a Dead Letter Exchange (DLX).
// For more information see: https://www.rabbitmq.com/dlx.html // For more information see: https://www.rabbitmq.com/dlx.html
_consumerChannel.BasicAck(eventArgs.DeliveryTag, multiple: false); _consumerChannel.BasicAck(eventArgs.DeliveryTag, false);
} }
private IModel CreateConsumerChannel() private IModel CreateConsumerChannel()
@ -237,21 +238,19 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
var channel = _persistentConnection.CreateModel(); var channel = _persistentConnection.CreateModel();
channel.ExchangeDeclare(exchange: BROKER_NAME, channel.ExchangeDeclare(BrokerName, "direct");
type: "direct"); channel.QueueDeclare(_queueName,
true,
channel.QueueDeclare(queue: _queueName, false,
durable: true, false,
exclusive: false, null);
autoDelete: false,
arguments: null);
channel.CallbackException += (sender, ea) => channel.CallbackException += (sender, ea) =>
{ {
_logger.LogWarning(ea.Exception, "Recreating RabbitMQ consumer channel"); _logger.LogWarning(ea.Exception, "Recreating RabbitMQ consumer channel");
_consumerChannel.Dispose(); _consumerChannel.Dispose();
_consumerChannel = CreateConsumerChannel(); _consumerChannel = CreateConsumerChannel();
StartBasicConsume(); StartBasicConsume();
}; };
@ -264,29 +263,46 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
if (_subsManager.HasSubscriptionsForEvent(eventName)) if (_subsManager.HasSubscriptionsForEvent(eventName))
{ {
using (var scope = _autofac.BeginLifetimeScope(AUTOFAC_SCOPE_NAME)) using (var scope = _serviceScopeFactory.CreateScope())
{ {
var subscriptions = _subsManager.GetHandlersForEvent(eventName); var subscriptions = _subsManager.GetHandlersForEvent(eventName);
foreach (var subscription in subscriptions) foreach (var subscription in subscriptions)
{ {
if (subscription.IsDynamic) if (subscription.IsDynamic)
{ {
var handler = scope.ResolveOptional(subscription.HandlerType) as IDynamicIntegrationEventHandler; if (!(scope.ServiceProvider.GetService(subscription.HandlerType) is
if (handler == null) continue; IDynamicIntegrationEventHandler handler))
using dynamic eventData = JsonDocument.Parse(message); {
continue;
}
using dynamic eventData = JsonDocument.Parse(message);
await Task.Yield(); await Task.Yield();
await handler.Handle(eventData); await handler.Handle(eventData);
} }
else else
{ {
var handler = scope.ResolveOptional(subscription.HandlerType); var handler = scope.ServiceProvider.GetService(subscription.HandlerType);
if (handler == null) continue; if (handler == null)
{
continue;
}
var eventType = _subsManager.GetEventTypeByName(eventName); var eventType = _subsManager.GetEventTypeByName(eventName);
var integrationEvent = JsonSerializer.Deserialize(message, eventType, new JsonSerializerOptions() { PropertyNameCaseInsensitive= true}); var integrationEvent = JsonSerializer.Deserialize(message, eventType,
new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType); var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType);
await Task.Yield(); await Task.Yield();
await (Task)concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent }); await (Task) concreteType.GetMethod("Handle").Invoke(handler, new[]
{
integrationEvent
});
} }
} }
} }
@ -297,4 +313,4 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
} }
} }
} }
} }

View File

@ -6,7 +6,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Autofac" Version="6.1.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Polly" Version="7.2.1" /> <PackageReference Include="Polly" Version="7.2.1" />