|
|
|
@ -22,13 +22,13 @@ namespace Volo.Abp.EventBus.Azure; |
|
|
|
[ExposeServices(typeof(IDistributedEventBus), typeof(AzureDistributedEventBus))] |
|
|
|
public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDependency |
|
|
|
{ |
|
|
|
private readonly AbpAzureEventBusOptions _options; |
|
|
|
private readonly IAzureServiceBusMessageConsumerFactory _messageConsumerFactory; |
|
|
|
private readonly IPublisherPool _publisherPool; |
|
|
|
private readonly IAzureServiceBusSerializer _serializer; |
|
|
|
private readonly ConcurrentDictionary<Type, List<IEventHandlerFactory>> _handlerFactories; |
|
|
|
private readonly ConcurrentDictionary<string, Type> _eventTypes; |
|
|
|
private IAzureServiceBusMessageConsumer _consumer; |
|
|
|
protected AbpAzureEventBusOptions Options { get; } |
|
|
|
protected IAzureServiceBusMessageConsumerFactory MessageConsumerFactory { get; } |
|
|
|
protected IPublisherPool PublisherPool { get; } |
|
|
|
protected IAzureServiceBusSerializer Serializer { get; } |
|
|
|
protected ConcurrentDictionary<Type, List<IEventHandlerFactory>> HandlerFactories { get; } |
|
|
|
protected ConcurrentDictionary<string, Type> EventTypes { get; } |
|
|
|
protected IAzureServiceBusMessageConsumer Consumer { get; private set; } |
|
|
|
|
|
|
|
public AzureDistributedEventBus( |
|
|
|
IServiceScopeFactory serviceScopeFactory, |
|
|
|
@ -52,22 +52,22 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen |
|
|
|
eventHandlerInvoker, |
|
|
|
localEventBus) |
|
|
|
{ |
|
|
|
_options = abpAzureEventBusOptions.Value; |
|
|
|
_serializer = serializer; |
|
|
|
_messageConsumerFactory = messageConsumerFactory; |
|
|
|
_publisherPool = publisherPool; |
|
|
|
_handlerFactories = new ConcurrentDictionary<Type, List<IEventHandlerFactory>>(); |
|
|
|
_eventTypes = new ConcurrentDictionary<string, Type>(); |
|
|
|
Options = abpAzureEventBusOptions.Value; |
|
|
|
Serializer = serializer; |
|
|
|
MessageConsumerFactory = messageConsumerFactory; |
|
|
|
PublisherPool = publisherPool; |
|
|
|
HandlerFactories = new ConcurrentDictionary<Type, List<IEventHandlerFactory>>(); |
|
|
|
EventTypes = new ConcurrentDictionary<string, Type>(); |
|
|
|
} |
|
|
|
|
|
|
|
public void Initialize() |
|
|
|
{ |
|
|
|
_consumer = _messageConsumerFactory.CreateMessageConsumer( |
|
|
|
_options.TopicName, |
|
|
|
_options.SubscriberName, |
|
|
|
_options.ConnectionName); |
|
|
|
Consumer = MessageConsumerFactory.CreateMessageConsumer( |
|
|
|
Options.TopicName, |
|
|
|
Options.SubscriberName, |
|
|
|
Options.ConnectionName); |
|
|
|
|
|
|
|
_consumer.OnMessageReceived(ProcessEventAsync); |
|
|
|
Consumer.OnMessageReceived(ProcessEventAsync); |
|
|
|
SubscribeHandlers(AbpDistributedEventBusOptions.Handlers); |
|
|
|
} |
|
|
|
|
|
|
|
@ -78,13 +78,13 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
var eventType = _eventTypes.GetOrDefault(eventName); |
|
|
|
var eventType = EventTypes.GetOrDefault(eventName); |
|
|
|
if (eventType == null) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
var eventData = _serializer.Deserialize(message.Body.ToArray(), eventType); |
|
|
|
var eventData = Serializer.Deserialize(message.Body.ToArray(), eventType); |
|
|
|
|
|
|
|
if (await AddToInboxAsync(message.MessageId, eventName, eventType, eventData)) |
|
|
|
{ |
|
|
|
@ -110,9 +110,9 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen |
|
|
|
{ |
|
|
|
var outgoingEventArray = outgoingEvents.ToArray(); |
|
|
|
|
|
|
|
var publisher = await _publisherPool.GetAsync( |
|
|
|
_options.TopicName, |
|
|
|
_options.ConnectionName); |
|
|
|
var publisher = await PublisherPool.GetAsync( |
|
|
|
Options.TopicName, |
|
|
|
Options.ConnectionName); |
|
|
|
|
|
|
|
using var messageBatch = await publisher.CreateMessageBatchAsync(); |
|
|
|
|
|
|
|
@ -144,13 +144,13 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen |
|
|
|
|
|
|
|
public async override Task ProcessFromInboxAsync(IncomingEventInfo incomingEvent, InboxConfig inboxConfig) |
|
|
|
{ |
|
|
|
var eventType = _eventTypes.GetOrDefault(incomingEvent.EventName); |
|
|
|
var eventType = EventTypes.GetOrDefault(incomingEvent.EventName); |
|
|
|
if (eventType == null) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
var eventData = _serializer.Deserialize(incomingEvent.EventData, eventType); |
|
|
|
var eventData = Serializer.Deserialize(incomingEvent.EventData, eventType); |
|
|
|
var exceptions = new List<Exception>(); |
|
|
|
await TriggerHandlersFromInboxAsync(eventType, eventData, exceptions, inboxConfig); |
|
|
|
if (exceptions.Any()) |
|
|
|
@ -161,7 +161,7 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen |
|
|
|
|
|
|
|
protected override byte[] Serialize(object eventData) |
|
|
|
{ |
|
|
|
return _serializer.Serialize(eventData); |
|
|
|
return Serializer.Serialize(eventData); |
|
|
|
} |
|
|
|
|
|
|
|
public override IDisposable Subscribe(Type eventType, IEventHandlerFactory factory) |
|
|
|
@ -242,7 +242,7 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen |
|
|
|
|
|
|
|
protected virtual Task PublishAsync(string eventName, object eventData) |
|
|
|
{ |
|
|
|
var body = _serializer.Serialize(eventData); |
|
|
|
var body = Serializer.Serialize(eventData); |
|
|
|
|
|
|
|
return PublishAsync(eventName, body, null); |
|
|
|
} |
|
|
|
@ -262,16 +262,16 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen |
|
|
|
message.MessageId = (eventId ?? GuidGenerator.Create()).ToString("N"); |
|
|
|
} |
|
|
|
|
|
|
|
var publisher = await _publisherPool.GetAsync( |
|
|
|
_options.TopicName, |
|
|
|
_options.ConnectionName); |
|
|
|
var publisher = await PublisherPool.GetAsync( |
|
|
|
Options.TopicName, |
|
|
|
Options.ConnectionName); |
|
|
|
|
|
|
|
await publisher.SendMessageAsync(message); |
|
|
|
} |
|
|
|
|
|
|
|
protected override IEnumerable<EventTypeWithEventHandlerFactories> GetHandlerFactories(Type eventType) |
|
|
|
{ |
|
|
|
return _handlerFactories |
|
|
|
return HandlerFactories |
|
|
|
.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key)) |
|
|
|
.Select(handlerFactory => |
|
|
|
new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value)) |
|
|
|
@ -283,14 +283,20 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen |
|
|
|
return handlerEventType == targetEventType || handlerEventType.IsAssignableFrom(targetEventType); |
|
|
|
} |
|
|
|
|
|
|
|
protected override Task OnAddToOutboxAsync(string eventName, Type eventType, object eventData) |
|
|
|
{ |
|
|
|
EventTypes.GetOrAdd(eventName, eventType); |
|
|
|
return base.OnAddToOutboxAsync(eventName, eventType, eventData); |
|
|
|
} |
|
|
|
|
|
|
|
private List<IEventHandlerFactory> GetOrCreateHandlerFactories(Type eventType) |
|
|
|
{ |
|
|
|
return _handlerFactories.GetOrAdd( |
|
|
|
return HandlerFactories.GetOrAdd( |
|
|
|
eventType, |
|
|
|
type => |
|
|
|
{ |
|
|
|
var eventName = EventNameAttribute.GetNameOrDefault(type); |
|
|
|
_eventTypes[eventName] = type; |
|
|
|
EventTypes.GetOrAdd(eventName, eventType); |
|
|
|
return new List<IEventHandlerFactory>(); |
|
|
|
} |
|
|
|
); |
|
|
|
|