2017-05-24 15:34:55 +02:00
|
|
|
|
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Azure.ServiceBus;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using Microsoft.Azure.ServiceBus.Filters;
|
2017-06-26 18:05:02 +02:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2017-05-24 15:34:55 +02:00
|
|
|
|
|
|
|
|
|
public class EventBusServiceBus : IEventBus
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceBusPersisterConnection _serviceBusPersisterConnection;
|
|
|
|
|
private readonly ILogger<EventBusServiceBus> _logger;
|
|
|
|
|
private readonly IEventBusSubscriptionsManager _subsManager;
|
|
|
|
|
private readonly SubscriptionClient _subscriptionClient;
|
2017-06-26 18:05:02 +02:00
|
|
|
|
private readonly ILifetimeScope _autofac;
|
|
|
|
|
private readonly string AUTOFAC_SCOPE_NAME = "eshop_event_bus";
|
2017-08-01 13:45:33 +02:00
|
|
|
|
private const string INTEGRATION_EVENT_SUFIX = "IntegrationEvent";
|
2017-06-26 18:05:02 +02:00
|
|
|
|
|
2017-05-24 15:34:55 +02:00
|
|
|
|
public EventBusServiceBus(IServiceBusPersisterConnection serviceBusPersisterConnection,
|
2017-06-26 18:05:02 +02:00
|
|
|
|
ILogger<EventBusServiceBus> logger, IEventBusSubscriptionsManager subsManager, string subscriptionClientName,
|
|
|
|
|
ILifetimeScope autofac)
|
2017-05-24 15:34:55 +02:00
|
|
|
|
{
|
|
|
|
|
_serviceBusPersisterConnection = serviceBusPersisterConnection;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_subsManager = subsManager ?? new InMemoryEventBusSubscriptionsManager();
|
|
|
|
|
|
|
|
|
|
_subscriptionClient = new SubscriptionClient(serviceBusPersisterConnection.ServiceBusConnectionStringBuilder,
|
|
|
|
|
subscriptionClientName);
|
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)
|
|
|
|
|
{
|
2017-08-01 13:45:33 +02:00
|
|
|
|
var eventName = @event.GetType().Name.Replace(INTEGRATION_EVENT_SUFIX, "");
|
2017-05-24 15:34:55 +02:00
|
|
|
|
var jsonMessage = JsonConvert.SerializeObject(@event);
|
|
|
|
|
var body = Encoding.UTF8.GetBytes(jsonMessage);
|
|
|
|
|
|
|
|
|
|
var message = new Message
|
|
|
|
|
{
|
|
|
|
|
MessageId = new Guid().ToString(),
|
|
|
|
|
Body = Encoding.UTF8.GetBytes(jsonMessage),
|
|
|
|
|
Label = eventName,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var topicClient = _serviceBusPersisterConnection.CreateModel();
|
|
|
|
|
|
|
|
|
|
topicClient.SendAsync(message)
|
|
|
|
|
.GetAwaiter()
|
|
|
|
|
.GetResult();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-26 18:05:02 +02:00
|
|
|
|
public void SubscribeDynamic<TH>(string eventName)
|
|
|
|
|
where TH : IDynamicIntegrationEventHandler
|
|
|
|
|
{
|
|
|
|
|
_subsManager.AddDynamicSubscription<TH>(eventName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Subscribe<T, TH>()
|
2017-05-24 15:34:55 +02:00
|
|
|
|
where T : IntegrationEvent
|
|
|
|
|
where TH : IIntegrationEventHandler<T>
|
|
|
|
|
{
|
2017-08-01 13:45:33 +02:00
|
|
|
|
var eventName = typeof(T).Name.Replace(INTEGRATION_EVENT_SUFIX, "");
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
_subscriptionClient.AddRuleAsync(new RuleDescription
|
|
|
|
|
{
|
|
|
|
|
Filter = new CorrelationFilter { Label = eventName },
|
|
|
|
|
Name = eventName
|
|
|
|
|
}).GetAwaiter().GetResult();
|
|
|
|
|
}
|
|
|
|
|
catch(ServiceBusException)
|
|
|
|
|
{
|
2017-05-24 16:26:40 +02:00
|
|
|
|
_logger.LogInformation($"The messaging entity {eventName} already exists.");
|
2017-05-24 15:34:55 +02: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>
|
|
|
|
|
{
|
2017-08-01 13:45:33 +02:00
|
|
|
|
var eventName = typeof(T).Name.Replace(INTEGRATION_EVENT_SUFIX, "");
|
2017-05-24 15:34:55 +02:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_subscriptionClient
|
|
|
|
|
.RemoveRuleAsync(eventName)
|
|
|
|
|
.GetAwaiter()
|
|
|
|
|
.GetResult();
|
|
|
|
|
}
|
|
|
|
|
catch (MessagingEntityNotFoundException)
|
|
|
|
|
{
|
2017-05-24 16:26:40 +02:00
|
|
|
|
_logger.LogInformation($"The messaging entity {eventName} Could not be found.");
|
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
|
|
|
|
|
{
|
|
|
|
|
_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
|
|
|
|
{
|
|
|
|
|
_subscriptionClient.RegisterMessageHandler(
|
|
|
|
|
async (message, token) =>
|
|
|
|
|
{
|
|
|
|
|
var eventName = message.Label;
|
|
|
|
|
var messageData = Encoding.UTF8.GetString(message.Body);
|
|
|
|
|
await ProcessEvent(eventName, messageData);
|
|
|
|
|
},
|
|
|
|
|
new MessageHandlerOptions() { MaxConcurrentCalls = 10, AutoComplete = true });
|
|
|
|
|
}
|
2017-05-24 15:34:55 +02:00
|
|
|
|
|
|
|
|
|
private async Task ProcessEvent(string eventName, string message)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
dynamic eventData = JObject.Parse(message);
|
|
|
|
|
await handler.Handle(eventData);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var eventType = _subsManager.GetEventTypeByName(eventName);
|
|
|
|
|
var integrationEvent = JsonConvert.DeserializeObject(message, eventType);
|
|
|
|
|
var handler = scope.ResolveOptional(subscription.HandlerType);
|
|
|
|
|
var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType);
|
|
|
|
|
await (Task)concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent });
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-24 15:34:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-26 01:35:19 +02:00
|
|
|
|
|
|
|
|
|
private void RemoveDefaultRule()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_subscriptionClient
|
|
|
|
|
.RemoveRuleAsync(SubscriptionClient.DefaultRule)
|
|
|
|
|
.GetAwaiter()
|
|
|
|
|
.GetResult();
|
|
|
|
|
}
|
|
|
|
|
catch (MessagingEntityNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation($"The messaging entity {SubscriptionClient.DefaultRule} Could not be found.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-24 15:34:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|