diff --git a/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpCollectionExtensions.cs b/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpCollectionExtensions.cs
index 4240d895ba..69b4d795e0 100644
--- a/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpCollectionExtensions.cs
+++ b/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpCollectionExtensions.cs
@@ -37,6 +37,33 @@ namespace System.Collections.Generic
return true;
}
+ ///
+ /// Adds items to the collection which are not already in the collection.
+ ///
+ /// The collection
+ /// Item to check and add
+ /// Type of the items in the collection
+ /// Returns the added items.
+ public static IEnumerable AddIfNotContains([NotNull] this ICollection source, IEnumerable items)
+ {
+ Check.NotNull(source, nameof(source));
+
+ var addedItems = new List();
+
+ foreach (var item in items)
+ {
+ if (source.Contains(item))
+ {
+ continue;
+ }
+
+ source.Add(item);
+ addedItems.Add(item);
+ }
+
+ return addedItems;
+ }
+
///
/// Adds an item to the collection if it's not already in the collection based on the given .
///
diff --git a/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs
index 72126873e2..c08f6c6c9f 100644
--- a/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs
+++ b/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs
@@ -17,15 +17,15 @@ namespace Volo.Abp.EventBus.Distributed.RabbitMq
public class RabbitMqDistributedEventBus : EventBusBase, IDistributedEventBus, ITransientDependency
{
protected RabbitMqDistributedEventBusOptions Options { get; }
- protected IChannelPool ChannelPool { get; }
+ protected IConnectionPool ConnectionPool { get; }
protected IRabbitMqSerializer Serializer { get; }
public RabbitMqDistributedEventBus(
IOptions options,
- IChannelPool channelPool,
+ IConnectionPool connectionPool,
IRabbitMqSerializer serializer)
{
- ChannelPool = channelPool;
+ ConnectionPool = connectionPool;
Serializer = serializer;
Options = options.Value;
}
@@ -57,18 +57,18 @@ namespace Volo.Abp.EventBus.Distributed.RabbitMq
public override Task PublishAsync(Type eventType, object eventData)
{
- var eventName = eventType.FullName; //TODO: Get eventname from an attribute if available
+ var eventName = EventNameAttribute.GetName(eventType);
var body = Serializer.Serialize(eventData);
- using (var channelAccessor = ChannelPool.Acquire(Guid.NewGuid().ToString()))
+ using (var channel = ConnectionPool.Get().CreateModel())
{
//TODO: Other properties like durable?
- channelAccessor.Channel.ExchangeDeclare(Options.ExchangeName, "");
+ channel.ExchangeDeclare(Options.ExchangeName, "");
- var properties = channelAccessor.Channel.CreateBasicProperties();
+ var properties = channel.CreateBasicProperties();
properties.DeliveryMode = 2; //persistent
- channelAccessor.Channel.BasicPublish(
+ channel.BasicPublish(
exchange: Options.ExchangeName,
routingKey: eventName,
mandatory: true,
diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs
index 92c15a08d7..7b95c34a3d 100644
--- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs
+++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs
@@ -1,8 +1,11 @@
-using System;
+using Microsoft.Extensions.DependencyInjection;
+using System;
using System.Collections.Generic;
using System.Linq;
-using Microsoft.Extensions.DependencyInjection;
+using Volo.Abp.EventBus.Distributed;
+using Volo.Abp.EventBus.Local;
using Volo.Abp.Modularity;
+using Volo.Abp.Reflection;
namespace Volo.Abp.EventBus
{
@@ -15,22 +18,29 @@ namespace Volo.Abp.EventBus
private static void AddEventHandlers(IServiceCollection services)
{
- var handlers = new List();
+ var localHandlers = new List();
+ var distributedHandlers = new List();
services.OnRegistred(context =>
{
- if (context.ImplementationType.GetInterfaces().Any(i => typeof(IEventHandler).IsAssignableFrom(i)))
+ if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(IEventHandler<>)))
{
- handlers.Add(context.ImplementationType);
+ localHandlers.Add(context.ImplementationType);
+ }
+ else if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(IDistributedEventHandler<>)))
+ {
+ distributedHandlers.Add(context.ImplementationType);
}
});
- services.Configure(options =>
+ services.Configure(options =>
{
- foreach (var handler in handlers)
- {
- options.Handlers.AddIfNotContains(handler);
- }
+ options.Handlers.AddIfNotContains(localHandlers);
+ });
+
+ services.Configure(options =>
+ {
+ options.Handlers.AddIfNotContains(distributedHandlers);
});
}
}
diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalEventBusOptions.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalEventBusOptions.cs
new file mode 100644
index 0000000000..9e48bfa158
--- /dev/null
+++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalEventBusOptions.cs
@@ -0,0 +1,14 @@
+using Volo.Abp.Collections;
+
+namespace Volo.Abp.EventBus.Distributed
+{
+ public class DistributedEventBusOptions
+ {
+ public ITypeList Handlers { get; }
+
+ public DistributedEventBusOptions()
+ {
+ Handlers = new TypeList();
+ }
+ }
+}
\ No newline at end of file
diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/EventBusBase.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs
similarity index 97%
rename from framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/EventBusBase.cs
rename to framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs
index d57f37f6db..c11650c438 100644
--- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/EventBusBase.cs
+++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs
@@ -121,9 +121,7 @@ namespace Volo.Abp.EventBus.Local
{
var handlerType = eventHandlerWrapper.EventHandler.GetType();
- if (ReflectionHelper.IsAssignableToGenericType(
- handlerType,
- typeof(IEventHandler<>)))
+ if (ReflectionHelper.IsAssignableToGenericType(handlerType, typeof(IEventHandler<>)))
{
var method = typeof(IEventHandler<>) //TODO: to a static field
.MakeGenericType(eventType)
@@ -134,9 +132,7 @@ namespace Volo.Abp.EventBus.Local
await (Task)method.Invoke(eventHandlerWrapper.EventHandler, new[] { eventData });
}
- else if (ReflectionHelper.IsAssignableToGenericType(
- handlerType,
- typeof(IDistributedEventHandler<>)))
+ else if (ReflectionHelper.IsAssignableToGenericType(handlerType, typeof(IDistributedEventHandler<>)))
{
var method = typeof(IDistributedEventHandler<>) //TODO: to a static field
.MakeGenericType(eventType)
diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventName.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventName.cs
new file mode 100644
index 0000000000..3d634c76f5
--- /dev/null
+++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventName.cs
@@ -0,0 +1,39 @@
+using System;
+using System.Linq;
+using JetBrains.Annotations;
+
+namespace Volo.Abp.EventBus
+{
+ [AttributeUsage(AttributeTargets.Class)]
+ public class EventNameAttribute : Attribute, IEventNameProvider
+ {
+ public string Name { get; }
+
+ public EventNameAttribute([NotNull] string name)
+ {
+ Name = Check.NotNullOrWhiteSpace(name, nameof(name));
+ }
+
+ public static string GetName()
+ {
+ return GetName(typeof(TEvent));
+ }
+
+ public static string GetName([NotNull] Type eventType)
+ {
+ Check.NotNull(eventType, nameof(eventType));
+
+ return eventType
+ .GetCustomAttributes(true)
+ .OfType()
+ .FirstOrDefault()
+ ?.Name
+ ?? eventType.FullName;
+ }
+ }
+
+ public interface IEventNameProvider
+ {
+ string Name { get; }
+ }
+}
diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs
index 967bdfb81f..c07e31a642 100644
--- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs
+++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs
@@ -6,8 +6,6 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
-using System.Runtime.CompilerServices;
-using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Collections;
using Volo.Abp.DependencyInjection;
@@ -26,14 +24,14 @@ namespace Volo.Abp.EventBus.Local
///
public ILogger Logger { get; set; }
- protected EventBusOptions Options { get; }
+ protected LocalEventBusOptions Options { get; }
protected ConcurrentDictionary> HandlerFactories { get; }
protected IServiceProvider ServiceProvider { get; }
public LocalEventBus(
- IOptions options,
+ IOptions options,
IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusOptions.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBusOptions.cs
similarity index 61%
rename from framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusOptions.cs
rename to framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBusOptions.cs
index 5486813597..24606a119a 100644
--- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusOptions.cs
+++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBusOptions.cs
@@ -1,12 +1,12 @@
using Volo.Abp.Collections;
-namespace Volo.Abp.EventBus
+namespace Volo.Abp.EventBus.Local
{
- public class EventBusOptions
+ public class LocalEventBusOptions
{
public ITypeList Handlers { get; }
- public EventBusOptions()
+ public LocalEventBusOptions()
{
Handlers = new TypeList();
}