| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.ComponentModel; |
| | 7 | | using System.Diagnostics.CodeAnalysis; |
| | 8 | | using System.Globalization; |
| | 9 | | using System.Threading; |
| | 10 | | using System.Threading.Tasks; |
| | 11 | | using Azure.Core; |
| | 12 | | using Azure.Messaging.ServiceBus.Core; |
| | 13 | | using Azure.Messaging.ServiceBus.Diagnostics; |
| | 14 | | using Azure.Messaging.ServiceBus.Plugins; |
| | 15 | |
|
| | 16 | | namespace Azure.Messaging.ServiceBus |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// The <see cref="ServiceBusProcessor"/> provides an abstraction around a set of <see cref="ServiceBusReceiver"/> t |
| | 20 | | /// allows using an event based model for processing received <see cref="ServiceBusReceivedMessage" />. It is constr |
| | 21 | | /// <see cref="ServiceBusClient.CreateProcessor(string, ServiceBusProcessorOptions)"/>. |
| | 22 | | /// The event handler is specified with the <see cref="ProcessMessageAsync"/> |
| | 23 | | /// property. The error handler is specified with the <see cref="ProcessErrorAsync"/> property. |
| | 24 | | /// To start processing after the handlers have been specified, call <see cref="StartProcessingAsync"/>. |
| | 25 | | /// </summary> |
| | 26 | | #pragma warning disable CA1001 // Types that own disposable fields should be disposable |
| | 27 | | public class ServiceBusProcessor |
| | 28 | | #pragma warning restore CA1001 // Types that own disposable fields should be disposable |
| | 29 | | { |
| | 30 | | private Func<ProcessMessageEventArgs, Task> _processMessageAsync; |
| | 31 | |
|
| | 32 | | private Func<ProcessSessionMessageEventArgs, Task> _processSessionMessageAsync; |
| | 33 | |
|
| | 34 | | private Func<ProcessErrorEventArgs, Task> _processErrorAsync = default; |
| | 35 | |
|
| | 36 | | private Func<ProcessSessionEventArgs, Task> _sessionInitializingAsync; |
| | 37 | |
|
| | 38 | | private Func<ProcessSessionEventArgs, Task> _sessionClosingAsync; |
| | 39 | |
|
| | 40 | | private SemaphoreSlim MessageHandlerSemaphore; |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// |
| | 44 | | /// </summary> |
| 0 | 45 | | private SemaphoreSlim MaxConcurrentAcceptSessionsSemaphore { get; set; } |
| | 46 | |
|
| | 47 | | /// <summary>The primitive for synchronizing access during start and close operations.</summary> |
| 0 | 48 | | private readonly SemaphoreSlim ProcessingStartStopSemaphore = new SemaphoreSlim(1, 1); |
| | 49 | |
|
| 28 | 50 | | private CancellationTokenSource RunningTaskTokenSource { get; set; } |
| | 51 | |
|
| 180 | 52 | | private Task ActiveReceiveTask { get; set; } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// The fully qualified Service Bus namespace that the receiver is associated with. This is likely |
| | 56 | | /// to be similar to <c>{yournamespace}.servicebus.windows.net</c>. |
| | 57 | | /// </summary> |
| 42 | 58 | | public string FullyQualifiedNamespace => _connection.FullyQualifiedNamespace; |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// The path of the Service Bus entity that the processor is connected to, specific to the |
| | 62 | | /// Service Bus namespace that contains it. |
| | 63 | | /// </summary> |
| 140 | 64 | | public string EntityPath { get; private set; } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Gets the ID to identify this client. This can be used to correlate logs and exceptions. |
| | 68 | | /// </summary> |
| | 69 | | /// <remarks>Every new client has a unique ID.</remarks> |
| 110 | 70 | | internal string Identifier { get; private set; } |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// The <see cref="ReceiveMode"/> used to specify how messages are received. Defaults to PeekLock mode. |
| | 74 | | /// </summary> |
| 4 | 75 | | public ReceiveMode ReceiveMode { get; } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Indicates whether the processor is configured to process session entities. |
| | 79 | | /// </summary> |
| 18 | 80 | | internal bool IsSessionProcessor { get; } |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// The number of messages that will be eagerly requested from Queues or Subscriptions |
| | 84 | | /// during processing. This is intended to help maximize throughput by allowing the |
| | 85 | | /// processor to receive from a local cache rather than waiting on a service request. |
| | 86 | | /// </summary> |
| 4 | 87 | | public int PrefetchCount { get; } |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Indicates whether or not this <see cref="ServiceBusProcessor"/> is currently processing messages. |
| | 91 | | /// </summary> |
| | 92 | | /// |
| | 93 | | /// <value> |
| | 94 | | /// <c>true</c> if the client is processing messages; otherwise, <c>false</c>. |
| | 95 | | /// </value> |
| 0 | 96 | | public bool IsProcessing => ActiveReceiveTask != null; |
| | 97 | |
|
| | 98 | | private readonly ServiceBusProcessorOptions _options; |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// The active connection to the Azure Service Bus service, enabling client communications for metadata |
| | 102 | | /// about the associated Service Bus entity and access to transport-aware consumers. |
| | 103 | | /// </summary> |
| | 104 | | private readonly ServiceBusConnection _connection; |
| | 105 | |
|
| | 106 | | /// <summary>Gets or sets the maximum number of concurrent calls to the |
| | 107 | | /// <see cref="ProcessMessageAsync"/> event handler the processor should initiate. |
| | 108 | | /// </summary> |
| | 109 | | /// |
| | 110 | | /// <value>The maximum number of concurrent calls to the event handler.</value> |
| 26 | 111 | | public int MaxConcurrentCalls { get; } |
| | 112 | |
|
| | 113 | | /// <summary> |
| | 114 | | /// The maximum amount of time to wait for each Receive call using the processor's underlying receiver. If not s |
| | 115 | | /// </summary> |
| 2 | 116 | | public TimeSpan? MaxReceiveWaitTime { get; } |
| | 117 | |
|
| | 118 | | /// <summary>Gets or sets a value that indicates whether the <see cref="ServiceBusProcessor"/> should automatica |
| | 119 | | /// complete messages after the event handler has completed processing. If the event handler |
| | 120 | | /// triggers an exception, the message will not be automatically completed.</summary> |
| | 121 | | /// |
| | 122 | | /// <value>true to complete the message processing automatically on successful execution of the operation; other |
| 4 | 123 | | public bool AutoComplete { get; } |
| | 124 | |
|
| | 125 | | /// <summary> |
| | 126 | | /// Gets or sets the maximum duration within which the lock will be renewed automatically. This |
| | 127 | | /// value should be greater than the longest message lock duration; for example, the LockDuration Property. |
| | 128 | | /// </summary> |
| | 129 | | /// |
| | 130 | | /// <value>The maximum duration during which locks are automatically renewed.</value> |
| | 131 | | /// |
| | 132 | | /// <remarks>The message renew can continue for sometime in the background |
| | 133 | | /// after completion of message and result in a few false MessageLockLostExceptions temporarily.</remarks> |
| 4 | 134 | | public TimeSpan MaxAutoLockRenewalDuration { get; } |
| | 135 | |
|
| | 136 | | /// <summary> |
| | 137 | | /// The instance of <see cref="ServiceBusEventSource" /> which can be mocked for testing. |
| | 138 | | /// </summary> |
| | 139 | | /// |
| 0 | 140 | | internal ServiceBusEventSource Logger { get; set; } = ServiceBusEventSource.Log; |
| 16 | 141 | | internal int MaxConcurrentSessions { get; } |
| 16 | 142 | | internal int MaxConcurrentCallsPerSession { get; } |
| | 143 | |
|
| | 144 | | private readonly string[] _sessionIds; |
| | 145 | | private readonly EntityScopeFactory _scopeFactory; |
| | 146 | | private readonly IList<ServiceBusPlugin> _plugins; |
| 0 | 147 | | private readonly IList<ReceiverManager> _receiverManagers = new List<ReceiverManager>(); |
| | 148 | |
|
| | 149 | | /// <summary> |
| | 150 | | /// Initializes a new instance of the <see cref="ServiceBusProcessor"/> class. |
| | 151 | | /// </summary> |
| | 152 | | /// |
| | 153 | | /// <param name="connection">The <see cref="ServiceBusConnection" /> connection to use for communication with th |
| | 154 | | /// <param name="entityPath">The queue name or subscription path to process messages from.</param> |
| | 155 | | /// <param name="isSessionEntity">Whether or not the processor is associated with a session entity.</param> |
| | 156 | | /// <param name="plugins">The set of plugins to apply to incoming messages.</param> |
| | 157 | | /// <param name="options">The set of options to use when configuring the processor.</param> |
| | 158 | | /// <param name="sessionIds">An optional set of session Ids to limit processing to. |
| | 159 | | /// Only applies if isSessionEntity is true.</param> |
| | 160 | | /// <param name="maxConcurrentSessions">The max number of sessions that can be processed concurrently. |
| | 161 | | /// Only applies if isSessionEntity is true.</param> |
| | 162 | | /// <param name="maxConcurrentCallsPerSession">The max number of concurrent calls per session. |
| | 163 | | /// Only applies if isSessionEntity is true.</param> |
| 34 | 164 | | internal ServiceBusProcessor( |
| 34 | 165 | | ServiceBusConnection connection, |
| 34 | 166 | | string entityPath, |
| 34 | 167 | | bool isSessionEntity, |
| 34 | 168 | | IList<ServiceBusPlugin> plugins, |
| 34 | 169 | | ServiceBusProcessorOptions options, |
| 34 | 170 | | string[] sessionIds = default, |
| 34 | 171 | | int maxConcurrentSessions = default, |
| 34 | 172 | | int maxConcurrentCallsPerSession = default) |
| | 173 | | { |
| 34 | 174 | | Argument.AssertNotNullOrWhiteSpace(entityPath, nameof(entityPath)); |
| 34 | 175 | | Argument.AssertNotNull(connection, nameof(connection)); |
| 34 | 176 | | Argument.AssertNotNull(connection.RetryOptions, nameof(connection.RetryOptions)); |
| 34 | 177 | | connection.ThrowIfClosed(); |
| | 178 | |
|
| 34 | 179 | | _options = options?.Clone() ?? new ServiceBusProcessorOptions(); |
| 34 | 180 | | _connection = connection; |
| 34 | 181 | | EntityPath = entityPath; |
| 34 | 182 | | Identifier = DiagnosticUtilities.GenerateIdentifier(EntityPath); |
| | 183 | |
|
| 34 | 184 | | ReceiveMode = _options.ReceiveMode; |
| 34 | 185 | | PrefetchCount = _options.PrefetchCount; |
| 34 | 186 | | MaxAutoLockRenewalDuration = _options.MaxAutoLockRenewalDuration; |
| 34 | 187 | | MaxConcurrentCalls = _options.MaxConcurrentCalls; |
| 34 | 188 | | MaxConcurrentSessions = maxConcurrentSessions; |
| 34 | 189 | | MaxConcurrentCallsPerSession = maxConcurrentCallsPerSession; |
| 34 | 190 | | _sessionIds = sessionIds ?? Array.Empty<string>(); |
| | 191 | |
|
| 34 | 192 | | int maxCalls = isSessionEntity ? |
| 34 | 193 | | (_sessionIds.Length > 0 ? |
| 34 | 194 | | Math.Min(_sessionIds.Length, MaxConcurrentSessions) : |
| 34 | 195 | | MaxConcurrentSessions) * MaxConcurrentCallsPerSession : |
| 34 | 196 | | MaxConcurrentCalls; |
| | 197 | |
|
| 34 | 198 | | MessageHandlerSemaphore = new SemaphoreSlim( |
| 34 | 199 | | maxCalls, |
| 34 | 200 | | maxCalls); |
| 34 | 201 | | var maxAcceptSessions = Math.Min(maxCalls, 2 * Environment.ProcessorCount); |
| 34 | 202 | | MaxConcurrentAcceptSessionsSemaphore = new SemaphoreSlim( |
| 34 | 203 | | maxAcceptSessions, |
| 34 | 204 | | maxAcceptSessions); |
| | 205 | |
|
| 34 | 206 | | MaxReceiveWaitTime = _options.MaxReceiveWaitTime; |
| 34 | 207 | | AutoComplete = _options.AutoComplete; |
| | 208 | |
|
| 34 | 209 | | EntityPath = entityPath; |
| 34 | 210 | | IsSessionProcessor = isSessionEntity; |
| 34 | 211 | | _scopeFactory = new EntityScopeFactory(EntityPath, FullyQualifiedNamespace); |
| 34 | 212 | | _plugins = plugins; |
| 34 | 213 | | } |
| | 214 | |
|
| | 215 | | /// <summary> |
| | 216 | | /// Initializes a new instance of the <see cref="ServiceBusProcessor"/> class for mocking. |
| | 217 | | /// </summary> |
| 0 | 218 | | protected ServiceBusProcessor() |
| | 219 | | { |
| 0 | 220 | | } |
| | 221 | |
|
| | 222 | | /// <summary> |
| | 223 | | /// Determines whether the specified <see cref="System.Object" /> is equal to this instance. |
| | 224 | | /// </summary> |
| | 225 | | /// |
| | 226 | | /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param> |
| | 227 | | /// |
| | 228 | | /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c> |
| | 229 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 230 | | public override bool Equals(object obj) => base.Equals(obj); |
| | 231 | |
|
| | 232 | | /// <summary> |
| | 233 | | /// Returns a hash code for this instance. |
| | 234 | | /// </summary> |
| | 235 | | /// |
| | 236 | | /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a ha |
| | 237 | | /// |
| | 238 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 239 | | public override int GetHashCode() => base.GetHashCode(); |
| | 240 | |
|
| | 241 | | /// <summary> |
| | 242 | | /// Converts the instance to string representation. |
| | 243 | | /// </summary> |
| | 244 | | /// |
| | 245 | | /// <returns>A <see cref="System.String" /> that represents this instance.</returns> |
| | 246 | | /// |
| | 247 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 248 | | public override string ToString() => base.ToString(); |
| | 249 | |
|
| | 250 | | /// <summary> |
| | 251 | | /// The event responsible for processing messages received from the Queue or Subscription. |
| | 252 | | /// Implementation is mandatory. |
| | 253 | | /// </summary> |
| | 254 | | /// |
| | 255 | | [SuppressMessage("Usage", "AZC0002:Ensure all service methods take an optional CancellationToken parameter.", Ju |
| | 256 | | [SuppressMessage("Usage", "AZC0003:DO make service methods virtual.", Justification = "This member follows the s |
| | 257 | | public event Func<ProcessMessageEventArgs, Task> ProcessMessageAsync |
| | 258 | | { |
| | 259 | | add |
| | 260 | | { |
| 18 | 261 | | Argument.AssertNotNull(value, nameof(ProcessMessageAsync)); |
| | 262 | |
|
| 16 | 263 | | if (_processMessageAsync != default) |
| | 264 | | { |
| | 265 | | #pragma warning disable CA1065 // Do not raise exceptions in unexpected locations |
| 2 | 266 | | throw new NotSupportedException(Resources.HandlerHasAlreadyBeenAssigned); |
| | 267 | | #pragma warning restore CA1065 // Do not raise exceptions in unexpected locations |
| | 268 | | } |
| 28 | 269 | | EnsureNotRunningAndInvoke(() => _processMessageAsync = value); |
| | 270 | |
|
| 14 | 271 | | } |
| | 272 | |
|
| | 273 | | remove |
| | 274 | | { |
| 6 | 275 | | Argument.AssertNotNull(value, nameof(ProcessMessageAsync)); |
| | 276 | |
|
| 6 | 277 | | if (_processMessageAsync != value) |
| | 278 | | { |
| | 279 | | #pragma warning disable CA1065 // Do not raise exceptions in unexpected locations |
| 4 | 280 | | throw new ArgumentException(Resources.HandlerHasNotBeenAssigned); |
| | 281 | | #pragma warning restore CA1065 // Do not raise exceptions in unexpected locations |
| | 282 | | } |
| | 283 | |
|
| 4 | 284 | | EnsureNotRunningAndInvoke(() => _processMessageAsync = default); |
| 2 | 285 | | } |
| | 286 | | } |
| | 287 | |
|
| | 288 | | /// <summary> |
| | 289 | | /// The event responsible for processing messages received from the Queue or Subscription. Implementation |
| | 290 | | /// is mandatory. |
| | 291 | | /// </summary> |
| | 292 | | /// |
| | 293 | | [SuppressMessage("Usage", "AZC0002:Ensure all service methods take an optional CancellationToken parameter.", Ju |
| | 294 | | [SuppressMessage("Usage", "AZC0003:DO make service methods virtual.", Justification = "This member follows the s |
| | 295 | | internal event Func<ProcessSessionMessageEventArgs, Task> ProcessSessionMessageAsync |
| | 296 | | { |
| | 297 | | add |
| | 298 | | { |
| 14 | 299 | | Argument.AssertNotNull(value, nameof(ProcessMessageAsync)); |
| | 300 | |
|
| 12 | 301 | | if (_processSessionMessageAsync != default) |
| | 302 | | { |
| 2 | 303 | | throw new NotSupportedException(Resources.HandlerHasAlreadyBeenAssigned); |
| | 304 | | } |
| 20 | 305 | | EnsureNotRunningAndInvoke(() => _processSessionMessageAsync = value); |
| | 306 | |
|
| 10 | 307 | | } |
| | 308 | |
|
| | 309 | | remove |
| | 310 | | { |
| 6 | 311 | | Argument.AssertNotNull(value, nameof(ProcessMessageAsync)); |
| | 312 | |
|
| 6 | 313 | | if (_processSessionMessageAsync != value) |
| | 314 | | { |
| 4 | 315 | | throw new ArgumentException(Resources.HandlerHasNotBeenAssigned); |
| | 316 | | } |
| | 317 | |
|
| 4 | 318 | | EnsureNotRunningAndInvoke(() => _processSessionMessageAsync = default); |
| 2 | 319 | | } |
| | 320 | | } |
| | 321 | |
|
| | 322 | | /// <summary> |
| | 323 | | /// The event responsible for processing unhandled exceptions thrown while this processor is running. |
| | 324 | | /// Implementation is mandatory. |
| | 325 | | /// </summary> |
| | 326 | | /// |
| | 327 | | [SuppressMessage("Usage", "AZC0002:Ensure all service methods take an optional CancellationToken parameter.", Ju |
| | 328 | | [SuppressMessage("Usage", "AZC0003:DO make service methods virtual.", Justification = "This member follows the s |
| | 329 | | public event Func<ProcessErrorEventArgs, Task> ProcessErrorAsync |
| | 330 | | { |
| | 331 | | add |
| | 332 | | { |
| 28 | 333 | | Argument.AssertNotNull(value, nameof(ProcessErrorAsync)); |
| | 334 | |
|
| 24 | 335 | | if (_processErrorAsync != default) |
| | 336 | | { |
| | 337 | | #pragma warning disable CA1065 // Do not raise exceptions in unexpected locations |
| 4 | 338 | | throw new NotSupportedException(Resources.HandlerHasAlreadyBeenAssigned); |
| | 339 | | #pragma warning restore CA1065 // Do not raise exceptions in unexpected locations |
| | 340 | | } |
| | 341 | |
|
| 40 | 342 | | EnsureNotRunningAndInvoke(() => _processErrorAsync = value); |
| 20 | 343 | | } |
| | 344 | |
|
| | 345 | | remove |
| | 346 | | { |
| 12 | 347 | | Argument.AssertNotNull(value, nameof(ProcessErrorAsync)); |
| | 348 | |
|
| 12 | 349 | | if (_processErrorAsync != value) |
| | 350 | | { |
| | 351 | | #pragma warning disable CA1065 // Do not raise exceptions in unexpected locations |
| 8 | 352 | | throw new ArgumentException(Resources.HandlerHasNotBeenAssigned); |
| | 353 | | #pragma warning restore CA1065 // Do not raise exceptions in unexpected locations |
| | 354 | | } |
| | 355 | |
|
| 8 | 356 | | EnsureNotRunningAndInvoke(() => _processErrorAsync = default); |
| 4 | 357 | | } |
| | 358 | | } |
| | 359 | |
|
| | 360 | | /// <summary> |
| | 361 | | /// Optional event that can be set to be notified when a new session is about to be processed. |
| | 362 | | /// </summary> |
| | 363 | | [SuppressMessage("Usage", "AZC0002:Ensure all service methods take an optional CancellationToken parameter.", Ju |
| | 364 | | [SuppressMessage("Usage", "AZC0003:DO make service methods virtual.", Justification = "This member follows the s |
| | 365 | | internal event Func<ProcessSessionEventArgs, Task> SessionInitializingAsync |
| | 366 | | { |
| | 367 | | add |
| | 368 | | { |
| 12 | 369 | | Argument.AssertNotNull(value, nameof(SessionInitializingAsync)); |
| | 370 | |
|
| 10 | 371 | | if (_sessionInitializingAsync != default) |
| | 372 | | { |
| 2 | 373 | | throw new NotSupportedException(Resources.HandlerHasAlreadyBeenAssigned); |
| | 374 | | } |
| 16 | 375 | | EnsureNotRunningAndInvoke(() => _sessionInitializingAsync = value); |
| | 376 | |
|
| 8 | 377 | | } |
| | 378 | |
|
| | 379 | | remove |
| | 380 | | { |
| 6 | 381 | | Argument.AssertNotNull(value, nameof(SessionInitializingAsync)); |
| 6 | 382 | | if (_sessionInitializingAsync != value) |
| | 383 | | { |
| 4 | 384 | | throw new ArgumentException(Resources.HandlerHasNotBeenAssigned); |
| | 385 | | } |
| 4 | 386 | | EnsureNotRunningAndInvoke(() => _sessionInitializingAsync = default); |
| 2 | 387 | | } |
| | 388 | | } |
| | 389 | |
|
| | 390 | | /// <summary> |
| | 391 | | /// Optional event that can be set to be notified when a session is about to be closed for processing. |
| | 392 | | /// This means that the most recent <see cref="ServiceBusReceiver.ReceiveMessageAsync"/> call timed out so there |
| | 393 | | /// available to be received for the session. |
| | 394 | | /// </summary> |
| | 395 | | [SuppressMessage("Usage", "AZC0002:Ensure all service methods take an optional CancellationToken parameter.", Ju |
| | 396 | | [SuppressMessage("Usage", "AZC0003:DO make service methods virtual.", Justification = "This member follows the s |
| | 397 | | internal event Func<ProcessSessionEventArgs, Task> SessionClosingAsync |
| | 398 | | { |
| | 399 | | add |
| | 400 | | { |
| 12 | 401 | | Argument.AssertNotNull(value, nameof(SessionClosingAsync)); |
| | 402 | |
|
| 10 | 403 | | if (_sessionClosingAsync != default) |
| | 404 | | { |
| 2 | 405 | | throw new NotSupportedException(Resources.HandlerHasAlreadyBeenAssigned); |
| | 406 | | } |
| 16 | 407 | | EnsureNotRunningAndInvoke(() => _sessionClosingAsync = value); |
| | 408 | |
|
| 8 | 409 | | } |
| | 410 | |
|
| | 411 | | remove |
| | 412 | | { |
| 6 | 413 | | Argument.AssertNotNull(value, nameof(SessionClosingAsync)); |
| 6 | 414 | | if (_sessionClosingAsync != value) |
| | 415 | | { |
| 4 | 416 | | throw new ArgumentException(Resources.HandlerHasNotBeenAssigned); |
| | 417 | | } |
| 4 | 418 | | EnsureNotRunningAndInvoke(() => _sessionClosingAsync = default); |
| 2 | 419 | | } |
| | 420 | | } |
| | 421 | |
|
| | 422 | | /// <summary> |
| | 423 | | /// Signals the <see cref="ServiceBusProcessor" /> to begin processing messages. Should this method be called wh |
| | 424 | | /// is running, no action is taken. |
| | 425 | | /// </summary> |
| | 426 | | /// |
| | 427 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> instance to signal the request to cancel t |
| | 428 | | public virtual async Task StartProcessingAsync( |
| | 429 | | CancellationToken cancellationToken = default) |
| | 430 | | { |
| 14 | 431 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| 14 | 432 | | if (ActiveReceiveTask == null) |
| | 433 | | { |
| 14 | 434 | | Logger.StartProcessingStart(Identifier); |
| 14 | 435 | | bool releaseGuard = false; |
| | 436 | |
|
| | 437 | | try |
| | 438 | | { |
| 14 | 439 | | await ProcessingStartStopSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); |
| 14 | 440 | | releaseGuard = true; |
| 14 | 441 | | ValidateMessageHandler(); |
| 8 | 442 | | ValidateErrorHandler(); |
| 4 | 443 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| | 444 | |
|
| 4 | 445 | | InitializeReceiverManagers(); |
| | 446 | |
|
| | 447 | | // We expect the token source to be null, but we are playing safe. |
| | 448 | |
|
| 4 | 449 | | RunningTaskTokenSource?.Cancel(); |
| 4 | 450 | | RunningTaskTokenSource?.Dispose(); |
| 4 | 451 | | RunningTaskTokenSource = new CancellationTokenSource(); |
| | 452 | |
|
| | 453 | | // Start the main running task. |
| 4 | 454 | | ActiveReceiveTask = RunReceiveTaskAsync(RunningTaskTokenSource.Token); |
| 4 | 455 | | } |
| 10 | 456 | | catch (Exception exception) |
| | 457 | | { |
| 10 | 458 | | Logger.StartProcessingException(Identifier, exception.ToString()); |
| 10 | 459 | | throw; |
| | 460 | | } |
| | 461 | | finally |
| | 462 | | { |
| 14 | 463 | | if (releaseGuard) |
| | 464 | | { |
| 14 | 465 | | ProcessingStartStopSemaphore.Release(); |
| | 466 | | } |
| | 467 | | } |
| 4 | 468 | | Logger.StartProcessingComplete(Identifier); |
| | 469 | | } |
| | 470 | | else |
| | 471 | | { |
| 0 | 472 | | throw new InvalidOperationException(Resources.RunningMessageProcessorCannotPerformOperation); |
| | 473 | | } |
| 4 | 474 | | } |
| | 475 | |
|
| | 476 | | private void InitializeReceiverManagers() |
| | 477 | | { |
| 4 | 478 | | if (IsSessionProcessor) |
| | 479 | | { |
| 0 | 480 | | var numReceivers = _sessionIds.Length > 0 ? _sessionIds.Length : MaxConcurrentSessions; |
| 0 | 481 | | for (int i = 0; i < numReceivers; i++) |
| | 482 | | { |
| 0 | 483 | | var sessionId = _sessionIds.Length > 0 ? _sessionIds[i] : null; |
| | 484 | | // If the user has listed named sessions, and they |
| | 485 | | // have MaxConcurrentSessions greater or equal to the number |
| | 486 | | // of sessions, we can leave the sessions open at all times |
| | 487 | | // instead of cycling through them as receive calls time out. |
| 0 | 488 | | bool keepOpenOnReceiveTimeout = _sessionIds.Length > 0 && |
| 0 | 489 | | MaxConcurrentSessions >= _sessionIds.Length; |
| | 490 | |
|
| 0 | 491 | | _receiverManagers.Add( |
| 0 | 492 | | new SessionReceiverManager( |
| 0 | 493 | | _connection, |
| 0 | 494 | | FullyQualifiedNamespace, |
| 0 | 495 | | EntityPath, |
| 0 | 496 | | Identifier, |
| 0 | 497 | | sessionId, |
| 0 | 498 | | _options, |
| 0 | 499 | | _sessionInitializingAsync, |
| 0 | 500 | | _sessionClosingAsync, |
| 0 | 501 | | _processSessionMessageAsync, |
| 0 | 502 | | _processErrorAsync, |
| 0 | 503 | | MaxConcurrentAcceptSessionsSemaphore, |
| 0 | 504 | | _scopeFactory, |
| 0 | 505 | | _plugins, |
| 0 | 506 | | MaxConcurrentCallsPerSession, |
| 0 | 507 | | keepOpenOnReceiveTimeout)); |
| | 508 | | } |
| | 509 | | } |
| | 510 | | else |
| | 511 | | { |
| 4 | 512 | | _receiverManagers.Add( |
| 4 | 513 | | new ReceiverManager( |
| 4 | 514 | | _connection, |
| 4 | 515 | | FullyQualifiedNamespace, |
| 4 | 516 | | EntityPath, |
| 4 | 517 | | Identifier, |
| 4 | 518 | | _options, |
| 4 | 519 | | _processMessageAsync, |
| 4 | 520 | | _processErrorAsync, |
| 4 | 521 | | _scopeFactory, |
| 4 | 522 | | _plugins)); |
| | 523 | | } |
| 4 | 524 | | } |
| | 525 | |
|
| | 526 | | private void ValidateErrorHandler() |
| | 527 | | { |
| 8 | 528 | | if (_processErrorAsync == null) |
| | 529 | | { |
| 4 | 530 | | throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.CannotStartMessa |
| | 531 | | } |
| 4 | 532 | | } |
| | 533 | |
|
| | 534 | | private void ValidateMessageHandler() |
| | 535 | | { |
| 14 | 536 | | if (IsSessionProcessor) |
| | 537 | | { |
| 4 | 538 | | if (_processSessionMessageAsync == null) |
| | 539 | | { |
| 2 | 540 | | throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.CannotStartM |
| | 541 | | } |
| | 542 | | } |
| 10 | 543 | | else if (_processMessageAsync == null) |
| | 544 | | { |
| 4 | 545 | | throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.CannotStartMessa |
| | 546 | | } |
| 8 | 547 | | } |
| | 548 | |
|
| | 549 | | /// <summary> |
| | 550 | | /// Signals the <see cref="ServiceBusProcessor" /> to stop processing events. Should this method be called while |
| | 551 | | /// is not running, no action is taken. |
| | 552 | | /// </summary> |
| | 553 | | /// |
| | 554 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> instance to signal the request to cancel t |
| | 555 | | public virtual async Task StopProcessingAsync(CancellationToken cancellationToken = default) |
| | 556 | | { |
| 6 | 557 | | bool releaseGuard = false; |
| | 558 | | try |
| | 559 | | { |
| 6 | 560 | | if (ActiveReceiveTask != null) |
| | 561 | | { |
| 6 | 562 | | Logger.StopProcessingStart(Identifier); |
| 6 | 563 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| | 564 | |
|
| 4 | 565 | | await ProcessingStartStopSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); |
| 4 | 566 | | releaseGuard = true; |
| | 567 | |
|
| 4 | 568 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| | 569 | |
|
| | 570 | | // Cancel the current running task. |
| | 571 | |
|
| 4 | 572 | | RunningTaskTokenSource.Cancel(); |
| 4 | 573 | | RunningTaskTokenSource.Dispose(); |
| 4 | 574 | | RunningTaskTokenSource = null; |
| | 575 | |
|
| | 576 | | // Now that a cancellation request has been issued, wait for the running task to finish. In case so |
| | 577 | | // unexpected happened and it stopped working midway, this is the moment we expect to catch an excep |
| | 578 | | try |
| | 579 | | { |
| 4 | 580 | | await ActiveReceiveTask.ConfigureAwait(false); |
| 0 | 581 | | } |
| 4 | 582 | | catch (Exception ex) when (ex is TaskCanceledException || ex is OperationCanceledException) |
| | 583 | | { |
| | 584 | | // Nothing to do here. These exceptions are expected. |
| 4 | 585 | | } |
| | 586 | |
|
| 4 | 587 | | ActiveReceiveTask.Dispose(); |
| 4 | 588 | | ActiveReceiveTask = null; |
| | 589 | |
|
| 16 | 590 | | foreach (ReceiverManager receiverManager in _receiverManagers) |
| | 591 | | { |
| 4 | 592 | | await receiverManager.CloseReceiverIfNeeded( |
| 4 | 593 | | cancellationToken) |
| 4 | 594 | | .ConfigureAwait(false); |
| | 595 | | } |
| | 596 | | } |
| 4 | 597 | | } |
| 2 | 598 | | catch (Exception exception) |
| | 599 | | { |
| 2 | 600 | | Logger.StopProcessingException(Identifier, exception.ToString()); |
| 2 | 601 | | throw; |
| | 602 | | } |
| | 603 | | finally |
| | 604 | | { |
| 6 | 605 | | if (releaseGuard) |
| | 606 | | { |
| 4 | 607 | | ProcessingStartStopSemaphore.Release(); |
| | 608 | | } |
| | 609 | | } |
| 4 | 610 | | Logger.StopProcessingComplete(Identifier); |
| 4 | 611 | | } |
| | 612 | |
|
| | 613 | | /// <summary> |
| | 614 | | /// Runs the Receive task in as many threads as are |
| | 615 | | /// specified in the <see cref="MaxConcurrentCalls"/> property. |
| | 616 | | /// </summary> |
| | 617 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> instance to signal the request to cancel t |
| | 618 | | private async Task RunReceiveTaskAsync( |
| | 619 | | CancellationToken cancellationToken) |
| | 620 | | { |
| 4 | 621 | | List<Task> tasks = new List<Task>(); |
| | 622 | | try |
| | 623 | | { |
| 8 | 624 | | while (!cancellationToken.IsCancellationRequested) |
| | 625 | | { |
| 28 | 626 | | foreach (ReceiverManager receiverManager in _receiverManagers) |
| | 627 | | { |
| 8 | 628 | | await MessageHandlerSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); |
| | 629 | | // hold onto all the tasks that we are starting so that when cancellation is requested, |
| | 630 | | // we can await them to make sure we surface any unexpected exceptions, i.e. exceptions |
| | 631 | | // other than TaskCanceledExceptions |
| 4 | 632 | | tasks.Add(ReceiveAndProcessMessagesAsync(receiverManager, cancellationToken)); |
| 4 | 633 | | } |
| 4 | 634 | | if (tasks.Count > MaxConcurrentCalls) |
| | 635 | | { |
| 0 | 636 | | tasks.RemoveAll(t => t.IsCompleted); |
| | 637 | | } |
| | 638 | | } |
| | 639 | | } |
| | 640 | | finally |
| | 641 | | { |
| 4 | 642 | | await Task.WhenAll(tasks).ConfigureAwait(false); |
| | 643 | | } |
| 0 | 644 | | } |
| | 645 | |
|
| | 646 | | private async Task ReceiveAndProcessMessagesAsync( |
| | 647 | | ReceiverManager receiverManager, |
| | 648 | | CancellationToken cancellationToken) |
| | 649 | | { |
| | 650 | | try |
| | 651 | | { |
| 4 | 652 | | await receiverManager.ReceiveAndProcessMessagesAsync(cancellationToken).ConfigureAwait(false); |
| 4 | 653 | | } |
| | 654 | | finally |
| | 655 | | { |
| 4 | 656 | | MessageHandlerSemaphore.Release(); |
| | 657 | | } |
| 4 | 658 | | } |
| | 659 | |
|
| | 660 | | /// <summary> |
| | 661 | | /// Invokes a specified action only if this <see cref="ServiceBusProcessor" /> instance is not running. |
| | 662 | | /// </summary> |
| | 663 | | /// |
| | 664 | | /// <param name="action">The action to invoke.</param> |
| | 665 | | /// |
| | 666 | | /// <exception cref="InvalidOperationException">Occurs when this method is invoked while the event processor is |
| | 667 | | internal void EnsureNotRunningAndInvoke(Action action) |
| | 668 | | { |
| 72 | 669 | | if (ActiveReceiveTask == null) |
| | 670 | | { |
| | 671 | | try |
| | 672 | | { |
| 72 | 673 | | ProcessingStartStopSemaphore.Wait(); |
| 72 | 674 | | if (ActiveReceiveTask == null) |
| | 675 | | { |
| 72 | 676 | | action?.Invoke(); |
| | 677 | | } |
| | 678 | | else |
| | 679 | | { |
| 0 | 680 | | throw new InvalidOperationException(Resources.RunningMessageProcessorCannotPerformOperation); |
| | 681 | | } |
| | 682 | | } |
| | 683 | | finally |
| | 684 | | { |
| 72 | 685 | | ProcessingStartStopSemaphore.Release(); |
| 72 | 686 | | } |
| | 687 | | } |
| | 688 | | else |
| | 689 | | { |
| 0 | 690 | | throw new InvalidOperationException(Resources.RunningMessageProcessorCannotPerformOperation); |
| | 691 | | } |
| 72 | 692 | | } |
| | 693 | | } |
| | 694 | | } |