1
- #if ! NET45
2
- using TaskEx = System . Threading . Tasks . Task ;
3
- #endif
4
-
5
- using System . Collections . Concurrent ;
1
+ using System . Collections . Concurrent ;
6
2
using NewLife . Collections ;
7
3
using NewLife . Data ;
4
+ #if ! NET45
5
+ using TaskEx = System . Threading . Tasks . Task ;
6
+ #endif
8
7
9
8
namespace NewLife . Messaging ;
10
9
@@ -24,13 +23,14 @@ namespace NewLife.Messaging;
24
23
/// <typeparam name="TEvent"></typeparam>
25
24
public interface IEventBus < TEvent >
26
25
{
27
- /// <summary>发布消息 </summary>
26
+ /// <summary>发布事件 </summary>
28
27
/// <param name="event">事件</param>
29
28
/// <param name="context">上下文</param>
30
- Task < Int32 > PublishAsync ( TEvent @event , IEventContext < TEvent > ? context = null ) ;
29
+ /// <param name="cancellationToken">取消令牌</param>
30
+ Task < Int32 > PublishAsync ( TEvent @event , IEventContext < TEvent > ? context = null , CancellationToken cancellationToken = default ) ;
31
31
32
- /// <summary>订阅消息 </summary>
33
- /// <param name="handler">处理器 </param>
32
+ /// <summary>订阅事件 </summary>
33
+ /// <param name="handler">事件处理器 </param>
34
34
/// <param name="clientId">客户标识。每个客户只能订阅一次,重复订阅将会挤掉前一次订阅</param>
35
35
Boolean Subscribe ( IEventHandler < TEvent > handler , String clientId = "" ) ;
36
36
@@ -44,21 +44,30 @@ public interface IEventBus<TEvent>
44
44
public interface IEventHandler < TEvent >
45
45
{
46
46
/// <summary>处理事件</summary>
47
- /// <param name="event"></param>
48
- /// <param name="context"></param>
47
+ /// <param name="event">事件</param>
48
+ /// <param name="context">上下文</param>
49
+ /// <param name="cancellationToken">取消令牌</param>
49
50
/// <returns></returns>
50
- Task HandleAsync ( TEvent @event , IEventContext < TEvent > ? context ) ;
51
+ Task HandleAsync ( TEvent @event , IEventContext < TEvent > ? context , CancellationToken cancellationToken ) ;
51
52
}
52
53
53
54
/// <summary>事件总线</summary>
54
55
public class EventBus < TEvent > : DisposeBase , IEventBus < TEvent >
55
56
{
56
- private ConcurrentDictionary < String , IEventHandler < TEvent > > _handlers = [ ] ;
57
+ private readonly ConcurrentDictionary < String , IEventHandler < TEvent > > _handlers = [ ] ;
57
58
58
- /// <summary>发布消息 </summary>
59
+ /// <summary>发布事件 </summary>
59
60
/// <param name="event">事件</param>
60
61
/// <param name="context">上下文</param>
61
- public virtual async Task < Int32 > PublishAsync ( TEvent @event , IEventContext < TEvent > ? context = null )
62
+ /// <param name="cancellationToken">取消令牌</param>
63
+ public virtual Task < Int32 > PublishAsync ( TEvent @event , IEventContext < TEvent > ? context = null , CancellationToken cancellationToken = default ) => DispatchAsync ( @event , context , cancellationToken ) ;
64
+
65
+ /// <summary>分发事件给各个处理器。进程内分发</summary>
66
+ /// <param name="event"></param>
67
+ /// <param name="context"></param>
68
+ /// <param name="cancellationToken"></param>
69
+ /// <returns></returns>
70
+ protected virtual async Task < Int32 > DispatchAsync ( TEvent @event , IEventContext < TEvent > ? context , CancellationToken cancellationToken )
62
71
{
63
72
var rs = 0 ;
64
73
@@ -67,7 +76,7 @@ public virtual async Task<Int32> PublishAsync(TEvent @event, IEventContext<TEven
67
76
foreach ( var item in _handlers )
68
77
{
69
78
var handler = item . Value ;
70
- await handler . HandleAsync ( @event , context ) . ConfigureAwait ( false ) ;
79
+ await handler . HandleAsync ( @event , context , cancellationToken ) . ConfigureAwait ( false ) ;
71
80
rs ++ ;
72
81
}
73
82
@@ -94,31 +103,31 @@ public static class EventBusExtensions
94
103
{
95
104
/// <summary>订阅事件</summary>
96
105
/// <typeparam name="TEvent"></typeparam>
97
- /// <param name="bus"></param>
98
- /// <param name="action"></param>
106
+ /// <param name="bus">事件总线 </param>
107
+ /// <param name="action">事件处理方法 </param>
99
108
/// <param name="clientId">客户标识。每个客户只能订阅一次,重复订阅将会挤掉前一次订阅</param>
100
109
public static void Subscribe < TEvent > ( this IEventBus < TEvent > bus , Action < TEvent > action , String clientId = "" ) => bus . Subscribe ( new DelegateEventHandler < TEvent > ( action ) , clientId ) ;
101
110
102
111
/// <summary>订阅事件</summary>
103
112
/// <typeparam name="TEvent"></typeparam>
104
- /// <param name="bus"></param>
105
- /// <param name="action"></param>
113
+ /// <param name="bus">事件总线 </param>
114
+ /// <param name="action">事件处理方法 </param>
106
115
/// <param name="clientId">客户标识。每个客户只能订阅一次,重复订阅将会挤掉前一次订阅</param>
107
116
public static void Subscribe < TEvent > ( this IEventBus < TEvent > bus , Action < TEvent , IEventContext < TEvent > > action , String clientId = "" ) => bus . Subscribe ( new DelegateEventHandler < TEvent > ( action ) , clientId ) ;
108
117
109
118
/// <summary>订阅事件</summary>
110
119
/// <typeparam name="TEvent"></typeparam>
111
- /// <param name="bus"></param>
112
- /// <param name="action"></param>
120
+ /// <param name="bus">事件总线 </param>
121
+ /// <param name="action">事件处理方法 </param>
113
122
/// <param name="clientId">客户标识。每个客户只能订阅一次,重复订阅将会挤掉前一次订阅</param>
114
123
public static void Subscribe < TEvent > ( this IEventBus < TEvent > bus , Func < TEvent , Task > action , String clientId = "" ) => bus . Subscribe ( new DelegateEventHandler < TEvent > ( action ) , clientId ) ;
115
124
116
125
/// <summary>订阅事件</summary>
117
126
/// <typeparam name="TEvent"></typeparam>
118
- /// <param name="bus"></param>
119
- /// <param name="action"></param>
127
+ /// <param name="bus">事件总线 </param>
128
+ /// <param name="action">事件处理方法 </param>
120
129
/// <param name="clientId">客户标识。每个客户只能订阅一次,重复订阅将会挤掉前一次订阅</param>
121
- public static void Subscribe < TEvent > ( this IEventBus < TEvent > bus , Func < TEvent , IEventContext < TEvent > , Task > action , String clientId = "" ) => bus . Subscribe ( new DelegateEventHandler < TEvent > ( action ) , clientId ) ;
130
+ public static void Subscribe < TEvent > ( this IEventBus < TEvent > bus , Func < TEvent , IEventContext < TEvent > , CancellationToken , Task > action , String clientId = "" ) => bus . Subscribe ( new DelegateEventHandler < TEvent > ( action ) , clientId ) ;
122
131
}
123
132
124
133
/// <summary>事件上下文接口</summary>
@@ -151,14 +160,15 @@ public class EventContext<TEvent>(IEventBus<TEvent> bus) : IEventContext<TEvent>
151
160
public class DelegateEventHandler < TEvent > ( Delegate method ) : IEventHandler < TEvent >
152
161
{
153
162
/// <summary>处理事件</summary>
154
- /// <param name="event"></param>
155
- /// <param name="context"></param>
163
+ /// <param name="event">事件</param>
164
+ /// <param name="context">上下文</param>
165
+ /// <param name="cancellationToken">取消令牌</param>
156
166
/// <returns></returns>
157
167
/// <exception cref="NotSupportedException"></exception>
158
- public Task HandleAsync ( TEvent @event , IEventContext < TEvent > ? context )
168
+ public Task HandleAsync ( TEvent @event , IEventContext < TEvent > ? context , CancellationToken cancellationToken = default )
159
169
{
160
170
if ( method is Func < TEvent , Task > func ) return func ( @event ) ;
161
- if ( method is Func < TEvent , IEventContext < TEvent > ? , Task > func2 ) return func2 ( @event , context ) ;
171
+ if ( method is Func < TEvent , IEventContext < TEvent > ? , CancellationToken , Task > func2 ) return func2 ( @event , context , cancellationToken ) ;
162
172
163
173
if ( method is Action < TEvent > act )
164
174
act ( @event ) ;
0 commit comments