| | 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 Primitives; |
| | 7 | | using System; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | |
|
| | 11 | | /// <summary>Provides options associated with message pump processing using |
| | 12 | | /// <see cref="QueueClient.RegisterMessageHandler(Func{Message, CancellationToken, Task}, MessageHandlerOptions)" /> |
| | 13 | | /// <see cref="SubscriptionClient.RegisterMessageHandler(Func{Message, CancellationToken, Task}, MessageHandlerOptio |
| | 14 | | public sealed class MessageHandlerOptions |
| | 15 | | { |
| | 16 | | int maxConcurrentCalls; |
| | 17 | | TimeSpan maxAutoRenewDuration; |
| | 18 | |
|
| | 19 | | /// <summary>Initializes a new instance of the <see cref="MessageHandlerOptions" /> class. |
| | 20 | | /// Default Values: |
| | 21 | | /// <see cref="MaxConcurrentCalls"/> = 1 |
| | 22 | | /// <see cref="AutoComplete"/> = true |
| | 23 | | /// <see cref="ReceiveTimeOut"/> = 1 minute |
| | 24 | | /// <see cref="MaxAutoRenewDuration"/> = 5 minutes |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="exceptionReceivedHandler">A <see cref="Func{T1, TResult}"/> that is invoked during exceptions. |
| | 27 | | /// <see cref="ExceptionReceivedEventArgs"/> contains contextual information regarding the exception.</param> |
| 0 | 28 | | public MessageHandlerOptions(Func<ExceptionReceivedEventArgs, Task> exceptionReceivedHandler) |
| | 29 | | { |
| 0 | 30 | | this.MaxConcurrentCalls = 1; |
| 0 | 31 | | this.AutoComplete = true; |
| 0 | 32 | | this.ReceiveTimeOut = Constants.DefaultOperationTimeout; |
| 0 | 33 | | this.MaxAutoRenewDuration = Constants.ClientPumpRenewLockTimeout; |
| 0 | 34 | | this.ExceptionReceivedHandler = exceptionReceivedHandler ?? throw new ArgumentNullException(nameof(exception |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary>Occurs when an exception is received. Enables you to be notified of any errors encountered by the m |
| | 38 | | /// When errors are received calls will automatically be retried, so this is informational. </summary> |
| 0 | 39 | | public Func<ExceptionReceivedEventArgs, Task> ExceptionReceivedHandler { get; } |
| | 40 | |
|
| | 41 | | /// <summary>Gets or sets the maximum number of concurrent calls to the callback the message pump should initiat |
| | 42 | | /// <value>The maximum number of concurrent calls to the callback.</value> |
| | 43 | | public int MaxConcurrentCalls |
| | 44 | | { |
| 0 | 45 | | get => this.maxConcurrentCalls; |
| | 46 | |
|
| | 47 | | set |
| | 48 | | { |
| 0 | 49 | | if (value <= 0) |
| | 50 | | { |
| 0 | 51 | | throw new ArgumentOutOfRangeException(Resources.MaxConcurrentCallsMustBeGreaterThanZero.FormatForUse |
| | 52 | | } |
| | 53 | |
|
| 0 | 54 | | this.maxConcurrentCalls = value; |
| 0 | 55 | | } |
| | 56 | | } |
| | 57 | |
|
| | 58 | | /// <summary>Gets or sets a value that indicates whether the message-pump should call |
| | 59 | | /// <see cref="QueueClient.CompleteAsync(string)" /> or |
| | 60 | | /// <see cref="SubscriptionClient.CompleteAsync(string)" /> on messages after the callback has completed process |
| | 61 | | /// <value>true to complete the message processing automatically on successful execution of the operation; other |
| 0 | 62 | | public bool AutoComplete { get; set; } |
| | 63 | |
|
| | 64 | | /// <summary>Gets or sets the maximum duration within which the lock will be renewed automatically. This |
| | 65 | | /// value should be greater than the longest message lock duration; for example, the LockDuration Property. </su |
| | 66 | | /// <value>The maximum duration during which locks are automatically renewed.</value> |
| | 67 | | /// <remarks>The message renew can continue for sometime in the background |
| | 68 | | /// after completion of message and result in a few false MessageLockLostExceptions temporarily.</remarks> |
| | 69 | | public TimeSpan MaxAutoRenewDuration |
| | 70 | | { |
| 0 | 71 | | get => this.maxAutoRenewDuration; |
| | 72 | |
|
| | 73 | | set |
| | 74 | | { |
| 0 | 75 | | TimeoutHelper.ThrowIfNegativeArgument(value, nameof(value)); |
| 0 | 76 | | this.maxAutoRenewDuration = value; |
| 0 | 77 | | } |
| | 78 | | } |
| | 79 | |
|
| 0 | 80 | | internal bool AutoRenewLock => this.MaxAutoRenewDuration > TimeSpan.Zero; |
| | 81 | |
|
| 0 | 82 | | internal TimeSpan ReceiveTimeOut { get; } |
| | 83 | |
|
| | 84 | | internal async Task RaiseExceptionReceived(ExceptionReceivedEventArgs eventArgs) |
| | 85 | | { |
| | 86 | | try |
| | 87 | | { |
| 0 | 88 | | await this.ExceptionReceivedHandler(eventArgs).ConfigureAwait(false); |
| 0 | 89 | | } |
| 0 | 90 | | catch (Exception exception) |
| | 91 | | { |
| 0 | 92 | | MessagingEventSource.Log.ExceptionReceivedHandlerThrewException(exception); |
| 0 | 93 | | } |
| 0 | 94 | | } |
| | 95 | | } |
| | 96 | | } |