-
Notifications
You must be signed in to change notification settings - Fork 1
/
InProcessServiceBus.cs
311 lines (276 loc) · 11.6 KB
/
InProcessServiceBus.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ServiceAnt.Handler;
using ServiceAnt.Handler.Request;
using ServiceAnt.Handler.Subscription.Handler;
using ServiceAnt.Request.Handler;
using ServiceAnt.Subscription;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace ServiceAnt
{
/// <summary>
/// The implement that work in-process
/// </summary>
public class InProcessServiceBus : IServiceBus
{
private ISubscriptionManager _subcriptionManager;
private IRequestHandlerManager _requestHandlerManager;
/// <summary>
/// Use to log message of bus
/// </summary>
public event LogBusMessage OnLogBusMessage;
private static Lazy<InProcessServiceBus> _defaultInstance = new Lazy<InProcessServiceBus>();
/// <summary>
/// Default Instance
/// </summary>
public static InProcessServiceBus Default => _defaultInstance.Value;
/// <summary>
/// Constructor
/// </summary>
public InProcessServiceBus()
{
_subcriptionManager = new InMemorySubscriptionsManager();
_requestHandlerManager = new InMemoryRequestHandlerManager();
}
/// <summary>
/// It used to inject mock object
/// </summary>
/// <param name="subcriptionManager"></param>
/// <param name="requestHandlerManager"></param>
public InProcessServiceBus(ISubscriptionManager subcriptionManager, IRequestHandlerManager requestHandlerManager)
{
_subcriptionManager = subcriptionManager;
_requestHandlerManager = requestHandlerManager;
}
#region Pub/Sub
/// <summary>
/// Add Subscription for event by delegate with dynamic parameter
/// </summary>
/// <param name="eventName">the name of event(the class name of event type)</param>
/// <param name="action">Handler delegate</param>
public void AddDynamicSubscription(string eventName, Func<dynamic, Task> action)
{
_subcriptionManager.AddDynamicSubscription(eventName, action);
}
/// <summary>
/// Add Subscription for event by factory
/// </summary>
/// <param name="eventType">The type of event must inherit TransportTray</param>
/// <param name="factory">The factory of handler</param>
public void AddSubscription(Type eventType, IHandlerFactory factory)
{
_subcriptionManager.AddSubscription(eventType, factory);
}
/// <summary>
/// Add Subscription for event by delegate
/// </summary>
/// <typeparam name="TEvent">The event must inherit TransportTray</typeparam>
/// <param name="action">Handler delegate</param>
public void AddSubscription<TEvent>(Func<TEvent, Task> action) where TEvent : TransportTray
{
_subcriptionManager.AddSubscription<TEvent>(action);
}
/// <summary>
/// Add Subscription for event by factory
/// </summary>
/// <typeparam name="TEvent">The event must inherit TransportTray </typeparam>
/// <param name="factory"></param>s
public void AddSubscription<TEvent>(IHandlerFactory factory) where TEvent : TransportTray
{
_subcriptionManager.AddSubscription<TEvent>(factory);
}
/// <summary>
/// Remove subscription from servicebus
/// </summary>
/// <param name="eventName">the name of event(the class name of event type)</param>
/// <param name="action">Handler delegate</param>
public void RemoveDynamicSubscription(string eventName, Func<dynamic, Task> action)
{
_subcriptionManager.RemoveDynamicSubscription(eventName, action);
}
/// <summary>
/// Remove subscription from servicebus
/// </summary>
/// <typeparam name="TEvent">The event must inherit TransportTray</typeparam>
/// <param name="action">Handler delegate</param>
public void RemoveSubscription<TEvent>(Func<TEvent, Task> action) where TEvent : TransportTray
{
_subcriptionManager.RemoveSubscription<TEvent>(action);
}
/// <summary>
/// Publish a event
/// </summary>
/// <param name="event"></param>
/// <returns></returns>
public Task Publish(TransportTray @event)
{
var asyncTask = ProcessEvent(_subcriptionManager.GetEventName(@event.GetType()), JsonConvert.SerializeObject(@event));
return asyncTask;
}
/// <summary>
/// Publish a event sync
/// </summary>
/// <param name="event"></param>
public void PublishSync(TransportTray @event)
{
var asyncTask = ProcessEvent(_subcriptionManager.GetEventName(@event.GetType()), JsonConvert.SerializeObject(@event));
asyncTask.Wait();
}
private async Task ProcessEvent(string eventName, string message)
{
var handlerFactories = _subcriptionManager.GetHandlerFactoriesForEvent(eventName);
foreach (var aHandlerFactory in handlerFactories)
{
try
{
var aHandler = aHandlerFactory.GetHandler();
if (aHandler is IDynamicEventHandler)
{
dynamic eventData = JObject.Parse(message);
await ((IDynamicEventHandler)aHandler).HandleAsync(eventData);
}
else
{
var subscriptionParamType = aHandlerFactory.GetLocalEventType();
if (subscriptionParamType != null)
{
var integrationEvent = JsonConvert.DeserializeObject(message, subscriptionParamType);
var concreteType = typeof(IEventHandler<>).MakeGenericType(subscriptionParamType);
await (Task)concreteType.GetMethod("HandleAsync").Invoke(aHandler, new object[] { integrationEvent });
}
}
aHandlerFactory.ReleaseHandler(aHandler);
}
catch (Exception ex)
{
LogMessage( LogLevel.ERROR, "There has raised a error when publishing event.", ex);
}
}
}
#endregion
#region Req/Resp
/// <summary>
/// Register handler with handler factory
/// </summary>
/// <param name="eventType"></param>
/// <param name="factory"></param>
public void AddRequestHandler(Type eventType, IHandlerFactory factory)
{
_requestHandlerManager.AddRequestHandler(eventType, factory);
}
/// <summary>
/// Register handler with handler factory
/// </summary>
/// <typeparam name="TEvent"></typeparam>
/// <param name="factory"></param>
public void AddRequestHandler<TEvent>(IHandlerFactory factory)
where TEvent : TransportTray
{
_requestHandlerManager.AddRequestHandler<TEvent>(factory);
}
/// <summary>
/// Register handler with delegate
/// </summary>
/// <typeparam name="TEvent"></typeparam>
/// <param name="action"></param>
public void AddRequestHandler<TEvent>(Func<TEvent, IRequestHandlerContext, Task> action)
where TEvent : TransportTray
{
_requestHandlerManager.AddRequestHandler<TEvent>(action);
}
/// <summary>
/// Register dynamic handler with delegate
/// </summary>
/// <param name="eventName"></param>
/// <param name="action"></param>
public void AddDynamicRequestHandler(string eventName, Func<dynamic, IRequestHandlerContext, Task> action)
{
_requestHandlerManager.AddDynamicRequestHandler(eventName, action);
}
/// <summary>
/// Remove handler by event type
/// </summary>
/// <typeparam name="TEvent"></typeparam>
/// <param name="action"></param>
public void RemoveRequestHandler<TEvent>(Func<TEvent, IRequestHandlerContext, Task> action)
where TEvent : TransportTray
{
_requestHandlerManager.RemoveRequestHandler(action);
}
/// <summary>
/// Remove dynamic handler by eventName
/// </summary>
/// <param name="eventName"></param>
/// <param name="action"></param>
public void RemoveDynamicRequestHandler(string eventName, Func<dynamic, IRequestHandlerContext, Task> action)
{
_requestHandlerManager.RemoveDynamicRequestHandler(eventName, action);
}
/// <summary>
/// Send a request sync
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="event"></param>
/// <returns></returns>
public T Send<T>(TransportTray @event)
{
var asyncTask = ProcessRequest<T>(_requestHandlerManager.GetRequestName(@event.GetType()), JsonConvert.SerializeObject(@event));
asyncTask.ConfigureAwait(false);
return asyncTask.Result;
}
/// <summary>
/// Send a request async
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="event"></param>
/// <returns></returns>
public async Task<T> SendAsync<T>(TransportTray @event)
{
return await ProcessRequest<T>(_requestHandlerManager.GetRequestName(@event.GetType()), JsonConvert.SerializeObject(@event));
}
private async Task<T> ProcessRequest<T>(string eventName, string message)
{
var handlerFactories = _requestHandlerManager.GetHandlerFactoriesForRequest(eventName);
var requestContext = new RequestHandlerContext();
if (handlerFactories.Count == 0)
return default(T);
foreach (var aHandlerFactory in handlerFactories)
{
if (requestContext.IsEnd)
break;
try
{
var aHandler = aHandlerFactory.GetHandler();
if (aHandler is IDynamicRequestHandler)
{
dynamic eventData = JObject.Parse(message);
await ((IDynamicRequestHandler)aHandler).HandleAsync(eventData, requestContext);
}
else
{
var requestParamType = aHandlerFactory.GetLocalEventType();
if (requestParamType != null)
{
var integrationEvent = JsonConvert.DeserializeObject(message, requestParamType);
var concreteType = typeof(IRequestHandler<>).MakeGenericType(requestParamType);
await (Task)concreteType.GetMethod("HandleAsync").Invoke(aHandler, new object[] { integrationEvent, requestContext });
}
}
aHandlerFactory.ReleaseHandler(aHandler);
}
catch (Exception ex)
{
LogMessage(LogLevel.ERROR, "There has raised a error when send request.", ex);
}
}
return (T)requestContext.Response;
}
#endregion
private void LogMessage(LogLevel type, string value, Exception ex)
{
OnLogBusMessage?.Invoke(type, value, ex);
}
}
}