-
Notifications
You must be signed in to change notification settings - Fork 1
/
InProcessServiceBus.cs
216 lines (182 loc) · 8.03 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
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
{
public class InProcessServiceBus : IServiceBus
{
private ISubscriptionManager _subcriptionManager;
private IRequestHandlerManager _requestHandlerManager;
public event Action<string, string, Exception> OnLogBusMessage;
private static Lazy<InProcessServiceBus> _defaultInstance = new Lazy<InProcessServiceBus>();
public static InProcessServiceBus Default => _defaultInstance.Value;
public InProcessServiceBus()
{
_subcriptionManager = new InMemorySubscriptionsManager();
_requestHandlerManager = new InMemoryRequestHandlerManager();
}
public InProcessServiceBus(ISubscriptionManager subcriptionManager, IRequestHandlerManager requestHandlerManager)
{
_subcriptionManager = subcriptionManager;
_requestHandlerManager = requestHandlerManager;
}
#region Pub/Sub
public void AddDynamicSubscription(string eventName, Func<dynamic, Task> action)
{
_subcriptionManager.AddDynamicSubscription(eventName, action);
}
public void AddSubscription(Type eventType, IHandlerFactory factory)
{
_subcriptionManager.AddSubscription(eventType, factory);
}
public void AddSubscription<TEvent>(Func<TEvent, Task> action) where TEvent : TransportTray
{
_subcriptionManager.AddSubscription<TEvent>(action);
}
public void AddSubscription<TEvent>(IHandlerFactory factory) where TEvent : TransportTray
{
_subcriptionManager.AddSubscription<TEvent>(factory);
}
public void RemoveDynamicSubscription(string eventName, Func<dynamic, Task> action)
{
_subcriptionManager.RemoveDynamicSubscription(eventName, action);
}
public void RemoveSubscription<TEvent>(Func<TEvent, Task> action) where TEvent : TransportTray
{
_subcriptionManager.RemoveSubscription<TEvent>(action);
}
public Task Publish(TransportTray @event)
{
var asyncTask = ProcessEvent(_subcriptionManager.GetEventName(@event.GetType()), JsonConvert.SerializeObject(@event));
return asyncTask;
}
/// <summary>
/// It's for Unit test
/// </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( "error", "There has raised a error when publishing event.", ex);
}
}
}
#endregion
#region Req/Resp
public void AddRequestHandler(Type eventType, IHandlerFactory factory)
{
_requestHandlerManager.AddRequestHandler(eventType, factory);
}
public void AddRequestHandler<TEvent>(IHandlerFactory factory)
where TEvent : TransportTray
{
_requestHandlerManager.AddRequestHandler<TEvent>(factory);
}
public void AddRequestHandler<TEvent>(Func<TEvent, IRequestHandlerContext, Task> action)
where TEvent : TransportTray
{
_requestHandlerManager.AddRequestHandler<TEvent>(action);
}
public void AddDynamicRequestHandler(string eventName, Func<dynamic, IRequestHandlerContext, Task> action)
{
_requestHandlerManager.AddDynamicRequestHandler(eventName, action);
}
public void RemoveRequestHandler<TEvent>(Func<TEvent, IRequestHandlerContext, Task> action)
where TEvent : TransportTray
{
_requestHandlerManager.RemoveRequestHandler(action);
}
public void RemoveDynamicRequestHandler(string eventName, Func<dynamic, IRequestHandlerContext, Task> action)
{
_requestHandlerManager.RemoveDynamicRequestHandler(eventName, action);
}
public T Send<T>(TransportTray @event)
{
var asyncTask = ProcessRequest<T>(_requestHandlerManager.GetRequestName(@event.GetType()), JsonConvert.SerializeObject(@event));
asyncTask.ConfigureAwait(false);
return asyncTask.Result;
}
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("error", "There has raised a error when send request.", ex);
}
}
return (T)requestContext.Response;
}
#endregion
private void LogMessage(string type, string value, Exception ex)
{
OnLogBusMessage?.Invoke(type, value, ex);
}
}
}