-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRabbitMqClientFactory.cs
More file actions
255 lines (239 loc) · 10.6 KB
/
RabbitMqClientFactory.cs
File metadata and controls
255 lines (239 loc) · 10.6 KB
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
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using RapidField.SolidInstruments.Core;
using RapidField.SolidInstruments.Messaging.TransportPrimitives;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace RapidField.SolidInstruments.Messaging.RabbitMq
{
/// <summary>
/// Represents an appliance that manages RabbitMQ messaging clients.
/// </summary>
public sealed class RabbitMqClientFactory : MessagingClientFactory<IMessagingEntitySendClient, IMessagingEntityReceiveClient, PrimitiveMessage, IMessageTransportConnection>
{
/// <summary>
/// Initializes a new instance of the <see cref="RabbitMqClientFactory" /> class.
/// </summary>
/// <param name="connection">
/// A connection that governs interaction with messaging entities.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="connection" /> is <see langword="null" />.
/// </exception>
public RabbitMqClientFactory(IMessageTransportConnection connection)
: base(connection)
{
Transport = connection.Transport;
}
/// <summary>
/// Creates a new implementation-specific client that facilitates receive operations.
/// </summary>
/// <typeparam name="TMessage">
/// The type of the message that the client handles.
/// </typeparam>
/// <param name="connection">
/// A connection that governs interaction with messaging entities.
/// </param>
/// <param name="entityType">
/// The type of the entity.
/// </param>
/// <param name="entityPath">
/// The unique path for the entity.
/// </param>
/// <param name="subscriptionName">
/// The unique name of the subscription.
/// </param>
/// <returns>
/// A new implementation-specific client that facilitates receive operations.
/// </returns>
protected sealed override IMessagingEntityReceiveClient CreateMessageReceiver<TMessage>(IMessageTransportConnection connection, MessagingEntityType entityType, IMessagingEntityPath entityPath, String subscriptionName) => entityType switch
{
MessagingEntityType.Queue => CreateQueueClient<TMessage>(connection, entityPath),
MessagingEntityType.Topic => CreateSubscriptionClient<TMessage>(connection, entityPath, subscriptionName),
_ => throw new UnsupportedSpecificationException($"The specified entity type, {entityType}, is not supported.")
};
/// <summary>
/// Creates a new implementation-specific client that facilitates send operations.
/// </summary>
/// <typeparam name="TMessage">
/// The type of the message that the client handles.
/// </typeparam>
/// <param name="connection">
/// A connection that governs interaction with messaging entities.
/// </param>
/// <param name="entityType">
/// The type of the entity.
/// </param>
/// <param name="entityPath">
/// The unique path for the entity.
/// </param>
/// <returns>
/// A new implementation-specific client that facilitates send operations.
/// </returns>
protected sealed override IMessagingEntitySendClient CreateMessageSender<TMessage>(IMessageTransportConnection connection, MessagingEntityType entityType, IMessagingEntityPath entityPath) => entityType switch
{
MessagingEntityType.Queue => CreateQueueClient<TMessage>(connection, entityPath),
MessagingEntityType.Topic => CreateTopicClient<TMessage>(connection, entityPath),
_ => throw new UnsupportedSpecificationException($"The specified entity type, {entityType}, is not supported.")
};
/// <summary>
/// Releases all resources consumed by the current <see cref="RabbitMqClientFactory" />.
/// </summary>
/// <param name="disposing">
/// A value indicating whether or not managed resources should be released.
/// </param>
protected sealed override void Dispose(Boolean disposing) => base.Dispose(disposing);
/// <summary>
/// Creates a new <see cref="IMessageQueueClient" /> for the specified message type.
/// </summary>
/// <typeparam name="TMessage">
/// The type of the message that the client handles.
/// </typeparam>
/// <param name="connection">
/// A connection that governs interaction with messaging entities.
/// </param>
/// <param name="queuePath">
/// The unique path for the queue.
/// </param>
/// <returns>
/// A new <see cref="IMessageQueueClient" />.
/// </returns>
/// <exception cref="Exception">
/// An exception was raised while creating the client.
/// </exception>
[DebuggerHidden]
private IMessageQueueClient CreateQueueClient<TMessage>(IMessageTransportConnection connection, IMessagingEntityPath queuePath)
where TMessage : class
{
EnsureQueueExistanceAsync(queuePath).Wait();
return new MessageQueueClient(connection, queuePath);
}
/// <summary>
/// Creates a new <see cref="IMessageSubscriptionClient" /> for the specified message type.
/// </summary>
/// <typeparam name="TMessage">
/// The type of the message that the client handles.
/// </typeparam>
/// <param name="connection">
/// A connection that governs interaction with messaging entities.
/// </param>
/// <param name="topicPath">
/// A unique path for the topic.
/// </param>
/// <param name="subscriptionName">
/// A name for the subscription.
/// </param>
/// <returns>
/// A new <see cref="IMessageSubscriptionClient" />.
/// </returns>
/// <exception cref="Exception">
/// An exception was raised while creating the client.
/// </exception>
[DebuggerHidden]
private IMessageSubscriptionClient CreateSubscriptionClient<TMessage>(IMessageTransportConnection connection, IMessagingEntityPath topicPath, String subscriptionName)
where TMessage : class
{
EnsureSubscriptionExistanceAsync(topicPath, subscriptionName).Wait();
return new MessageSubscriptionClient(connection, topicPath, subscriptionName);
}
/// <summary>
/// Creates a new <see cref="IMessageTopicClient" /> for the specified message type.
/// </summary>
/// <typeparam name="TMessage">
/// The type of the message that the client handles.
/// </typeparam>
/// <param name="connection">
/// A connection that governs interaction with messaging entities.
/// </param>
/// <param name="topicPath">
/// A unique path for the topic.
/// </param>
/// <returns>
/// A new <see cref="IMessageTopicClient" />.
/// </returns>
/// <exception cref="Exception">
/// An exception was raised while creating the client.
/// </exception>
[DebuggerHidden]
private IMessageTopicClient CreateTopicClient<TMessage>(IMessageTransportConnection connection, IMessagingEntityPath topicPath)
where TMessage : class
{
EnsureTopicExistanceAsync(topicPath).Wait();
return new MessageTopicClient(connection, topicPath);
}
/// <summary>
/// Asynchronously creates the specified RabbitMQ queue if it does not exist.
/// </summary>
/// <param name="queuePath">
/// The queue entity path.
/// </param>
/// <returns>
/// A task representing the asynchronous operation.
/// </returns>
[DebuggerHidden]
private Task EnsureQueueExistanceAsync(IMessagingEntityPath queuePath) => Task.Factory.StartNew(() =>
{
lock (MessagingEntityClient.EnsureQueueExistenceSyncRoot)
{
if (Transport.QueueExists(queuePath))
{
return;
}
Transport.CreateQueueAsync(queuePath).Wait();
}
});
/// <summary>
/// Asynchronously creates the specified RabbitMQ subscription if it does not exist.
/// </summary>
/// <param name="topicPath">
/// The topic entity path for the subscription.
/// </param>
/// <param name="subscriptionName">
/// The name of the subscription.
/// </param>
/// <returns>
/// A task representing the asynchronous operation.
/// </returns>
[DebuggerHidden]
private Task EnsureSubscriptionExistanceAsync(IMessagingEntityPath topicPath, String subscriptionName) => Task.Factory.StartNew(() =>
{
EnsureTopicExistanceAsync(topicPath).Wait();
lock (MessagingEntityClient.EnsureSubscriptionExistenceSyncRoot)
{
if (Transport.SubscriptionExists(topicPath, subscriptionName))
{
return;
}
Transport.CreateSubscriptionAsync(topicPath, subscriptionName).Wait();
}
});
/// <summary>
/// Asynchronously creates the specified RabbitMQ topic if it does not exist.
/// </summary>
/// <param name="topicPath">
/// The topic entity path.
/// </param>
/// <returns>
/// A task representing the asynchronous operation.
/// </returns>
[DebuggerHidden]
private Task EnsureTopicExistanceAsync(IMessagingEntityPath topicPath) => Task.Factory.StartNew(() =>
{
lock (MessagingEntityClient.EnsureTopicExistenceSyncRoot)
{
if (Transport.TopicExists(topicPath))
{
return;
}
Transport.CreateTopicAsync(topicPath).Wait();
}
});
/// <summary>
/// Represents the transport for which the current <see cref="RabbitMqClientFactory" /> creates clients.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly IMessageTransport Transport;
}
}