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;
|
|
|
|
|
|
|
|
|
|
public class EventBusServiceBus : IEventBus
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceBusPersisterConnection _serviceBusPersisterConnection;
|
|
|
|
|
private ServiceBusConnectionStringBuilder _serviceBusConnectionStringBuilder;
|
|
|
|
|
private readonly ILogger<EventBusServiceBus> _logger;
|
|
|
|
|
private readonly IEventBusSubscriptionsManager _subsManager;
|
|
|
|
|
private readonly SubscriptionClient _subscriptionClient;
|
|
|
|
|
|
|
|
|
|
public EventBusServiceBus(IServiceBusPersisterConnection serviceBusPersisterConnection,
|
|
|
|
|
ILogger<EventBusServiceBus> logger, IEventBusSubscriptionsManager subsManager, string subscriptionClientName)
|
|
|
|
|
{
|
|
|
|
|
_serviceBusPersisterConnection = serviceBusPersisterConnection;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_subsManager = subsManager ?? new InMemoryEventBusSubscriptionsManager();
|
|
|
|
|
|
|
|
|
|
_subscriptionClient = new SubscriptionClient(serviceBusPersisterConnection.ServiceBusConnectionStringBuilder,
|
|
|
|
|
subscriptionClientName);
|
2017-05-24 16:26:40 +02:00
|
|
|
|
|
|
|
|
|
CreateConsumerChannel();
|
2017-05-24 15:34:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Publish(IntegrationEvent @event)
|
|
|
|
|
{
|
|
|
|
|
var eventName = @event.GetType().Name;
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Subscribe<T, TH>(Func<TH> handler)
|
|
|
|
|
where T : IntegrationEvent
|
|
|
|
|
where TH : IIntegrationEventHandler<T>
|
|
|
|
|
{
|
|
|
|
|
var eventName = typeof(T).Name;
|
|
|
|
|
var containsKey = _subsManager.HasSubscriptionsForEvent<T>();
|
|
|
|
|
if (!containsKey)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_subsManager.AddSubscription<T, TH>(handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Unsubscribe<T, TH>()
|
|
|
|
|
where T : IntegrationEvent
|
|
|
|
|
where TH : IIntegrationEventHandler<T>
|
|
|
|
|
{
|
|
|
|
|
var eventName = typeof(T).Name;
|
|
|
|
|
|
|
|
|
|
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>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_subsManager.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 16:26:40 +02:00
|
|
|
|
private void CreateConsumerChannel()
|
|
|
|
|
{
|
|
|
|
|
_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))
|
|
|
|
|
{
|
|
|
|
|
var eventType = _subsManager.GetEventTypeByName(eventName);
|
|
|
|
|
var integrationEvent = JsonConvert.DeserializeObject(message, eventType);
|
|
|
|
|
var handlers = _subsManager.GetHandlersForEvent(eventName);
|
|
|
|
|
|
|
|
|
|
foreach (var handlerfactory in handlers)
|
|
|
|
|
{
|
|
|
|
|
var handler = handlerfactory.DynamicInvoke();
|
|
|
|
|
var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType);
|
|
|
|
|
await (Task)concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|