From 0c7d5938e38a5014bbb5d100f7c0872925d79ba8 Mon Sep 17 00:00:00 2001 From: ShimizuShiori <116971263@qq.com> Date: Tue, 21 Apr 2020 14:31:27 +0800 Subject: [PATCH] =?UTF-8?q?3.2.3=20=E6=96=B0=E5=A2=9E=E4=BA=86=E6=B8=85?= =?UTF-8?q?=E9=99=A4=E7=BC=93=E5=AD=98=E7=9A=84=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E5=BD=93=E6=97=A0=E6=B3=95=E4=BB=8E=E7=BC=93=E5=AD=98=E4=B8=AD?= =?UTF-8?q?=E5=BE=97=E5=88=B0=E7=9B=91=E5=90=AC=E5=99=A8=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E4=BC=9A=E6=B8=85=E9=99=A4=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E5=8F=91=E5=B8=83=E3=80=82=E8=8B=A5=E5=86=8D?= =?UTF-8?q?=E6=AC=A1=E5=8F=91=E5=B8=83=E4=BE=9D=E7=84=B6=E6=89=BE=E4=B8=8D?= =?UTF-8?q?=E5=88=B0=E7=9B=91=E5=90=AC=E5=99=A8=E4=BF=A1=E6=81=AF=E5=88=99?= =?UTF-8?q?=E6=8A=9B=E5=87=BA=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/DotNet/Reface.EventBus/DefaultCache.cs | 9 +++--- src/DotNet/Reface.EventBus/DefaultEventBus.cs | 28 +++++++++++++++++-- src/DotNet/Reface.EventBus/ICache.cs | 2 ++ src/DotNet/Reface.EventBus/package.nuspec | 10 ++----- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/DotNet/Reface.EventBus/DefaultCache.cs b/src/DotNet/Reface.EventBus/DefaultCache.cs index 7ca2f3e..8a61a2b 100644 --- a/src/DotNet/Reface.EventBus/DefaultCache.cs +++ b/src/DotNet/Reface.EventBus/DefaultCache.cs @@ -1,9 +1,5 @@ using System; using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Reface.EventBus { @@ -11,6 +7,11 @@ public class DefaultCache : ICache { private static readonly ConcurrentDictionary values = new ConcurrentDictionary(); + public void Clean(string name) + { + values.TryRemove(name, out object value); + } + public T GetOrCreate(string name, Func factory) { return (T)values.GetOrAdd(name, s => factory()); diff --git a/src/DotNet/Reface.EventBus/DefaultEventBus.cs b/src/DotNet/Reface.EventBus/DefaultEventBus.cs index 1091b1e..b0bf45d 100644 --- a/src/DotNet/Reface.EventBus/DefaultEventBus.cs +++ b/src/DotNet/Reface.EventBus/DefaultEventBus.cs @@ -1,6 +1,7 @@ using Reface.EventBus.EventListenerFinders; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Reflection.Emit; @@ -31,12 +32,33 @@ public DefaultEventBus() : this(new ConfigurationEventListenerFinder()) public void Publish(Event @event) { + this.Publish(@event, 0); + } + + public void Publish(Event @event, int time) + { + var allListeners = this.eventListenerFinder.CreateAllEventListeners(); allListeners = this.SortEventListeners(allListeners); Type eventType = @event.GetType(); - Dictionary infoMap = cache.GetOrCreate($"ListenerInfosOf${eventType.FullName}", + string cacheKey = $"ListenerInfosOf${eventType.FullName}"; + Dictionary infoMap = cache.GetOrCreate(cacheKey, () => CreateListenerInfos(eventType, allListeners)); + foreach (var listener in allListeners) + { + Type listenerType = listener.GetType(); + if (!infoMap.ContainsKey(listenerType)) + { + if (time != 0) + throw new KeyNotFoundException($"未发现监听器 [{listenerType.FullName}] 的信息,经清除缓存后,依然无法正常发布。"); + + Debug.WriteLine($"未发现监听器 [{listenerType.FullName}] 的信息,可能与缓存有关,清除缓存将重新发布"); + cache.Clean(cacheKey); + this.Publish(@event, time + 1); + } + } + foreach (var listener in allListeners) { Type listenerType = listener.GetType(); @@ -50,6 +72,8 @@ public void Publish(Event @event) } } + + /// /// Sort all EventListeners b Priority /// @@ -71,7 +95,7 @@ private IEnumerable SortEventListeners(IEnumerable x.L) .ToList(); } - + private Dictionary CreateListenerInfos(Type eventType, IEnumerable allListeners) { Dictionary result = new Dictionary(); diff --git a/src/DotNet/Reface.EventBus/ICache.cs b/src/DotNet/Reface.EventBus/ICache.cs index 42eb667..7fd0fd3 100644 --- a/src/DotNet/Reface.EventBus/ICache.cs +++ b/src/DotNet/Reface.EventBus/ICache.cs @@ -8,5 +8,7 @@ namespace Reface.EventBus public interface ICache { T GetOrCreate(String name, Func factory); + + void Clean(string name); } } diff --git a/src/DotNet/Reface.EventBus/package.nuspec b/src/DotNet/Reface.EventBus/package.nuspec index 435c8d7..cd63fb2 100644 --- a/src/DotNet/Reface.EventBus/package.nuspec +++ b/src/DotNet/Reface.EventBus/package.nuspec @@ -4,11 +4,11 @@ Reface.EventBus - 3.2.0 + 3.2.3 Reface.EventBus - ShimizuShiori - ShimizuShiori + Felix + Felix https://github.com/ShimizuShiori/EventBus/blob/master/LICENSE https://github.com/ShimizuShiori/EventBus EventBus in C# - - + 添加验证,事件源不能为 null - + 当发布消息时,才获取所有事件监听者 - Copyright © HP Inc. 2019