| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Diagnostics; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Threading.Tasks; |
| | 11 | | using Amqp; |
| | 12 | | using Azure.Amqp; |
| | 13 | | using Core; |
| | 14 | | using Primitives; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// A session client can be used to accept session objects which can be used to interact with all messages with the |
| | 18 | | /// </summary> |
| | 19 | | /// <remarks> |
| | 20 | | /// You can accept any session or a given session (identified by <see cref="IMessageSession.SessionId"/> using a ses |
| | 21 | | /// Once you accept a session, you can use it as a <see cref="MessageReceiver"/> which receives only messages having |
| | 22 | | /// See <see cref="IMessageSession"/> for usage of session object. |
| | 23 | | /// This uses AMQP protocol to communicate with the service. |
| | 24 | | /// </remarks> |
| | 25 | | /// <example> |
| | 26 | | /// To create a new SessionClient |
| | 27 | | /// <code> |
| | 28 | | /// ISessionClient sessionClient = new SessionClient( |
| | 29 | | /// namespaceConnectionString, |
| | 30 | | /// queueName, |
| | 31 | | /// ReceiveMode.PeekLock); |
| | 32 | | /// </code> |
| | 33 | | /// |
| | 34 | | /// To receive a session object for a given sessionId |
| | 35 | | /// <code> |
| | 36 | | /// IMessageSession session = await sessionClient.AcceptMessageSessionAsync(sessionId); |
| | 37 | | /// </code> |
| | 38 | | /// |
| | 39 | | /// To receive any session |
| | 40 | | /// <code> |
| | 41 | | /// IMessageSession session = await sessionClient.AcceptMessageSessionAsync(); |
| | 42 | | /// </code> |
| | 43 | | /// </example> |
| | 44 | | /// <seealso cref="IMessageSession"/> |
| | 45 | | public sealed class SessionClient : ClientEntity, ISessionClient |
| | 46 | | { |
| | 47 | | const int DefaultPrefetchCount = 0; |
| | 48 | | readonly ServiceBusDiagnosticSource diagnosticSource; |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Creates a new SessionClient from a <see cref="ServiceBusConnectionStringBuilder"/> |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="connectionStringBuilder">The <see cref="ServiceBusConnectionStringBuilder"/> having entity leve |
| | 54 | | /// <param name="receiveMode">The <see cref="ReceiveMode"/> used to specify how messages are received. Defaults |
| | 55 | | /// <param name="retryPolicy">The <see cref="RetryPolicy"/> that will be used when communicating with ServiceBus |
| | 56 | | /// <param name="prefetchCount">The <see cref="PrefetchCount"/> that specifies the upper limit of messages the s |
| | 57 | | /// will actively receive regardless of whether a receive operation is pending. Defaults to 0.</param> |
| | 58 | | /// <remarks>Creates a new connection to the entity, which is used for all the sessions objects accepted using t |
| | 59 | | public SessionClient( |
| | 60 | | ServiceBusConnectionStringBuilder connectionStringBuilder, |
| | 61 | | ReceiveMode receiveMode = ReceiveMode.PeekLock, |
| | 62 | | RetryPolicy retryPolicy = null, |
| | 63 | | int prefetchCount = DefaultPrefetchCount) |
| 0 | 64 | | : this(connectionStringBuilder?.GetNamespaceConnectionString(), connectionStringBuilder?.EntityPath, receive |
| | 65 | | { |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Creates a new SessionClient from a specified connection string and entity path. |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="connectionString">Namespace connection string used to communicate with Service Bus. Must not co |
| | 72 | | /// <param name="entityPath">The path of the entity for this receiver. For Queues this will be the name, but for |
| | 73 | | /// <param name="receiveMode">The <see cref="ReceiveMode"/> used to specify how messages are received. Defaults |
| | 74 | | /// <param name="retryPolicy">The <see cref="RetryPolicy"/> that will be used when communicating with ServiceBus |
| | 75 | | /// <param name="prefetchCount">The <see cref="PrefetchCount"/> that specifies the upper limit of messages the s |
| | 76 | | /// will actively receive regardless of whether a receive operation is pending. Defaults to 0.</param> |
| | 77 | | /// <remarks>Creates a new connection to the entity, which is used for all the sessions objects accepted using t |
| | 78 | | public SessionClient( |
| | 79 | | string connectionString, |
| | 80 | | string entityPath, |
| | 81 | | ReceiveMode receiveMode = ReceiveMode.PeekLock, |
| | 82 | | RetryPolicy retryPolicy = null, |
| | 83 | | int prefetchCount = DefaultPrefetchCount) |
| 0 | 84 | | : this(nameof(SessionClient), |
| 0 | 85 | | entityPath, |
| 0 | 86 | | null, |
| 0 | 87 | | receiveMode, |
| 0 | 88 | | prefetchCount, |
| 0 | 89 | | new ServiceBusConnection(connectionString), |
| 0 | 90 | | null, |
| 0 | 91 | | retryPolicy, |
| 0 | 92 | | null) |
| | 93 | | { |
| 0 | 94 | | if (string.IsNullOrWhiteSpace(connectionString)) |
| | 95 | | { |
| 0 | 96 | | throw Fx.Exception.ArgumentNullOrWhiteSpace(connectionString); |
| | 97 | | } |
| | 98 | |
|
| 0 | 99 | | this.OwnsConnection = true; |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | /// <summary> |
| | 103 | | /// Creates a new SessionClient from a specified endpoint, entity path, and token provider. |
| | 104 | | /// </summary> |
| | 105 | | /// <param name="endpoint">Fully qualified domain name for Service Bus. Most likely, {yournamespace}.servicebus. |
| | 106 | | /// <param name="entityPath">Queue path.</param> |
| | 107 | | /// <param name="tokenProvider">Token provider which will generate security tokens for authorization.</param> |
| | 108 | | /// <param name="transportType">Transport type.</param> |
| | 109 | | /// <param name="receiveMode">Mode of receive of messages. Defaults to <see cref="ReceiveMode"/>.PeekLock.</para |
| | 110 | | /// <param name="retryPolicy">Retry policy for queue operations. Defaults to <see cref="RetryPolicy.Default"/></ |
| | 111 | | /// <param name="prefetchCount">The <see cref="PrefetchCount"/> that specifies the upper limit of messages this |
| | 112 | | /// will actively receive regardless of whether a receive operation is pending. Defaults to 0.</param> |
| | 113 | | /// <remarks>Creates a new connection to the entity, which is opened during the first operation.</remarks> |
| | 114 | | public SessionClient( |
| | 115 | | string endpoint, |
| | 116 | | string entityPath, |
| | 117 | | ITokenProvider tokenProvider, |
| | 118 | | TransportType transportType = TransportType.Amqp, |
| | 119 | | ReceiveMode receiveMode = ReceiveMode.PeekLock, |
| | 120 | | RetryPolicy retryPolicy = null, |
| | 121 | | int prefetchCount = DefaultPrefetchCount) |
| 0 | 122 | | : this(nameof(SessionClient), |
| 0 | 123 | | entityPath, |
| 0 | 124 | | null, |
| 0 | 125 | | receiveMode, |
| 0 | 126 | | prefetchCount, |
| 0 | 127 | | new ServiceBusConnection(endpoint, transportType, retryPolicy) {TokenProvider = tokenProvider}, |
| 0 | 128 | | null, |
| 0 | 129 | | retryPolicy, |
| 0 | 130 | | null) |
| | 131 | | { |
| 0 | 132 | | this.OwnsConnection = true; |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | /// <summary> |
| | 136 | | /// Creates a new SessionClient on a given <see cref="ServiceBusConnection"/> |
| | 137 | | /// </summary> |
| | 138 | | /// <param name="serviceBusConnection">Connection object to the service bus namespace.</param> |
| | 139 | | /// <param name="entityPath">The path of the entity for this receiver. For Queues this will be the name, but for |
| | 140 | | /// <param name="receiveMode">The <see cref="ReceiveMode"/> used to specify how messages are received. Defaults |
| | 141 | | /// <param name="retryPolicy">The <see cref="RetryPolicy"/> that will be used when communicating with ServiceBus |
| | 142 | | /// <param name="prefetchCount">The <see cref="PrefetchCount"/> that specifies the upper limit of messages the s |
| | 143 | | /// will actively receive regardless of whether a receive operation is pending. Defaults to 0.</param> |
| | 144 | | public SessionClient( |
| | 145 | | ServiceBusConnection serviceBusConnection, |
| | 146 | | string entityPath, |
| | 147 | | ReceiveMode receiveMode, |
| | 148 | | RetryPolicy retryPolicy = null, |
| | 149 | | int prefetchCount = DefaultPrefetchCount) |
| 0 | 150 | | : this(nameof(SessionClient), |
| 0 | 151 | | entityPath, |
| 0 | 152 | | null, |
| 0 | 153 | | receiveMode, |
| 0 | 154 | | prefetchCount, |
| 0 | 155 | | serviceBusConnection, |
| 0 | 156 | | null, |
| 0 | 157 | | retryPolicy, |
| 0 | 158 | | null) |
| | 159 | | { |
| 0 | 160 | | this.OwnsConnection = false; |
| 0 | 161 | | } |
| | 162 | |
|
| | 163 | | internal SessionClient( |
| | 164 | | string clientTypeName, |
| | 165 | | string entityPath, |
| | 166 | | MessagingEntityType? entityType, |
| | 167 | | ReceiveMode receiveMode, |
| | 168 | | int prefetchCount, |
| | 169 | | ServiceBusConnection serviceBusConnection, |
| | 170 | | ICbsTokenProvider cbsTokenProvider, |
| | 171 | | RetryPolicy retryPolicy, |
| | 172 | | IList<ServiceBusPlugin> registeredPlugins) |
| 0 | 173 | | : base(clientTypeName, entityPath, retryPolicy ?? RetryPolicy.Default) |
| | 174 | | { |
| 0 | 175 | | if (string.IsNullOrWhiteSpace(entityPath)) |
| | 176 | | { |
| 0 | 177 | | throw Fx.Exception.ArgumentNullOrWhiteSpace(entityPath); |
| | 178 | | } |
| | 179 | |
|
| 0 | 180 | | this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnect |
| 0 | 181 | | this.EntityPath = entityPath; |
| 0 | 182 | | this.EntityType = entityType; |
| 0 | 183 | | this.ReceiveMode = receiveMode; |
| 0 | 184 | | this.PrefetchCount = prefetchCount; |
| 0 | 185 | | this.ServiceBusConnection.ThrowIfClosed(); |
| | 186 | |
|
| 0 | 187 | | if (cbsTokenProvider != null) |
| | 188 | | { |
| 0 | 189 | | this.CbsTokenProvider = cbsTokenProvider; |
| | 190 | | } |
| 0 | 191 | | else if (this.ServiceBusConnection.TokenProvider != null) |
| | 192 | | { |
| 0 | 193 | | this.CbsTokenProvider = new TokenProviderAdapter(this.ServiceBusConnection.TokenProvider, this.ServiceBu |
| | 194 | | } |
| | 195 | | else |
| | 196 | | { |
| 0 | 197 | | throw new ArgumentNullException($"{nameof(ServiceBusConnection)} doesn't have a valid token provider"); |
| | 198 | | } |
| | 199 | |
|
| 0 | 200 | | this.diagnosticSource = new ServiceBusDiagnosticSource(entityPath, serviceBusConnection.Endpoint); |
| | 201 | |
|
| | 202 | | // Register plugins on the message session. |
| 0 | 203 | | if (registeredPlugins != null) |
| | 204 | | { |
| 0 | 205 | | foreach (var serviceBusPlugin in registeredPlugins) |
| | 206 | | { |
| 0 | 207 | | this.RegisterPlugin(serviceBusPlugin); |
| | 208 | | } |
| | 209 | | } |
| 0 | 210 | | } |
| | 211 | |
|
| 0 | 212 | | ReceiveMode ReceiveMode { get; } |
| | 213 | |
|
| | 214 | | /// <summary> |
| | 215 | | /// Gets the path of the entity. This is either the name of the queue, or the full path of the subscription. |
| | 216 | | /// </summary> |
| 0 | 217 | | public string EntityPath { get; } |
| | 218 | |
|
| | 219 | | /// <summary> |
| | 220 | | /// Gets the path of the entity. This is either the name of the queue, or the full path of the subscription. |
| | 221 | | /// </summary> |
| 0 | 222 | | public override string Path => this.EntityPath; |
| | 223 | |
|
| | 224 | | /// <summary> |
| | 225 | | /// Duration after which individual operations will timeout. |
| | 226 | | /// </summary> |
| | 227 | | public override TimeSpan OperationTimeout |
| | 228 | | { |
| 0 | 229 | | get => this.ServiceBusConnection.OperationTimeout; |
| 0 | 230 | | set => this.ServiceBusConnection.OperationTimeout = value; |
| | 231 | | } |
| | 232 | |
|
| | 233 | | /// <summary> |
| | 234 | | /// Connection object to the service bus namespace. |
| | 235 | | /// </summary> |
| 0 | 236 | | public override ServiceBusConnection ServiceBusConnection { get; } |
| | 237 | |
|
| 0 | 238 | | MessagingEntityType? EntityType { get; } |
| | 239 | |
|
| 0 | 240 | | internal int PrefetchCount { get; set; } |
| | 241 | |
|
| 0 | 242 | | ICbsTokenProvider CbsTokenProvider { get; } |
| | 243 | |
|
| | 244 | | /// <summary> |
| | 245 | | /// Gets a list of currently registered plugins. |
| | 246 | | /// </summary> |
| 0 | 247 | | public override IList<ServiceBusPlugin> RegisteredPlugins { get; } = new List<ServiceBusPlugin>(); |
| | 248 | |
|
| | 249 | | /// <summary> |
| | 250 | | /// Gets a session object of any <see cref="IMessageSession.SessionId"/> that can be used to receive messages fo |
| | 251 | | /// </summary> |
| | 252 | | /// <remarks>All plugins registered on <see cref="SessionClient"/> will be applied to each <see cref="MessageSes |
| | 253 | | /// Individual sessions can further register additional plugins.</remarks> |
| | 254 | | public Task<IMessageSession> AcceptMessageSessionAsync() |
| | 255 | | { |
| 0 | 256 | | return this.AcceptMessageSessionAsync(this.ServiceBusConnection.OperationTimeout); |
| | 257 | | } |
| | 258 | |
|
| | 259 | | /// <summary> |
| | 260 | | /// Gets a session object of any <see cref="IMessageSession.SessionId"/> that can be used to receive messages fo |
| | 261 | | /// </summary> |
| | 262 | | /// <param name="operationTimeout">Amount of time for which the call should wait to fetch the next session.</par |
| | 263 | | /// <remarks>All plugins registered on <see cref="SessionClient"/> will be applied to each <see cref="MessageSes |
| | 264 | | /// Individual sessions can further register additional plugins.</remarks> |
| | 265 | | public Task<IMessageSession> AcceptMessageSessionAsync(TimeSpan operationTimeout) |
| | 266 | | { |
| 0 | 267 | | return this.AcceptMessageSessionAsync(null, operationTimeout); |
| | 268 | | } |
| | 269 | |
|
| | 270 | | /// <summary> |
| | 271 | | /// Gets a particular session object identified by <paramref name="sessionId"/> that can be used to receive mess |
| | 272 | | /// </summary> |
| | 273 | | /// <param name="sessionId">The sessionId present in all its messages.</param> |
| | 274 | | /// <remarks>All plugins registered on <see cref="SessionClient"/> will be applied to each <see cref="MessageSes |
| | 275 | | /// Individual sessions can further register additional plugins.</remarks> |
| | 276 | | public Task<IMessageSession> AcceptMessageSessionAsync(string sessionId) |
| | 277 | | { |
| 0 | 278 | | return this.AcceptMessageSessionAsync(sessionId, this.ServiceBusConnection.OperationTimeout); |
| | 279 | | } |
| | 280 | |
|
| | 281 | | /// <summary> |
| | 282 | | /// Gets a particular session object identified by <paramref name="sessionId"/> that can be used to receive mess |
| | 283 | | /// </summary> |
| | 284 | | /// <param name="sessionId">The sessionId present in all its messages.</param> |
| | 285 | | /// <param name="operationTimeout">Amount of time for which the call should wait to fetch the next session.</par |
| | 286 | | /// <remarks>All plugins registered on <see cref="SessionClient"/> will be applied to each <see cref="MessageSes |
| | 287 | | /// Individual sessions can further register additional plugins.</remarks> |
| | 288 | | public async Task<IMessageSession> AcceptMessageSessionAsync(string sessionId, TimeSpan operationTimeout) |
| | 289 | | { |
| 0 | 290 | | this.ThrowIfClosed(); |
| | 291 | |
|
| 0 | 292 | | MessagingEventSource.Log.AmqpSessionClientAcceptMessageSessionStart( |
| 0 | 293 | | this.ClientId, |
| 0 | 294 | | this.EntityPath, |
| 0 | 295 | | this.ReceiveMode, |
| 0 | 296 | | this.PrefetchCount, |
| 0 | 297 | | sessionId); |
| | 298 | |
|
| 0 | 299 | | bool isDiagnosticSourceEnabled = ServiceBusDiagnosticSource.IsEnabled(); |
| 0 | 300 | | Activity activity = isDiagnosticSourceEnabled ? this.diagnosticSource.AcceptMessageSessionStart(sessionId) : |
| 0 | 301 | | Task acceptMessageSessionTask = null; |
| | 302 | |
|
| 0 | 303 | | var session = new MessageSession( |
| 0 | 304 | | this.EntityPath, |
| 0 | 305 | | this.EntityType, |
| 0 | 306 | | this.ReceiveMode, |
| 0 | 307 | | this.ServiceBusConnection, |
| 0 | 308 | | this.CbsTokenProvider, |
| 0 | 309 | | this.RetryPolicy, |
| 0 | 310 | | this.PrefetchCount, |
| 0 | 311 | | sessionId, |
| 0 | 312 | | true); |
| | 313 | |
|
| | 314 | | try |
| | 315 | | { |
| 0 | 316 | | acceptMessageSessionTask = this.RetryPolicy.RunOperation( |
| 0 | 317 | | () => session.GetSessionReceiverLinkAsync(operationTimeout), |
| 0 | 318 | | operationTimeout); |
| 0 | 319 | | await acceptMessageSessionTask.ConfigureAwait(false); |
| 0 | 320 | | } |
| 0 | 321 | | catch (Exception exception) |
| | 322 | | { |
| 0 | 323 | | if (isDiagnosticSourceEnabled && !(exception is ServiceBusTimeoutException)) |
| | 324 | | { |
| 0 | 325 | | this.diagnosticSource.ReportException(exception); |
| | 326 | | } |
| | 327 | |
|
| 0 | 328 | | MessagingEventSource.Log.AmqpSessionClientAcceptMessageSessionException( |
| 0 | 329 | | this.ClientId, |
| 0 | 330 | | this.EntityPath, |
| 0 | 331 | | exception); |
| | 332 | |
|
| 0 | 333 | | await session.CloseAsync().ConfigureAwait(false); |
| 0 | 334 | | throw AmqpExceptionHelper.GetClientException(exception); |
| | 335 | | } |
| | 336 | | finally |
| | 337 | | { |
| 0 | 338 | | this.diagnosticSource.AcceptMessageSessionStop(activity, session.SessionId, acceptMessageSessionTask?.St |
| | 339 | | } |
| | 340 | |
|
| 0 | 341 | | MessagingEventSource.Log.AmqpSessionClientAcceptMessageSessionStop( |
| 0 | 342 | | this.ClientId, |
| 0 | 343 | | this.EntityPath, |
| 0 | 344 | | session.SessionIdInternal); |
| | 345 | |
|
| 0 | 346 | | session.UpdateClientId(ClientEntity.GenerateClientId(nameof(MessageSession), $"{this.EntityPath}_{session.Se |
| | 347 | | // Register plugins on the message session. |
| 0 | 348 | | foreach (var serviceBusPlugin in this.RegisteredPlugins) |
| | 349 | | { |
| 0 | 350 | | session.RegisterPlugin(serviceBusPlugin); |
| | 351 | | } |
| | 352 | |
|
| 0 | 353 | | return session; |
| 0 | 354 | | } |
| | 355 | |
|
| | 356 | | /// <summary> |
| | 357 | | /// Registers a <see cref="ServiceBusPlugin"/> to be used with this receiver. |
| | 358 | | /// </summary> |
| | 359 | | /// <param name="serviceBusPlugin">The <see cref="ServiceBusPlugin"/> to register.</param> |
| | 360 | | public override void RegisterPlugin(ServiceBusPlugin serviceBusPlugin) |
| | 361 | | { |
| 0 | 362 | | this.ThrowIfClosed(); |
| | 363 | |
|
| 0 | 364 | | if (serviceBusPlugin == null) |
| | 365 | | { |
| 0 | 366 | | throw new ArgumentNullException(nameof(serviceBusPlugin), Resources.ArgumentNullOrWhiteSpace.FormatForUs |
| | 367 | | } |
| 0 | 368 | | if (this.RegisteredPlugins.Any(p => p.Name == serviceBusPlugin.Name)) |
| | 369 | | { |
| 0 | 370 | | throw new ArgumentException(nameof(serviceBusPlugin), Resources.PluginAlreadyRegistered.FormatForUser(na |
| | 371 | | } |
| 0 | 372 | | this.RegisteredPlugins.Add(serviceBusPlugin); |
| 0 | 373 | | } |
| | 374 | |
|
| | 375 | | /// <summary> |
| | 376 | | /// Unregisters a <see cref="ServiceBusPlugin"/>. |
| | 377 | | /// </summary> |
| | 378 | | /// <param name="serviceBusPluginName">The <see cref="ServiceBusPlugin.Name"/> of the plugin to be unregistered. |
| | 379 | | public override void UnregisterPlugin(string serviceBusPluginName) |
| | 380 | | { |
| 0 | 381 | | this.ThrowIfClosed(); |
| | 382 | |
|
| 0 | 383 | | if (this.RegisteredPlugins == null) |
| | 384 | | { |
| 0 | 385 | | return; |
| | 386 | | } |
| 0 | 387 | | if (string.IsNullOrWhiteSpace(serviceBusPluginName)) |
| | 388 | | { |
| 0 | 389 | | throw new ArgumentNullException(nameof(serviceBusPluginName), Resources.ArgumentNullOrWhiteSpace.FormatF |
| | 390 | | } |
| 0 | 391 | | if (this.RegisteredPlugins.Any(p => p.Name == serviceBusPluginName)) |
| | 392 | | { |
| 0 | 393 | | var plugin = this.RegisteredPlugins.First(p => p.Name == serviceBusPluginName); |
| 0 | 394 | | this.RegisteredPlugins.Remove(plugin); |
| | 395 | | } |
| 0 | 396 | | } |
| | 397 | |
|
| | 398 | | protected override Task OnClosingAsync() |
| | 399 | | { |
| 0 | 400 | | return Task.CompletedTask; |
| | 401 | | } |
| | 402 | | } |
| | 403 | | } |