@ -72,6 +72,23 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen
SubscribeHandlers ( AbpDistributedEventBusOptions . Handlers ) ;
}
public async Task ProcessEventAsync ( Type eventType , object eventData )
{
var messageId = MessageContext . Current . TransportMessage . GetMessageId ( ) ;
var eventName = EventNameAttribute . GetNameOrDefault ( eventType ) ;
var correlationId = MessageContext . Current . Headers . GetOrDefault ( EventBusConsts . CorrelationIdHeaderName ) ;
if ( await AddToInboxAsync ( messageId , eventName , eventType , eventData , correlationId ) )
{
return ;
}
using ( CorrelationIdProvider . Change ( correlationId ) )
{
await TriggerHandlersDirectAsync ( eventType , eventData ) ;
}
}
public override IDisposable Subscribe ( Type eventType , IEventHandlerFactory factory )
{
var handlerFactories = GetOrCreateHandlerFactories ( eventType ) ;
@ -91,6 +108,21 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen
return new EventHandlerFactoryUnregistrar ( this , eventType , factory ) ;
}
/// <inheritdoc/>
public override IDisposable Subscribe ( string eventName , IEventHandlerFactory handler )
{
var handlerFactories = GetOrCreateAnonymousHandlerFactories ( eventName ) ;
if ( handler . IsInFactories ( handlerFactories ) )
{
return NullDisposable . Instance ;
}
handlerFactories . Add ( handler ) ;
return new AnonymousEventHandlerFactoryUnregistrar ( this , eventName , handler ) ;
}
public override void Unsubscribe < TEvent > ( Func < TEvent , Task > action )
{
Check . NotNull ( action , nameof ( action ) ) ;
@ -145,23 +177,7 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen
Rebus . Unsubscribe ( eventType ) ;
}
public async Task ProcessEventAsync ( Type eventType , object eventData )
{
var messageId = MessageContext . Current . TransportMessage . GetMessageId ( ) ;
var eventName = EventNameAttribute . GetNameOrDefault ( eventType ) ;
var correlationId = MessageContext . Current . Headers . GetOrDefault ( EventBusConsts . CorrelationIdHeaderName ) ;
if ( await AddToInboxAsync ( messageId , eventName , eventType , eventData , correlationId ) )
{
return ;
}
using ( CorrelationIdProvider . Change ( correlationId ) )
{
await TriggerHandlersDirectAsync ( eventType , eventData ) ;
}
}
/// <inheritdoc/>
public override Task PublishAsync ( string eventName , object eventData , bool onUnitOfWorkComplete = true )
{
var eventType = EventTypes . GetOrDefault ( eventName ) ;
@ -174,7 +190,7 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen
if ( AnonymousHandlerFactories . ContainsKey ( eventName ) )
{
return PublishAsync ( typeof ( AnonymousEventData ) , new AnonymousEventData ( eventName , eventData ) , onUnitOfWorkComplete ) ;
return PublishAsync ( typeof ( AnonymousEventData ) , anonymousEventData , onUnitOfWorkComplete ) ;
}
throw new AbpException ( $"Unknown event name: {eventName}" ) ;
@ -190,6 +206,111 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen
await PublishAsync ( eventType , eventData , headersArguments : headers ) ;
}
protected override void AddToUnitOfWork ( IUnitOfWork unitOfWork , UnitOfWorkEventRecord eventRecord )
{
unitOfWork . AddOrReplaceDistributedEvent ( eventRecord ) ;
}
public async override Task PublishFromOutboxAsync (
OutgoingEventInfo outgoingEvent ,
OutboxConfig outboxConfig )
{
var eventType = EventTypes . GetOrDefault ( outgoingEvent . EventName ) ;
object eventData ;
if ( eventType ! = null )
{
eventData = Serializer . Deserialize ( outgoingEvent . EventData , eventType ) ;
}
else if ( AnonymousHandlerFactories . ContainsKey ( outgoingEvent . EventName ) )
{
eventData = Serializer . Deserialize ( outgoingEvent . EventData , typeof ( object ) ) ;
eventType = typeof ( AnonymousEventData ) ;
}
else
{
return ;
}
var headers = new Dictionary < string , string > ( ) ;
if ( outgoingEvent . GetCorrelationId ( ) ! = null )
{
headers . Add ( EventBusConsts . CorrelationIdHeaderName , outgoingEvent . GetCorrelationId ( ) ! ) ;
}
await PublishAsync ( eventType , eventData , eventId : outgoingEvent . Id , headersArguments : headers ) ;
using ( CorrelationIdProvider . Change ( outgoingEvent . GetCorrelationId ( ) ) )
{
await TriggerDistributedEventSentAsync ( new DistributedEventSent ( ) {
Source = DistributedEventSource . Outbox ,
EventName = outgoingEvent . EventName ,
EventData = outgoingEvent . EventData
} ) ;
}
}
public async override Task PublishManyFromOutboxAsync ( IEnumerable < OutgoingEventInfo > outgoingEvents , OutboxConfig outboxConfig )
{
var outgoingEventArray = outgoingEvents . ToArray ( ) ;
using ( var scope = new RebusTransactionScope ( ) )
{
foreach ( var outgoingEvent in outgoingEventArray )
{
await PublishFromOutboxAsync ( outgoingEvent , outboxConfig ) ;
using ( CorrelationIdProvider . Change ( outgoingEvent . GetCorrelationId ( ) ) )
{
await TriggerDistributedEventSentAsync ( new DistributedEventSent ( )
{
Source = DistributedEventSource . Outbox ,
EventName = outgoingEvent . EventName ,
EventData = outgoingEvent . EventData
} ) ;
}
}
await scope . CompleteAsync ( ) ;
}
}
public async override Task ProcessFromInboxAsync (
IncomingEventInfo incomingEvent ,
InboxConfig inboxConfig )
{
var eventType = EventTypes . GetOrDefault ( incomingEvent . EventName ) ;
object eventData ;
if ( eventType ! = null )
{
eventData = Serializer . Deserialize ( incomingEvent . EventData , eventType ) ;
}
else if ( AnonymousHandlerFactories . ContainsKey ( incomingEvent . EventName ) )
{
eventData = new AnonymousEventData ( incomingEvent . EventName , Serializer . Deserialize ( incomingEvent . EventData , typeof ( object ) ) ) ;
eventType = typeof ( AnonymousEventData ) ;
}
else
{
return ;
}
var exceptions = new List < Exception > ( ) ;
using ( CorrelationIdProvider . Change ( incomingEvent . GetCorrelationId ( ) ) )
{
await TriggerHandlersFromInboxAsync ( eventType , eventData , exceptions , inboxConfig ) ;
}
if ( exceptions . Any ( ) )
{
ThrowOriginalExceptions ( eventType , exceptions ) ;
}
}
protected override byte [ ] Serialize ( object eventData )
{
return Serializer . Serialize ( eventData ) ;
}
protected virtual async Task PublishAsync (
Type eventType ,
object eventData ,
@ -211,14 +332,12 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen
await Rebus . Publish ( eventData , headersArguments ) ;
}
protected override void AddToUnitOfWork ( IUnitOfWork unitOfWork , UnitOfWorkEventRecord eventRecord )
{
unitOfWork . AddOrReplaceDistributedEvent ( eventRecord ) ;
}
protected override Task OnAddToOutboxAsync ( string eventName , Type eventType , object eventData )
{
EventTypes . GetOrAdd ( eventName , eventType ) ;
if ( typeof ( AnonymousEventData ) ! = eventType )
{
EventTypes . GetOrAdd ( eventName , eventType ) ;
}
return base . OnAddToOutboxAsync ( eventName , eventType , eventData ) ;
}
@ -238,12 +357,16 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen
protected override IEnumerable < EventTypeWithEventHandlerFactories > GetHandlerFactories ( Type eventType )
{
var handlerFactoryList = new List < EventTypeWithEventHandlerFactories > ( ) ;
var eventNames = EventTypes . Where ( x = > ShouldTriggerEventForHandler ( eventType , x . Value ) ) . Select ( x = > x . Key ) . ToList ( ) ;
foreach ( var handlerFactory in HandlerFactories . Where ( hf = > ShouldTriggerEventForHandler ( eventType , hf . Key ) ) )
{
handlerFactoryList . Add ( new EventTypeWithEventHandlerFactories ( handlerFactory . Key , handlerFactory . Value ) ) ;
}
foreach ( var handlerFactory in HandlerFactories . Where ( hf = > ShouldTriggerEventForHandler ( eventType , hf . Key ) )
)
foreach ( var handlerFactory in AnonymousHandlerFactories . Where ( aehf = > eventNames . Contains ( aehf . Key ) ) )
{
handlerFactoryList . Add (
new EventTypeWithEventHandlerFactories ( handlerFactory . Key , handlerFactory . Value ) ) ;
handlerFactoryList . Add ( new EventTypeWithEventHandlerFactories ( typeof ( AnonymousEventData ) , handlerFactory . Value ) ) ;
}
return handlerFactoryList . ToArray ( ) ;
@ -254,22 +377,30 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen
return EventTypes . GetOrDefault ( eventName ) ;
}
public override IDisposable Subscribe ( string eventName , IEventHandlerFactory handler )
/// <inheritdoc/>
public override void Unsubscribe ( string eventName , IEventHandlerFactory factory )
{
GetOrCreateAnonymousHandlerFactories ( eventName ) . Locking ( factories = >
{
if ( ! handler . IsInFactories ( factories ) )
{
factories . Add ( handler ) ;
}
} ) ;
GetOrCreateAnonymousHandlerFactories ( eventName ) . Locking ( factories = > factories . Remove ( factory ) ) ;
}
return new AnonymousEventHandlerFactoryUnregistrar ( this , eventName , handler ) ;
/// <inheritdoc/>
public override void Unsubscribe ( string eventName , IEventHandler handler )
{
GetOrCreateAnonymousHandlerFactories ( eventName )
. Locking ( factories = >
{
factories . RemoveAll (
factory = >
factory is SingleInstanceHandlerFactory singleFactory & &
singleFactory . HandlerInstance = = handler
) ;
} ) ;
}
public override void Unsubscribe ( string eventName , IEventHandlerFactory factory )
/// <inheritdoc/>
public override void UnsubscribeAll ( string eventName )
{
GetOrCreateAnonymousHandlerFactories ( eventName ) . Locking ( factories = > factories . Remove ( factory ) ) ;
GetOrCreateAnonymousHandlerFactories ( eventName ) . Locking ( factories = > factories . Clear ( ) ) ;
}
protected override IEnumerable < EventTypeWithEventHandlerFactories > GetAnonymousHandlerFactories ( string eventName )
@ -311,95 +442,4 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen
return false ;
}
public async override Task PublishFromOutboxAsync (
OutgoingEventInfo outgoingEvent ,
OutboxConfig outboxConfig )
{
var eventType = EventTypes . GetOrDefault ( outgoingEvent . EventName ) ;
if ( eventType = = null )
{
return ;
}
var eventData = Serializer . Deserialize ( outgoingEvent . EventData , eventType ) ;
var headers = new Dictionary < string , string > ( ) ;
if ( outgoingEvent . GetCorrelationId ( ) ! = null )
{
headers . Add ( EventBusConsts . CorrelationIdHeaderName , outgoingEvent . GetCorrelationId ( ) ! ) ;
}
await PublishAsync ( eventType , eventData , eventId : outgoingEvent . Id , headersArguments : headers ) ;
using ( CorrelationIdProvider . Change ( outgoingEvent . GetCorrelationId ( ) ) )
{
await TriggerDistributedEventSentAsync ( new DistributedEventSent ( ) {
Source = DistributedEventSource . Outbox ,
EventName = outgoingEvent . EventName ,
EventData = outgoingEvent . EventData
} ) ;
}
}
public async override Task PublishManyFromOutboxAsync ( IEnumerable < OutgoingEventInfo > outgoingEvents , OutboxConfig outboxConfig )
{
var outgoingEventArray = outgoingEvents . ToArray ( ) ;
using ( var scope = new RebusTransactionScope ( ) )
{
foreach ( var outgoingEvent in outgoingEventArray )
{
await PublishFromOutboxAsync ( outgoingEvent , outboxConfig ) ;
using ( CorrelationIdProvider . Change ( outgoingEvent . GetCorrelationId ( ) ) )
{
await TriggerDistributedEventSentAsync ( new DistributedEventSent ( )
{
Source = DistributedEventSource . Outbox ,
EventName = outgoingEvent . EventName ,
EventData = outgoingEvent . EventData
} ) ;
}
}
await scope . CompleteAsync ( ) ;
}
}
public async override Task ProcessFromInboxAsync (
IncomingEventInfo incomingEvent ,
InboxConfig inboxConfig )
{
var eventType = EventTypes . GetOrDefault ( incomingEvent . EventName ) ;
object eventData ;
if ( eventType ! = null )
{
eventData = Serializer . Deserialize ( incomingEvent . EventData , eventType ) ;
}
else if ( AnonymousHandlerFactories . ContainsKey ( incomingEvent . EventName ) )
{
eventData = new AnonymousEventData ( incomingEvent . EventName , Serializer . Deserialize ( incomingEvent . EventData , typeof ( object ) ) ) ;
eventType = typeof ( AnonymousEventData ) ;
}
else
{
return ;
}
var exceptions = new List < Exception > ( ) ;
using ( CorrelationIdProvider . Change ( incomingEvent . GetCorrelationId ( ) ) )
{
await TriggerHandlersFromInboxAsync ( eventType , eventData , exceptions , inboxConfig ) ;
}
if ( exceptions . Any ( ) )
{
ThrowOriginalExceptions ( eventType , exceptions ) ;
}
}
protected override byte [ ] Serialize ( object eventData )
{
return Serializer . Serialize ( eventData ) ;
}
}