* Created global using file for catalog.api * Moved individual usings statements to globalusing * Updated catalog.api project * Fixed local run bug for catalog.api * Included globalusing for payment.api * Refactored namespace statement for payment.api * Moved namespaces to ordering.domain project * Included globalusing for ordering.domain project * Included globalusings for ordering.infrastructure project * Refactored namespaces for ordering.infrastructure project * Updated relevant packages in ordering.infrastructure project * Included globalusings for ordering.signalrHub project * Moved all the namespace to globalusings * Updated packages in ordering.signalrHub csproj file * Refactored namespace statements in catalog.api project * Fixed namespace name in ordering.domain * Included global usings for ordering.api project * Moved all usings to globalusing file * Updated ordering.api csproj project * Fixed bug in statup.cs * Updated ordering.unittests.csproj file * Included globalusings in webhooks.api project * Moved using statements to globalusing file in webhooks.api * Included globalusing for web.bff.shoppping aggregator project * Moved namespaces to globalusing shopping aggregator * Included globalusing mobile.bff.shoppping project * Moved namespaces to globalusing file * Included globalusing for eventbus project * Moved namespaces to global usings for eventbus * Included globalusing for EventBusRabbitMQ project * Moved using statements to EventBusRabbitMQ project * Included global using in EventBusServiceBus project * Moved using statements to globalusing for EventBusServiceBus * Included globalusing file for IntegrationEventLogEF project * Move using statements to globalusing file * Updated packages of IntegrationEventLogEF project * Included globalusing to Devspaces.Support project * Moved using statements to globalusing Devspaces * Updated dependent packages for Devspaces.Support.csproj * Fixed bug in Basket API * Fixed bug in catalog.api * Fixed bug Identity.API * Included globalusing to Basket.UnitTest project * Moved namespaces to Basket.UnitTest project * Updated packages of Basket.UnitTest csproj * Included globalusing for Basket.FunctionalTests project * Included file-scoped namespaces Basket.FunctionalTests * Updated packages of Basket.FunctionalTests.csproj file * Updated catalog unit test project to Net 6.0 * Included global usings for Catalog.FunctionalTests * Included file-scope namespace catalog.functionaltests * Updated packages of catalog.functionaltest csproj * Included MigrateDbContext method in HostExtensions * Included globalusing for ordering.UnitTests project * Included file-scope statement for Ordering.UnitTest project * Included globalusing for Ordering.FunctionalTests * Included file-scope namespace statement for using * Updated packages in Ordering.FunctionalTests.csproj * Apply suggestions from code review Co-authored-by: David Pine <david.pine@microsoft.com> * Apply suggestions from code review Co-authored-by: David Pine <david.pine@microsoft.com> * Update src/Services/Ordering/Ordering.API/Startup.cs Co-authored-by: David Pine <david.pine@microsoft.com> * Update src/Services/Ordering/Ordering.Domain/Events/OrderStatusChangedToPaidDomainEvent.cs Co-authored-by: David Pine <david.pine@microsoft.com> * Update src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToSubmittedIntegrationEventHandler.cs Co-authored-by: David Pine <david.pine@microsoft.com> * Update src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs Co-authored-by: David Pine <david.pine@microsoft.com> * Apply suggestions from code review Co-authored-by: David Pine <david.pine@microsoft.com> * Apply suggestions from code review Co-authored-by: David Pine <david.pine@microsoft.com> Co-authored-by: David Pine <david.pine@microsoft.com>
280 lines
9.8 KiB
C#
280 lines
9.8 KiB
C#
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
|
|
|
|
public class EventBusRabbitMQ : IEventBus, IDisposable
|
|
{
|
|
const string BROKER_NAME = "eshop_event_bus";
|
|
const string AUTOFAC_SCOPE_NAME = "eshop_event_bus";
|
|
|
|
private readonly IRabbitMQPersistentConnection _persistentConnection;
|
|
private readonly ILogger<EventBusRabbitMQ> _logger;
|
|
private readonly IEventBusSubscriptionsManager _subsManager;
|
|
private readonly ILifetimeScope _autofac;
|
|
private readonly int _retryCount;
|
|
|
|
private IModel _consumerChannel;
|
|
private string _queueName;
|
|
|
|
public EventBusRabbitMQ(IRabbitMQPersistentConnection persistentConnection, ILogger<EventBusRabbitMQ> logger,
|
|
ILifetimeScope autofac, IEventBusSubscriptionsManager subsManager, string queueName = null, int retryCount = 5)
|
|
{
|
|
_persistentConnection = persistentConnection ?? throw new ArgumentNullException(nameof(persistentConnection));
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
_subsManager = subsManager ?? new InMemoryEventBusSubscriptionsManager();
|
|
_queueName = queueName;
|
|
_consumerChannel = CreateConsumerChannel();
|
|
_autofac = autofac;
|
|
_retryCount = retryCount;
|
|
_subsManager.OnEventRemoved += SubsManager_OnEventRemoved;
|
|
}
|
|
|
|
private void SubsManager_OnEventRemoved(object sender, string eventName)
|
|
{
|
|
if (!_persistentConnection.IsConnected)
|
|
{
|
|
_persistentConnection.TryConnect();
|
|
}
|
|
|
|
using (var channel = _persistentConnection.CreateModel())
|
|
{
|
|
channel.QueueUnbind(queue: _queueName,
|
|
exchange: BROKER_NAME,
|
|
routingKey: eventName);
|
|
|
|
if (_subsManager.IsEmpty)
|
|
{
|
|
_queueName = string.Empty;
|
|
_consumerChannel.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Publish(IntegrationEvent @event)
|
|
{
|
|
if (!_persistentConnection.IsConnected)
|
|
{
|
|
_persistentConnection.TryConnect();
|
|
}
|
|
|
|
var policy = RetryPolicy.Handle<BrokerUnreachableException>()
|
|
.Or<SocketException>()
|
|
.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);
|
|
});
|
|
|
|
var eventName = @event.GetType().Name;
|
|
|
|
_logger.LogTrace("Creating RabbitMQ channel to publish event: {EventId} ({EventName})", @event.Id, eventName);
|
|
|
|
using (var channel = _persistentConnection.CreateModel())
|
|
{
|
|
_logger.LogTrace("Declaring RabbitMQ exchange to publish event: {EventId}", @event.Id);
|
|
|
|
channel.ExchangeDeclare(exchange: BROKER_NAME, type: "direct");
|
|
|
|
var body = JsonSerializer.SerializeToUtf8Bytes(@event, @event.GetType(), new JsonSerializerOptions
|
|
{
|
|
WriteIndented = true
|
|
});
|
|
|
|
policy.Execute(() =>
|
|
{
|
|
var properties = channel.CreateBasicProperties();
|
|
properties.DeliveryMode = 2; // persistent
|
|
|
|
_logger.LogTrace("Publishing event to RabbitMQ: {EventId}", @event.Id);
|
|
|
|
channel.BasicPublish(
|
|
exchange: BROKER_NAME,
|
|
routingKey: eventName,
|
|
mandatory: true,
|
|
basicProperties: properties,
|
|
body: body);
|
|
});
|
|
}
|
|
}
|
|
|
|
public void SubscribeDynamic<TH>(string eventName)
|
|
where TH : IDynamicIntegrationEventHandler
|
|
{
|
|
_logger.LogInformation("Subscribing to dynamic event {EventName} with {EventHandler}", eventName, typeof(TH).GetGenericTypeName());
|
|
|
|
DoInternalSubscription(eventName);
|
|
_subsManager.AddDynamicSubscription<TH>(eventName);
|
|
StartBasicConsume();
|
|
}
|
|
|
|
public void Subscribe<T, TH>()
|
|
where T : IntegrationEvent
|
|
where TH : IIntegrationEventHandler<T>
|
|
{
|
|
var eventName = _subsManager.GetEventKey<T>();
|
|
DoInternalSubscription(eventName);
|
|
|
|
_logger.LogInformation("Subscribing to event {EventName} with {EventHandler}", eventName, typeof(TH).GetGenericTypeName());
|
|
|
|
_subsManager.AddSubscription<T, TH>();
|
|
StartBasicConsume();
|
|
}
|
|
|
|
private void DoInternalSubscription(string eventName)
|
|
{
|
|
var containsKey = _subsManager.HasSubscriptionsForEvent(eventName);
|
|
if (!containsKey)
|
|
{
|
|
if (!_persistentConnection.IsConnected)
|
|
{
|
|
_persistentConnection.TryConnect();
|
|
}
|
|
|
|
_consumerChannel.QueueBind(queue: _queueName,
|
|
exchange: BROKER_NAME,
|
|
routingKey: eventName);
|
|
}
|
|
}
|
|
|
|
public void Unsubscribe<T, TH>()
|
|
where T : IntegrationEvent
|
|
where TH : IIntegrationEventHandler<T>
|
|
{
|
|
var eventName = _subsManager.GetEventKey<T>();
|
|
|
|
_logger.LogInformation("Unsubscribing from event {EventName}", eventName);
|
|
|
|
_subsManager.RemoveSubscription<T, TH>();
|
|
}
|
|
|
|
public void UnsubscribeDynamic<TH>(string eventName)
|
|
where TH : IDynamicIntegrationEventHandler
|
|
{
|
|
_subsManager.RemoveDynamicSubscription<TH>(eventName);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_consumerChannel != null)
|
|
{
|
|
_consumerChannel.Dispose();
|
|
}
|
|
|
|
_subsManager.Clear();
|
|
}
|
|
|
|
private void StartBasicConsume()
|
|
{
|
|
_logger.LogTrace("Starting RabbitMQ basic consume");
|
|
|
|
if (_consumerChannel != null)
|
|
{
|
|
var consumer = new AsyncEventingBasicConsumer(_consumerChannel);
|
|
|
|
consumer.Received += Consumer_Received;
|
|
|
|
_consumerChannel.BasicConsume(
|
|
queue: _queueName,
|
|
autoAck: false,
|
|
consumer: consumer);
|
|
}
|
|
else
|
|
{
|
|
_logger.LogError("StartBasicConsume can't call on _consumerChannel == null");
|
|
}
|
|
}
|
|
|
|
private async Task Consumer_Received(object sender, BasicDeliverEventArgs eventArgs)
|
|
{
|
|
var eventName = eventArgs.RoutingKey;
|
|
var message = Encoding.UTF8.GetString(eventArgs.Body.Span);
|
|
|
|
try
|
|
{
|
|
if (message.ToLowerInvariant().Contains("throw-fake-exception"))
|
|
{
|
|
throw new InvalidOperationException($"Fake exception requested: \"{message}\"");
|
|
}
|
|
|
|
await ProcessEvent(eventName, message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, "----- ERROR Processing message \"{Message}\"", message);
|
|
}
|
|
|
|
// 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).
|
|
// For more information see: https://www.rabbitmq.com/dlx.html
|
|
_consumerChannel.BasicAck(eventArgs.DeliveryTag, multiple: false);
|
|
}
|
|
|
|
private IModel CreateConsumerChannel()
|
|
{
|
|
if (!_persistentConnection.IsConnected)
|
|
{
|
|
_persistentConnection.TryConnect();
|
|
}
|
|
|
|
_logger.LogTrace("Creating RabbitMQ consumer channel");
|
|
|
|
var channel = _persistentConnection.CreateModel();
|
|
|
|
channel.ExchangeDeclare(exchange: BROKER_NAME,
|
|
type: "direct");
|
|
|
|
channel.QueueDeclare(queue: _queueName,
|
|
durable: true,
|
|
exclusive: false,
|
|
autoDelete: false,
|
|
arguments: null);
|
|
|
|
channel.CallbackException += (sender, ea) =>
|
|
{
|
|
_logger.LogWarning(ea.Exception, "Recreating RabbitMQ consumer channel");
|
|
|
|
_consumerChannel.Dispose();
|
|
_consumerChannel = CreateConsumerChannel();
|
|
StartBasicConsume();
|
|
};
|
|
|
|
return channel;
|
|
}
|
|
|
|
private async Task ProcessEvent(string eventName, string message)
|
|
{
|
|
_logger.LogTrace("Processing RabbitMQ event: {EventName}", eventName);
|
|
|
|
if (_subsManager.HasSubscriptionsForEvent(eventName))
|
|
{
|
|
using (var scope = _autofac.BeginLifetimeScope(AUTOFAC_SCOPE_NAME))
|
|
{
|
|
var subscriptions = _subsManager.GetHandlersForEvent(eventName);
|
|
foreach (var subscription in subscriptions)
|
|
{
|
|
if (subscription.IsDynamic)
|
|
{
|
|
var handler = scope.ResolveOptional(subscription.HandlerType) as IDynamicIntegrationEventHandler;
|
|
if (handler == null) continue;
|
|
using dynamic eventData = JsonDocument.Parse(message);
|
|
await Task.Yield();
|
|
await handler.Handle(eventData);
|
|
}
|
|
else
|
|
{
|
|
var handler = scope.ResolveOptional(subscription.HandlerType);
|
|
if (handler == null) continue;
|
|
var eventType = _subsManager.GetEventTypeByName(eventName);
|
|
var integrationEvent = JsonSerializer.Deserialize(message, eventType, new JsonSerializerOptions() { PropertyNameCaseInsensitive= true});
|
|
var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType);
|
|
|
|
await Task.Yield();
|
|
await (Task)concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_logger.LogWarning("No subscription for RabbitMQ event: {EventName}", eventName);
|
|
}
|
|
}
|
|
}
|