2017-05-24 15:34:55 +02:00
|
|
|
|
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus
|
|
|
|
|
{
|
2017-08-17 13:00:48 +02:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Microsoft.Azure.ServiceBus;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
2017-05-24 15:34:55 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
2021-05-03 16:36:31 +05:30
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-08-17 13:00:48 +02:00
|
|
|
|
using System;
|
2017-05-24 15:34:55 +02:00
|
|
|
|
using System.Text;
|
2021-05-03 16:36:31 +05:30
|
|
|
|
using System.Text.Json;
|
2017-05-24 15:34:55 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
public class EventBusServiceBus : IEventBus
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceBusPersisterConnection _serviceBusPersisterConnection;
|
|
|
|
|
private readonly ILogger<EventBusServiceBus> _logger;
|
|
|
|
|
private readonly IEventBusSubscriptionsManager _subsManager;
|
2017-06-26 18:05:02 +02:00
|
|
|
|
private readonly ILifetimeScope _autofac;
|
|
|
|
|
private readonly string AUTOFAC_SCOPE_NAME = "eshop_event_bus";
|
2019-07-19 11:49:25 -07:00
|
|
|
|
private const string INTEGRATION_EVENT_SUFFIX = "IntegrationEvent";
|
2017-06-26 18:05:02 +02:00
|
|
|
|
|
2018-05-30 22:51:49 +02:00
|
|
|
|
public EventBusServiceBus(IServiceBusPersisterConnection serviceBusPersisterConnection,
|
2021-03-09 12:02:28 +02:00
|
|
|
|
ILogger<EventBusServiceBus> logger, IEventBusSubscriptionsManager subsManager, ILifetimeScope autofac)
|
2017-05-24 15:34:55 +02:00
|
|
|
|
{
|
|
|
|
|
_serviceBusPersisterConnection = serviceBusPersisterConnection;
|
2019-03-04 19:08:14 +00:00
|
|
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
2017-05-24 15:34:55 +02:00
|
|
|
|
_subsManager = subsManager ?? new InMemoryEventBusSubscriptionsManager();
|
2017-06-26 18:05:02 +02:00
|
|
|
|
_autofac = autofac;
|
2017-05-24 16:26:40 +02:00
|
|
|
|
|
2017-05-26 01:35:19 +02:00
|
|
|
|
RemoveDefaultRule();
|
|
|
|
|
RegisterSubscriptionClientMessageHandler();
|
2017-05-24 15:34:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Publish(IntegrationEvent @event)
|
|
|
|
|
{
|
2019-07-19 11:49:25 -07:00
|
|
|
|
var eventName = @event.GetType().Name.Replace(INTEGRATION_EVENT_SUFFIX, "");
|
2021-05-03 16:36:31 +05:30
|
|
|
|
var jsonMessage = JsonSerializer.Serialize(@event);
|
2017-05-24 15:34:55 +02:00
|
|
|
|
var body = Encoding.UTF8.GetBytes(jsonMessage);
|
|
|
|
|
|
|
|
|
|
var message = new Message
|
|
|
|
|
{
|
2018-02-10 16:42:28 +01:00
|
|
|
|
MessageId = Guid.NewGuid().ToString(),
|
2018-02-12 18:31:20 +01:00
|
|
|
|
Body = body,
|
2017-05-24 15:34:55 +02:00
|
|
|
|
Label = eventName,
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-09 12:02:28 +02:00
|
|
|
|
_serviceBusPersisterConnection.TopicClient.SendAsync(message)
|
2017-05-24 15:34:55 +02:00
|
|
|
|
.GetAwaiter()
|
|
|
|
|
.GetResult();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-26 18:05:02 +02:00
|
|
|
|
public void SubscribeDynamic<TH>(string eventName)
|
|
|
|
|
where TH : IDynamicIntegrationEventHandler
|
|
|
|
|
{
|
2019-07-11 15:44:32 +01:00
|
|
|
|
_logger.LogInformation("Subscribing to dynamic event {EventName} with {EventHandler}", eventName, typeof(TH).Name);
|
2019-03-04 19:08:14 +00:00
|
|
|
|
|
2017-06-26 18:05:02 +02:00
|
|
|
|
_subsManager.AddDynamicSubscription<TH>(eventName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Subscribe<T, TH>()
|
2017-05-24 15:34:55 +02:00
|
|
|
|
where T : IntegrationEvent
|
|
|
|
|
where TH : IIntegrationEventHandler<T>
|
|
|
|
|
{
|
2019-07-19 11:49:25 -07:00
|
|
|
|
var eventName = typeof(T).Name.Replace(INTEGRATION_EVENT_SUFFIX, "");
|
2017-08-01 13:45:33 +02:00
|
|
|
|
|
2017-05-24 15:34:55 +02:00
|
|
|
|
var containsKey = _subsManager.HasSubscriptionsForEvent<T>();
|
|
|
|
|
if (!containsKey)
|
2017-05-26 01:35:19 +02:00
|
|
|
|
{
|
2017-05-24 15:34:55 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-03-09 12:02:28 +02:00
|
|
|
|
_serviceBusPersisterConnection.SubscriptionClient.AddRuleAsync(new RuleDescription
|
2017-05-24 15:34:55 +02:00
|
|
|
|
{
|
|
|
|
|
Filter = new CorrelationFilter { Label = eventName },
|
|
|
|
|
Name = eventName
|
|
|
|
|
}).GetAwaiter().GetResult();
|
|
|
|
|
}
|
2018-05-30 22:51:49 +02:00
|
|
|
|
catch (ServiceBusException)
|
2017-05-24 15:34:55 +02:00
|
|
|
|
{
|
2019-02-22 15:05:28 +00:00
|
|
|
|
_logger.LogWarning("The messaging entity {eventName} already exists.", eventName);
|
2017-05-24 15:34:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 15:44:32 +01:00
|
|
|
|
_logger.LogInformation("Subscribing to event {EventName} with {EventHandler}", eventName, typeof(TH).Name);
|
2019-03-04 19:08:14 +00:00
|
|
|
|
|
2017-06-26 18:05:02 +02:00
|
|
|
|
_subsManager.AddSubscription<T, TH>();
|
2017-05-24 15:34:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Unsubscribe<T, TH>()
|
|
|
|
|
where T : IntegrationEvent
|
|
|
|
|
where TH : IIntegrationEventHandler<T>
|
|
|
|
|
{
|
2019-07-19 11:49:25 -07:00
|
|
|
|
var eventName = typeof(T).Name.Replace(INTEGRATION_EVENT_SUFFIX, "");
|
2017-05-24 15:34:55 +02:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-03-09 12:02:28 +02:00
|
|
|
|
_serviceBusPersisterConnection
|
|
|
|
|
.SubscriptionClient
|
|
|
|
|
.RemoveRuleAsync(eventName)
|
|
|
|
|
.GetAwaiter()
|
|
|
|
|
.GetResult();
|
2017-05-24 15:34:55 +02:00
|
|
|
|
}
|
|
|
|
|
catch (MessagingEntityNotFoundException)
|
|
|
|
|
{
|
2019-02-22 15:05:28 +00:00
|
|
|
|
_logger.LogWarning("The messaging entity {eventName} Could not be found.", eventName);
|
2017-05-24 15:34:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 19:08:14 +00:00
|
|
|
|
_logger.LogInformation("Unsubscribing from event {EventName}", eventName);
|
|
|
|
|
|
2017-05-24 15:34:55 +02:00
|
|
|
|
_subsManager.RemoveSubscription<T, TH>();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-26 18:05:02 +02:00
|
|
|
|
public void UnsubscribeDynamic<TH>(string eventName)
|
|
|
|
|
where TH : IDynamicIntegrationEventHandler
|
|
|
|
|
{
|
2019-03-04 19:08:14 +00:00
|
|
|
|
_logger.LogInformation("Unsubscribing from dynamic event {EventName}", eventName);
|
|
|
|
|
|
2017-06-26 18:05:02 +02:00
|
|
|
|
_subsManager.RemoveDynamicSubscription<TH>(eventName);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 15:34:55 +02:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_subsManager.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-26 01:35:19 +02:00
|
|
|
|
private void RegisterSubscriptionClientMessageHandler()
|
2017-05-24 16:26:40 +02:00
|
|
|
|
{
|
2021-03-09 12:02:28 +02:00
|
|
|
|
_serviceBusPersisterConnection.SubscriptionClient.RegisterMessageHandler(
|
2017-05-24 16:26:40 +02:00
|
|
|
|
async (message, token) =>
|
|
|
|
|
{
|
2019-07-19 11:49:25 -07:00
|
|
|
|
var eventName = $"{message.Label}{INTEGRATION_EVENT_SUFFIX}";
|
2017-05-24 16:26:40 +02:00
|
|
|
|
var messageData = Encoding.UTF8.GetString(message.Body);
|
2018-05-30 22:51:49 +02:00
|
|
|
|
|
2017-08-17 13:00:48 +02:00
|
|
|
|
// Complete the message so that it is not received again.
|
2018-05-30 22:51:49 +02:00
|
|
|
|
if (await ProcessEvent(eventName, messageData))
|
2018-04-13 12:03:34 +02:00
|
|
|
|
{
|
2021-03-09 12:02:28 +02:00
|
|
|
|
await _serviceBusPersisterConnection.SubscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
|
2018-04-13 12:03:34 +02:00
|
|
|
|
}
|
2017-05-24 16:26:40 +02:00
|
|
|
|
},
|
2019-03-04 19:08:14 +00:00
|
|
|
|
new MessageHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentCalls = 10, AutoComplete = false });
|
2017-08-17 13:00:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs)
|
|
|
|
|
{
|
2019-03-04 19:08:14 +00:00
|
|
|
|
var ex = exceptionReceivedEventArgs.Exception;
|
2017-08-17 13:00:48 +02:00
|
|
|
|
var context = exceptionReceivedEventArgs.ExceptionReceivedContext;
|
2019-03-04 19:08:14 +00:00
|
|
|
|
|
|
|
|
|
_logger.LogError(ex, "ERROR handling message: {ExceptionMessage} - Context: {@ExceptionContext}", ex.Message, context);
|
|
|
|
|
|
2017-08-17 13:00:48 +02:00
|
|
|
|
return Task.CompletedTask;
|
2017-05-24 16:26:40 +02:00
|
|
|
|
}
|
2017-05-24 15:34:55 +02:00
|
|
|
|
|
2018-04-13 12:03:34 +02:00
|
|
|
|
private async Task<bool> ProcessEvent(string eventName, string message)
|
2017-05-24 15:34:55 +02:00
|
|
|
|
{
|
2018-04-13 12:03:34 +02:00
|
|
|
|
var processed = false;
|
2017-05-24 15:34:55 +02:00
|
|
|
|
if (_subsManager.HasSubscriptionsForEvent(eventName))
|
|
|
|
|
{
|
2017-06-26 18:05:02 +02:00
|
|
|
|
using (var scope = _autofac.BeginLifetimeScope(AUTOFAC_SCOPE_NAME))
|
2017-05-24 15:34:55 +02:00
|
|
|
|
{
|
2017-06-26 18:05:02 +02:00
|
|
|
|
var subscriptions = _subsManager.GetHandlersForEvent(eventName);
|
|
|
|
|
foreach (var subscription in subscriptions)
|
|
|
|
|
{
|
|
|
|
|
if (subscription.IsDynamic)
|
|
|
|
|
{
|
|
|
|
|
var handler = scope.ResolveOptional(subscription.HandlerType) as IDynamicIntegrationEventHandler;
|
2018-10-11 17:16:31 +02:00
|
|
|
|
if (handler == null) continue;
|
2021-05-03 16:36:31 +05:30
|
|
|
|
|
|
|
|
|
using dynamic eventData = JsonDocument.Parse(message);
|
2017-06-26 18:05:02 +02:00
|
|
|
|
await handler.Handle(eventData);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var handler = scope.ResolveOptional(subscription.HandlerType);
|
2018-10-11 17:16:31 +02:00
|
|
|
|
if (handler == null) continue;
|
|
|
|
|
var eventType = _subsManager.GetEventTypeByName(eventName);
|
2021-05-03 16:36:31 +05:30
|
|
|
|
var integrationEvent = JsonSerializer.Deserialize(message, eventType);
|
2017-06-26 18:05:02 +02:00
|
|
|
|
var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType);
|
|
|
|
|
await (Task)concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent });
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-24 15:34:55 +02:00
|
|
|
|
}
|
2018-04-13 12:03:34 +02:00
|
|
|
|
processed = true;
|
2017-05-24 15:34:55 +02:00
|
|
|
|
}
|
2018-05-30 22:57:27 +02:00
|
|
|
|
return processed;
|
2017-05-24 15:34:55 +02:00
|
|
|
|
}
|
2017-05-26 01:35:19 +02:00
|
|
|
|
|
|
|
|
|
private void RemoveDefaultRule()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-03-09 12:02:28 +02:00
|
|
|
|
_serviceBusPersisterConnection
|
|
|
|
|
.SubscriptionClient
|
|
|
|
|
.RemoveRuleAsync(RuleDescription.DefaultRuleName)
|
|
|
|
|
.GetAwaiter()
|
|
|
|
|
.GetResult();
|
2017-05-26 01:35:19 +02:00
|
|
|
|
}
|
|
|
|
|
catch (MessagingEntityNotFoundException)
|
|
|
|
|
{
|
2019-02-22 15:05:28 +00:00
|
|
|
|
_logger.LogWarning("The messaging entity {DefaultRuleName} Could not be found.", RuleDescription.DefaultRuleName);
|
2017-05-26 01:35:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-24 15:34:55 +02:00
|
|
|
|
}
|
2019-07-19 11:49:25 -07:00
|
|
|
|
}
|