2021-10-26 17:39:15 +08:00
|
|
|
|
using Azure.Messaging.ServiceBus;
|
|
|
|
|
using Azure.Messaging.ServiceBus.Administration;
|
|
|
|
|
using Autofac;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus
|
2017-05-24 15:34:55 +02:00
|
|
|
|
{
|
2021-10-26 17:39:15 +08:00
|
|
|
|
public class EventBusServiceBus : IEventBus, IDisposable
|
2017-05-24 15:34:55 +02:00
|
|
|
|
{
|
|
|
|
|
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;
|
2021-10-26 17:39:15 +08:00
|
|
|
|
private readonly string _topicName = "eshop_event_bus";
|
|
|
|
|
private readonly string _subscriptionName;
|
|
|
|
|
private ServiceBusSender _sender;
|
|
|
|
|
private ServiceBusProcessor _processor;
|
2017-06-26 18:05:02 +02:00
|
|
|
|
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-10-26 17:39:15 +08:00
|
|
|
|
ILogger<EventBusServiceBus> logger, IEventBusSubscriptionsManager subsManager, ILifetimeScope autofac, string subscriptionClientName)
|
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;
|
2021-10-26 17:39:15 +08:00
|
|
|
|
_subscriptionName = subscriptionClientName;
|
|
|
|
|
_sender = _serviceBusPersisterConnection.TopicClient.CreateSender(_topicName);
|
|
|
|
|
ServiceBusProcessorOptions options = new ServiceBusProcessorOptions { MaxConcurrentCalls = 10, AutoCompleteMessages = false };
|
|
|
|
|
_processor = _serviceBusPersisterConnection.TopicClient.CreateProcessor(_topicName, _subscriptionName, options);
|
2017-05-24 16:26:40 +02:00
|
|
|
|
|
2017-05-26 01:35:19 +02:00
|
|
|
|
RemoveDefaultRule();
|
2021-10-26 17:39:15 +08:00
|
|
|
|
RegisterSubscriptionClientMessageHandlerAsync().GetAwaiter().GetResult();
|
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-09-15 10:46:26 +01:00
|
|
|
|
var jsonMessage = JsonSerializer.Serialize(@event, @event.GetType());
|
2017-05-24 15:34:55 +02:00
|
|
|
|
var body = Encoding.UTF8.GetBytes(jsonMessage);
|
|
|
|
|
|
2021-10-26 17:39:15 +08:00
|
|
|
|
var message = new ServiceBusMessage
|
2017-05-24 15:34:55 +02:00
|
|
|
|
{
|
2018-02-10 16:42:28 +01:00
|
|
|
|
MessageId = Guid.NewGuid().ToString(),
|
2021-10-26 17:39:15 +08:00
|
|
|
|
Body = new BinaryData(body),
|
|
|
|
|
Subject = eventName,
|
2017-05-24 15:34:55 +02:00
|
|
|
|
};
|
|
|
|
|
|
2021-10-26 17:39:15 +08:00
|
|
|
|
_sender.SendMessageAsync(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-10-26 17:39:15 +08:00
|
|
|
|
_serviceBusPersisterConnection.AdministrationClient.CreateRuleAsync(_topicName, _subscriptionName, new CreateRuleOptions
|
2017-05-24 15:34:55 +02:00
|
|
|
|
{
|
2021-10-26 17:39:15 +08:00
|
|
|
|
Filter = new CorrelationRuleFilter() { Subject = eventName },
|
2017-05-24 15:34:55 +02:00
|
|
|
|
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
|
2021-10-26 17:39:15 +08:00
|
|
|
|
.AdministrationClient
|
|
|
|
|
.DeleteRuleAsync(_topicName, _subscriptionName, eventName)
|
2021-03-09 12:02:28 +02:00
|
|
|
|
.GetAwaiter()
|
|
|
|
|
.GetResult();
|
2017-05-24 15:34:55 +02:00
|
|
|
|
}
|
2021-10-26 17:39:15 +08:00
|
|
|
|
catch (ServiceBusException ex) when (ex.Reason == ServiceBusFailureReason.MessagingEntityNotFound)
|
2017-05-24 15:34:55 +02:00
|
|
|
|
{
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 17:39:15 +08:00
|
|
|
|
private async Task RegisterSubscriptionClientMessageHandlerAsync()
|
2017-05-24 16:26:40 +02:00
|
|
|
|
{
|
2021-10-26 17:39:15 +08:00
|
|
|
|
_processor.ProcessMessageAsync +=
|
|
|
|
|
async (args) =>
|
2017-05-24 16:26:40 +02:00
|
|
|
|
{
|
2021-10-26 17:39:15 +08:00
|
|
|
|
var eventName = $"{args.Message.Subject}{INTEGRATION_EVENT_SUFFIX}";
|
|
|
|
|
string messageData = args.Message.Body.ToString();
|
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-10-26 17:39:15 +08:00
|
|
|
|
await args.CompleteMessageAsync(args.Message);
|
2018-04-13 12:03:34 +02:00
|
|
|
|
}
|
2021-10-26 17:39:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_processor.ProcessErrorAsync += ErrorHandler;
|
|
|
|
|
await _processor.StartProcessingAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_subsManager.Clear();
|
|
|
|
|
_processor.CloseAsync().GetAwaiter().GetResult();
|
2017-08-17 13:00:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 17:39:15 +08:00
|
|
|
|
private Task ErrorHandler(ProcessErrorEventArgs args)
|
2017-08-17 13:00:48 +02:00
|
|
|
|
{
|
2021-10-26 17:39:15 +08:00
|
|
|
|
var ex = args.Exception;
|
|
|
|
|
var context = args.ErrorSource;
|
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
|
2021-10-26 17:39:15 +08:00
|
|
|
|
.AdministrationClient
|
|
|
|
|
.DeleteRuleAsync(_topicName, _subscriptionName, RuleProperties.DefaultRuleName)
|
2021-03-09 12:02:28 +02:00
|
|
|
|
.GetAwaiter()
|
|
|
|
|
.GetResult();
|
2017-05-26 01:35:19 +02:00
|
|
|
|
}
|
2021-10-26 17:39:15 +08:00
|
|
|
|
catch (ServiceBusException ex) when (ex.Reason == ServiceBusFailureReason.MessagingEntityNotFound)
|
2017-05-26 01:35:19 +02:00
|
|
|
|
{
|
2021-10-26 17:39:15 +08:00
|
|
|
|
_logger.LogWarning("The messaging entity {DefaultRuleName} Could not be found.", RuleProperties.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
|
|
|
|
}
|