| | 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.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Azure.Core; |
| | 10 | | using Azure.Messaging.ServiceBus.Core; |
| | 11 | | using Azure.Messaging.ServiceBus.Diagnostics; |
| | 12 | | using Azure.Messaging.ServiceBus.Management; |
| | 13 | |
|
| | 14 | | namespace Azure.Messaging.ServiceBus |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Manages rules for subscriptions. |
| | 18 | | /// </summary> |
| | 19 | | internal class ServiceBusRuleManager : IAsyncDisposable |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// The path of the Service Bus subscription that the rule manager is connected to, specific to the |
| | 23 | | /// Service Bus namespace that contains it. |
| | 24 | | /// </summary> |
| 0 | 25 | | public string SubscriptionPath { get; private set; } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Gets the ID to identify this client. This can be used to correlate logs and exceptions. |
| | 29 | | /// </summary> |
| | 30 | | /// <remarks>Every new client has a unique ID.</remarks> |
| 0 | 31 | | internal string Identifier { get; private set; } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Indicates whether or not this <see cref="ServiceBusRuleManager"/> has been disposed. |
| | 35 | | /// </summary> |
| | 36 | | /// |
| | 37 | | /// <value> |
| | 38 | | /// <c>true</c> if the rule manager is disposed; otherwise, <c>false</c>. |
| | 39 | | /// </value> |
| 0 | 40 | | public bool IsDisposed { get; private set; } = false; |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// The active connection to the Azure Service Bus service, enabling client communications for metadata |
| | 44 | | /// about the associated Service Bus entity and access to transport-aware rule manager. |
| | 45 | | /// </summary> |
| | 46 | | /// |
| | 47 | | private readonly ServiceBusConnection _connection; |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// An abstracted Service Bus transport-specific rule manager that is associated with the |
| | 51 | | /// Service Bus entity gateway; intended to perform delegated operations. |
| | 52 | | /// </summary> |
| | 53 | | internal readonly TransportRuleManager InnerRuleManager; |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Initializes a new instance of the <see cref="ServiceBusRuleManager"/> class. |
| | 57 | | /// </summary> |
| | 58 | | /// |
| | 59 | | /// <param name="connection">The <see cref="ServiceBusConnection" /> connection to use for communication with th |
| | 60 | | /// <param name="subscriptionPath">The path of the Service Bus subscription to which the rule manager is bound.< |
| | 61 | | /// |
| 0 | 62 | | internal ServiceBusRuleManager( |
| 0 | 63 | | ServiceBusConnection connection, |
| 0 | 64 | | string subscriptionPath) |
| | 65 | | { |
| 0 | 66 | | Argument.AssertNotNull(connection, nameof(connection)); |
| 0 | 67 | | Argument.AssertNotNull(connection.RetryOptions, nameof(connection.RetryOptions)); |
| 0 | 68 | | Argument.AssertNotNullOrWhiteSpace(subscriptionPath, nameof(subscriptionPath)); |
| 0 | 69 | | connection.ThrowIfClosed(); |
| | 70 | |
|
| 0 | 71 | | Identifier = DiagnosticUtilities.GenerateIdentifier(subscriptionPath); |
| 0 | 72 | | _connection = connection; |
| 0 | 73 | | SubscriptionPath = subscriptionPath; |
| 0 | 74 | | InnerRuleManager = _connection.CreateTransportRuleManager( |
| 0 | 75 | | subscriptionPath: SubscriptionPath, |
| 0 | 76 | | retryPolicy: connection.RetryOptions.ToRetryPolicy(), |
| 0 | 77 | | identifier: Identifier); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Adds a rule to the current subscription to filter the messages reaching from topic to the subscription. |
| | 82 | | /// </summary> |
| | 83 | | /// |
| | 84 | | /// <param name="ruleName">Name of the rule</param> |
| | 85 | | /// <param name="filter">The filter expression against which messages will be matched.</param> |
| | 86 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 87 | | /// |
| | 88 | | /// <remarks> |
| | 89 | | /// You can add rules to the subscription that decides which messages from the topic should reach the subscripti |
| | 90 | | /// A default <see cref="TrueRuleFilter"/> rule named <see cref="RuleProperties.DefaultRuleName"/> is always add |
| | 91 | | /// You can add multiple rules with distinct names to the same subscription. |
| | 92 | | /// Multiple filters combine with each other using logical OR condition. i.e., If any filter succeeds, the messa |
| | 93 | | /// </remarks> |
| | 94 | | /// |
| | 95 | | /// <returns>A task instance that represents the asynchronous add rule operation.</returns> |
| | 96 | | public virtual async Task AddRuleAsync( |
| | 97 | | string ruleName, |
| | 98 | | RuleFilter filter, |
| | 99 | | CancellationToken cancellationToken = default) |
| | 100 | | { |
| 0 | 101 | | await AddRuleAsync(new RuleProperties(name: ruleName, filter: filter), cancellationToken).ConfigureAwait(fal |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// Adds a rule to the current subscription to filter the messages reaching from topic to the subscription. |
| | 106 | | /// </summary> |
| | 107 | | /// |
| | 108 | | /// <param name="description">The rule description that provides the rule to add.</param> |
| | 109 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 110 | | /// |
| | 111 | | /// <remarks> |
| | 112 | | /// You can add rules to the subscription that decides which messages from the topic should reach the subscripti |
| | 113 | | /// A default <see cref="TrueRuleFilter"/> rule named <see cref="RuleProperties.DefaultRuleName"/> is always add |
| | 114 | | /// You can add multiple rules with distinct names to the same subscription. |
| | 115 | | /// Multiple filters combine with each other using logical OR condition. i.e., If any filter succeeds, the messa |
| | 116 | | /// </remarks> |
| | 117 | | /// |
| | 118 | | /// <returns>A task instance that represents the asynchronous add rule operation.</returns> |
| | 119 | | public virtual async Task AddRuleAsync( |
| | 120 | | RuleProperties description, |
| | 121 | | CancellationToken cancellationToken = default) |
| | 122 | | { |
| 0 | 123 | | Argument.AssertNotDisposed(IsDisposed, nameof(ServiceBusRuleManager)); |
| 0 | 124 | | Argument.AssertNotNull(description, nameof(description)); |
| 0 | 125 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| 0 | 126 | | EntityNameFormatter.CheckValidRuleName(description.Name); |
| 0 | 127 | | ServiceBusEventSource.Log.AddRuleStart(Identifier, description.Name); |
| | 128 | |
|
| | 129 | | try |
| | 130 | | { |
| 0 | 131 | | await InnerRuleManager.AddRuleAsync( |
| 0 | 132 | | description, |
| 0 | 133 | | cancellationToken).ConfigureAwait(false); |
| 0 | 134 | | } |
| 0 | 135 | | catch (Exception exception) |
| | 136 | | { |
| 0 | 137 | | ServiceBusEventSource.Log.AddRuleException(Identifier, exception.ToString()); |
| 0 | 138 | | throw; |
| | 139 | | } |
| | 140 | |
|
| 0 | 141 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| 0 | 142 | | ServiceBusEventSource.Log.AddRuleComplete(Identifier); |
| 0 | 143 | | } |
| | 144 | |
|
| | 145 | | /// <summary> |
| | 146 | | /// Removes the rule on the subscription identified by <paramref name="ruleName" />. |
| | 147 | | /// </summary> |
| | 148 | | /// |
| | 149 | | /// <param name="ruleName">Name of the rule</param> |
| | 150 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 151 | | /// |
| | 152 | | /// <returns>A task instance that represents the asynchronous remove rule operation.</returns> |
| | 153 | | public virtual async Task RemoveRuleAsync( |
| | 154 | | string ruleName, |
| | 155 | | CancellationToken cancellationToken = default) |
| | 156 | | { |
| 0 | 157 | | Argument.AssertNotDisposed(IsDisposed, nameof(ServiceBusRuleManager)); |
| 0 | 158 | | Argument.AssertNotNullOrEmpty(ruleName, nameof(ruleName)); |
| 0 | 159 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| 0 | 160 | | ServiceBusEventSource.Log.RemoveRuleStart(Identifier, ruleName); |
| | 161 | |
|
| | 162 | | try |
| | 163 | | { |
| 0 | 164 | | await InnerRuleManager.RemoveRuleAsync( |
| 0 | 165 | | ruleName, |
| 0 | 166 | | cancellationToken).ConfigureAwait(false); |
| 0 | 167 | | } |
| 0 | 168 | | catch (Exception exception) |
| | 169 | | { |
| 0 | 170 | | ServiceBusEventSource.Log.RemoveRuleException(Identifier, exception.ToString()); |
| 0 | 171 | | throw; |
| | 172 | | } |
| | 173 | |
|
| 0 | 174 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| 0 | 175 | | ServiceBusEventSource.Log.RemoveRuleComplete(Identifier); |
| 0 | 176 | | } |
| | 177 | |
|
| | 178 | | /// <summary> |
| | 179 | | /// Get all rules associated with the subscription. |
| | 180 | | /// </summary> |
| | 181 | | /// |
| | 182 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 183 | | /// |
| | 184 | | /// <returns>Returns a list of rules description</returns> |
| | 185 | | public virtual async Task<IList<RuleProperties>> GetRulesAsync(CancellationToken cancellationToken = default) |
| | 186 | | { |
| | 187 | |
|
| 0 | 188 | | Argument.AssertNotDisposed(IsDisposed, nameof(ServiceBusRuleManager)); |
| 0 | 189 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| 0 | 190 | | ServiceBusEventSource.Log.GetRuleStart(Identifier); |
| | 191 | | IList<RuleProperties> rulesDescription; |
| | 192 | |
|
| | 193 | | try |
| | 194 | | { |
| 0 | 195 | | rulesDescription = await InnerRuleManager.GetRulesAsync(cancellationToken).ConfigureAwait(false); |
| 0 | 196 | | } |
| 0 | 197 | | catch (Exception exception) |
| | 198 | | { |
| 0 | 199 | | ServiceBusEventSource.Log.GetRuleException(Identifier, exception.ToString()); |
| 0 | 200 | | throw; |
| | 201 | | } |
| | 202 | |
|
| 0 | 203 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| 0 | 204 | | ServiceBusEventSource.Log.GetRuleComplete(Identifier); |
| 0 | 205 | | return rulesDescription; |
| 0 | 206 | | } |
| | 207 | |
|
| | 208 | | /// <summary> |
| | 209 | | /// Performs the task needed to clean up resources used by the <see cref="ServiceBusRuleManager" />. |
| | 210 | | /// </summary> |
| | 211 | | /// |
| | 212 | | /// <returns>A task to be resolved on when the operation has completed.</returns> |
| | 213 | | public virtual async ValueTask DisposeAsync() |
| | 214 | | { |
| 0 | 215 | | IsDisposed = true; |
| | 216 | |
|
| 0 | 217 | | ServiceBusEventSource.Log.ClientDisposeStart(typeof(ServiceBusRuleManager), Identifier); |
| | 218 | | try |
| | 219 | | { |
| 0 | 220 | | await InnerRuleManager.CloseAsync(CancellationToken.None).ConfigureAwait(false); |
| 0 | 221 | | } |
| 0 | 222 | | catch (Exception ex) |
| | 223 | | { |
| 0 | 224 | | ServiceBusEventSource.Log.ClientDisposeException(typeof(ServiceBusRuleManager), Identifier, ex); |
| 0 | 225 | | throw; |
| | 226 | | } |
| | 227 | |
|
| 0 | 228 | | ServiceBusEventSource.Log.ClientDisposeComplete(typeof(ServiceBusRuleManager), Identifier); |
| 0 | 229 | | } |
| | 230 | |
|
| | 231 | | /// <summary> |
| | 232 | | /// Determines whether the specified <see cref="System.Object" /> is equal to this instance. |
| | 233 | | /// </summary> |
| | 234 | | /// |
| | 235 | | /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param> |
| | 236 | | /// |
| | 237 | | /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c> |
| | 238 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 239 | | public override bool Equals(object obj) => base.Equals(obj); |
| | 240 | |
|
| | 241 | | /// <summary> |
| | 242 | | /// Returns a hash code for this instance. |
| | 243 | | /// </summary> |
| | 244 | | /// |
| | 245 | | /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a ha |
| | 246 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 247 | | public override int GetHashCode() => base.GetHashCode(); |
| | 248 | |
|
| | 249 | | /// <summary> |
| | 250 | | /// Converts the instance to string representation. |
| | 251 | | /// </summary> |
| | 252 | | /// |
| | 253 | | /// <returns>A <see cref="System.String" /> that represents this instance.</returns> |
| | 254 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 255 | | public override string ToString() => base.ToString(); |
| | 256 | | } |
| | 257 | | } |