| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using Azure.Core; |
| | 6 | |
|
| | 7 | | namespace Azure.Messaging.ServiceBus.Management |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Represents the set of options that can be specified for the creation of a queue. |
| | 11 | | /// </summary> |
| | 12 | | public class CreateQueueOptions : IEquatable<CreateQueueOptions> |
| | 13 | | { |
| 36 | 14 | | private TimeSpan _duplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(1); |
| | 15 | | private string _name; |
| 36 | 16 | | private TimeSpan _lockDuration = TimeSpan.FromSeconds(60); |
| 36 | 17 | | private TimeSpan _defaultMessageTimeToLive = TimeSpan.MaxValue; |
| 36 | 18 | | private TimeSpan autoDeleteOnIdle = TimeSpan.MaxValue; |
| 36 | 19 | | private int _maxDeliveryCount = 10; |
| | 20 | | private string _forwardTo = null; |
| | 21 | | private string _forwardDeadLetteredMessagesTo = null; |
| | 22 | | private string _userMetadata = null; |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Initializes a new instance of <see cref="CreateQueueOptions"/> with the specified relative name. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="name">Name of the queue relative to the namespace base address.</param> |
| 28 | 28 | | public CreateQueueOptions(string name) |
| | 29 | | { |
| 28 | 30 | | Name = name; |
| 28 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Initializes a new instance of <see cref="CreateQueueOptions"/> based on the |
| | 35 | | /// specified <see cref="QueueProperties"/> instance. This is useful for creating a new queue based |
| | 36 | | /// on the properties of an existing queue. |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="queue">Existing queue to create options from.</param> |
| 8 | 39 | | public CreateQueueOptions(QueueProperties queue) |
| | 40 | | { |
| 8 | 41 | | Argument.AssertNotNull(queue, nameof(queue)); |
| 8 | 42 | | Name = queue.Name; |
| 8 | 43 | | LockDuration = queue.LockDuration; |
| 8 | 44 | | MaxSizeInMegabytes = queue.MaxSizeInMegabytes; |
| 8 | 45 | | RequiresDuplicateDetection = queue.RequiresDuplicateDetection; |
| 8 | 46 | | RequiresSession = queue.RequiresSession; |
| 8 | 47 | | DefaultMessageTimeToLive = queue.DefaultMessageTimeToLive; |
| 8 | 48 | | AutoDeleteOnIdle = queue.AutoDeleteOnIdle; |
| 8 | 49 | | DeadLetteringOnMessageExpiration = queue.DeadLetteringOnMessageExpiration; |
| 8 | 50 | | DuplicateDetectionHistoryTimeWindow = queue.DuplicateDetectionHistoryTimeWindow; |
| 8 | 51 | | MaxDeliveryCount = queue.MaxDeliveryCount; |
| 8 | 52 | | EnableBatchedOperations = queue.EnableBatchedOperations; |
| 8 | 53 | | AuthorizationRules = queue.AuthorizationRules.Clone(); |
| 8 | 54 | | Status = queue.Status; |
| 8 | 55 | | ForwardTo = queue.ForwardTo; |
| 8 | 56 | | ForwardDeadLetteredMessagesTo = queue.ForwardDeadLetteredMessagesTo; |
| 8 | 57 | | EnablePartitioning = queue.EnablePartitioning; |
| 8 | 58 | | if (queue.UserMetadata != null) |
| | 59 | | { |
| 4 | 60 | | UserMetadata = queue.UserMetadata; |
| | 61 | | } |
| 8 | 62 | | } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Name of the queue relative to the namespace base address. |
| | 66 | | /// </summary> |
| | 67 | | /// <remarks>Max length is 260 chars. Cannot start or end with a slash. |
| | 68 | | /// Cannot have restricted characters: '@','?','#','*'</remarks> |
| | 69 | | public string Name |
| | 70 | | { |
| 48 | 71 | | get => _name; |
| | 72 | | set |
| | 73 | | { |
| 44 | 74 | | EntityNameFormatter.CheckValidQueueName(value, nameof(Name)); |
| 40 | 75 | | _name = value; |
| 40 | 76 | | } |
| | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Duration of a peek lock receive. i.e., the amount of time that the message is locked by a given receiver so |
| | 81 | | /// no other receiver receives the same message. |
| | 82 | | /// </summary> |
| | 83 | | /// <remarks>Max value is 5 minutes. Default value is 60 seconds.</remarks> |
| | 84 | | public TimeSpan LockDuration |
| | 85 | | { |
| 36 | 86 | | get => _lockDuration; |
| | 87 | | set |
| | 88 | | { |
| 12 | 89 | | Argument.AssertPositive(value, nameof(LockDuration)); |
| 12 | 90 | | _lockDuration = value; |
| 12 | 91 | | } |
| | 92 | | } |
| | 93 | |
|
| | 94 | | /// <summary> |
| | 95 | | /// The maximum size of the queue in megabytes, which is the size of memory allocated for the queue. |
| | 96 | | /// </summary> |
| | 97 | | /// <remarks>Default value is 1024.</remarks> |
| 84 | 98 | | public long MaxSizeInMegabytes { get; set; } = 1024; |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// This value indicates if the queue requires guard against duplicate messages. If true, duplicate messages hav |
| | 102 | | /// <see cref="ServiceBusMessage.MessageId"/> and sent to queue within duration of <see cref="DuplicateDetection |
| | 103 | | /// will be discarded. |
| | 104 | | /// </summary> |
| | 105 | | /// <remarks>Defaults to false.</remarks> |
| 56 | 106 | | public bool RequiresDuplicateDetection { get; set; } = false; |
| | 107 | |
|
| | 108 | | /// <summary> |
| | 109 | | /// This indicates whether the queue supports the concept of session. Sessionful-messages follow FIFO ordering. |
| | 110 | | /// </summary> |
| | 111 | | /// <remarks> |
| | 112 | | /// If true, the receiver can only receive messages using <see cref="ServiceBusSessionReceiver"/>. |
| | 113 | | /// Defaults to false. |
| | 114 | | /// </remarks> |
| 48 | 115 | | public bool RequiresSession { get; set; } = false; |
| | 116 | |
|
| | 117 | | /// <summary> |
| | 118 | | /// The default time to live value for the messages. This is the duration after which the message expires, start |
| | 119 | | /// the message is sent to Service Bus. </summary> |
| | 120 | | /// <remarks> |
| | 121 | | /// This is the default value used when <see cref="ServiceBusMessage.TimeToLive"/> is not set on a |
| | 122 | | /// message itself. Messages older than their TimeToLive value will expire and no longer be retained in the mes |
| | 123 | | /// Subscribers will be unable to receive expired messages. |
| | 124 | | /// Default value is <see cref="TimeSpan.MaxValue"/>. |
| | 125 | | /// </remarks> |
| | 126 | | public TimeSpan DefaultMessageTimeToLive |
| | 127 | | { |
| 36 | 128 | | get => _defaultMessageTimeToLive; |
| | 129 | | set |
| | 130 | | { |
| 12 | 131 | | Argument.AssertInRange( |
| 12 | 132 | | value, |
| 12 | 133 | | ManagementClientConstants.MinimumAllowedTimeToLive, |
| 12 | 134 | | ManagementClientConstants.MaximumAllowedTimeToLive, |
| 12 | 135 | | nameof(DefaultMessageTimeToLive)); |
| | 136 | |
|
| 12 | 137 | | _defaultMessageTimeToLive = value; |
| 12 | 138 | | } |
| | 139 | | } |
| | 140 | |
|
| | 141 | | /// <summary> |
| | 142 | | /// The <see cref="TimeSpan"/> idle interval after which the queue is automatically deleted. |
| | 143 | | /// </summary> |
| | 144 | | /// <remarks>The minimum duration is 5 minutes. Default value is <see cref="TimeSpan.MaxValue"/>.</remarks> |
| | 145 | | public TimeSpan AutoDeleteOnIdle |
| | 146 | | { |
| 36 | 147 | | get => autoDeleteOnIdle; |
| | 148 | | set |
| | 149 | | { |
| 12 | 150 | | Argument.AssertAtLeast( |
| 12 | 151 | | value, |
| 12 | 152 | | ManagementClientConstants.MinimumAllowedAutoDeleteOnIdle, |
| 12 | 153 | | nameof(AutoDeleteOnIdle)); |
| | 154 | |
|
| 12 | 155 | | autoDeleteOnIdle = value; |
| 12 | 156 | | } |
| | 157 | | } |
| | 158 | |
|
| | 159 | | /// <summary> |
| | 160 | | /// Indicates whether this queue has dead letter support when a message expires. |
| | 161 | | /// </summary> |
| | 162 | | /// <remarks>If true, the expired messages are moved to dead-letter sub-queue. Default value is false.</remarks> |
| 48 | 163 | | public bool DeadLetteringOnMessageExpiration { get; set; } = false; |
| | 164 | |
|
| | 165 | | /// <summary> |
| | 166 | | /// The <see cref="TimeSpan"/> duration of duplicate detection history that is maintained by the service. |
| | 167 | | /// </summary> |
| | 168 | | /// <remarks> |
| | 169 | | /// The default value is 1 minute. Max value is 7 days and minimum is 20 seconds. |
| | 170 | | /// </remarks> |
| | 171 | | public TimeSpan DuplicateDetectionHistoryTimeWindow |
| | 172 | | { |
| 28 | 173 | | get => _duplicateDetectionHistoryTimeWindow; |
| | 174 | | set |
| | 175 | | { |
| 12 | 176 | | Argument.AssertInRange( |
| 12 | 177 | | value, |
| 12 | 178 | | ManagementClientConstants.MinimumDuplicateDetectionHistoryTimeWindow, |
| 12 | 179 | | ManagementClientConstants.MaximumDuplicateDetectionHistoryTimeWindow, |
| 12 | 180 | | nameof(DuplicateDetectionHistoryTimeWindow)); |
| | 181 | |
|
| 12 | 182 | | _duplicateDetectionHistoryTimeWindow = value; |
| 12 | 183 | | } |
| | 184 | | } |
| | 185 | |
|
| | 186 | | /// <summary> |
| | 187 | | /// The maximum delivery count of a message before it is dead-lettered. |
| | 188 | | /// </summary> |
| | 189 | | /// <remarks>The delivery count is increased when a message is received in <see cref="ReceiveMode.PeekLock"/> mo |
| | 190 | | /// and didn't complete the message before the message lock expired. |
| | 191 | | /// Default value is 10. Minimum value is 1.</remarks> |
| | 192 | | public int MaxDeliveryCount |
| | 193 | | { |
| 36 | 194 | | get => _maxDeliveryCount; |
| | 195 | | set |
| | 196 | | { |
| 12 | 197 | | Argument.AssertAtLeast( |
| 12 | 198 | | value, |
| 12 | 199 | | ManagementClientConstants.MinAllowedMaxDeliveryCount, |
| 12 | 200 | | nameof(MaxDeliveryCount)); |
| | 201 | |
|
| 12 | 202 | | _maxDeliveryCount = value; |
| 12 | 203 | | } |
| | 204 | | } |
| | 205 | |
|
| | 206 | | /// <summary> |
| | 207 | | /// Indicates whether server-side batched operations are enabled. |
| | 208 | | /// </summary> |
| | 209 | | /// <remarks>Defaults to true.</remarks> |
| 104 | 210 | | public bool EnableBatchedOperations { get; set; } = true; |
| | 211 | |
|
| | 212 | | /// <summary> |
| | 213 | | /// The <see cref="AuthorizationRules"/> on the queue to control user access at entity level. |
| | 214 | | /// </summary> |
| 116 | 215 | | public AuthorizationRules AuthorizationRules { get; internal set; } = new AuthorizationRules(); |
| | 216 | |
|
| | 217 | | /// <summary> |
| | 218 | | /// The current status of the queue (Enabled / Disabled). |
| | 219 | | /// </summary> |
| | 220 | | /// <remarks>When an entity is disabled, that entity cannot send or receive messages.</remarks> |
| 84 | 221 | | public EntityStatus Status { get; set; } = EntityStatus.Active; |
| | 222 | |
|
| | 223 | | /// <summary> |
| | 224 | | /// The name of the recipient entity to which all the messages sent to the queue are forwarded to. |
| | 225 | | /// </summary> |
| | 226 | | /// <remarks>If set, user cannot manually receive messages from this queue. The destination entity |
| | 227 | | /// must be an already existing entity.</remarks> |
| | 228 | | public string ForwardTo |
| | 229 | | { |
| 36 | 230 | | get => _forwardTo; |
| | 231 | | set |
| | 232 | | { |
| 12 | 233 | | if (string.IsNullOrWhiteSpace(value)) |
| | 234 | | { |
| 12 | 235 | | _forwardTo = value; |
| 12 | 236 | | return; |
| | 237 | | } |
| | 238 | |
|
| 0 | 239 | | EntityNameFormatter.CheckValidQueueName(value, nameof(ForwardTo)); |
| 0 | 240 | | if (_name.Equals(value, StringComparison.CurrentCultureIgnoreCase)) |
| | 241 | |
|
| | 242 | | { |
| 0 | 243 | | throw new InvalidOperationException("Entity cannot have auto-forwarding policy to itself"); |
| | 244 | | } |
| | 245 | |
|
| 0 | 246 | | _forwardTo = value; |
| 0 | 247 | | } |
| | 248 | | } |
| | 249 | |
|
| | 250 | | /// <summary> |
| | 251 | | /// The name of the recipient entity to which all the dead-lettered messages of this queue are forwarded to. |
| | 252 | | /// </summary> |
| | 253 | | /// <remarks>If set, user cannot manually receive dead-lettered messages from this queue. The destination |
| | 254 | | /// entity must already exist.</remarks> |
| | 255 | | public string ForwardDeadLetteredMessagesTo |
| | 256 | | { |
| 36 | 257 | | get => _forwardDeadLetteredMessagesTo; |
| | 258 | | set |
| | 259 | | { |
| 12 | 260 | | if (string.IsNullOrWhiteSpace(value)) |
| | 261 | | { |
| 12 | 262 | | _forwardDeadLetteredMessagesTo = value; |
| 12 | 263 | | return; |
| | 264 | | } |
| | 265 | |
|
| 0 | 266 | | EntityNameFormatter.CheckValidQueueName(value, nameof(ForwardDeadLetteredMessagesTo)); |
| 0 | 267 | | if (_name.Equals(value, StringComparison.CurrentCultureIgnoreCase)) |
| | 268 | | { |
| 0 | 269 | | throw new InvalidOperationException("Entity cannot have auto-forwarding policy to itself"); |
| | 270 | | } |
| | 271 | |
|
| 0 | 272 | | _forwardDeadLetteredMessagesTo = value; |
| 0 | 273 | | } |
| | 274 | | } |
| | 275 | |
|
| | 276 | | /// <summary> |
| | 277 | | /// Indicates whether the queue is to be partitioned across multiple message brokers. |
| | 278 | | /// </summary> |
| | 279 | | /// <remarks>Defaults to false.</remarks> |
| 28 | 280 | | public bool EnablePartitioning { get; set; } = false; |
| | 281 | |
|
| | 282 | | /// <summary> |
| | 283 | | /// Custom metdata that user can associate with the queue. |
| | 284 | | /// </summary> |
| | 285 | | /// <remarks>Cannot be null. Max length is 1024 chars.</remarks> |
| | 286 | | public string UserMetadata |
| | 287 | | { |
| 24 | 288 | | get => _userMetadata; |
| | 289 | | set |
| | 290 | | { |
| 8 | 291 | | Argument.AssertNotNull(value, nameof(UserMetadata)); |
| 8 | 292 | | Argument.AssertNotTooLong( |
| 8 | 293 | | value, |
| 8 | 294 | | ManagementClientConstants.MaxUserMetadataLength, |
| 8 | 295 | | nameof(UserMetadata)); |
| | 296 | |
|
| 8 | 297 | | _userMetadata = value; |
| 8 | 298 | | } |
| | 299 | | } |
| | 300 | |
|
| | 301 | | /// <summary> |
| | 302 | | /// Returns a hash code for this instance. |
| | 303 | | /// </summary> |
| | 304 | | public override int GetHashCode() |
| | 305 | | { |
| 0 | 306 | | return Name?.GetHashCode() ?? base.GetHashCode(); |
| | 307 | | } |
| | 308 | |
|
| | 309 | | /// <summary>Determines whether the specified object is equal to the current object.</summary> |
| | 310 | | public override bool Equals(object obj) |
| | 311 | | { |
| 0 | 312 | | var other = obj as CreateQueueOptions; |
| 0 | 313 | | return Equals(other); |
| | 314 | | } |
| | 315 | |
|
| | 316 | | /// <summary>Determines whether the specified object is equal to the current object.</summary> |
| | 317 | | public bool Equals(CreateQueueOptions other) |
| | 318 | | { |
| 8 | 319 | | if (other is CreateQueueOptions otherOptions |
| 8 | 320 | | && Name.Equals(otherOptions.Name, StringComparison.OrdinalIgnoreCase) |
| 8 | 321 | | && AutoDeleteOnIdle.Equals(otherOptions.AutoDeleteOnIdle) |
| 8 | 322 | | && DefaultMessageTimeToLive.Equals(otherOptions.DefaultMessageTimeToLive) |
| 8 | 323 | | && (!RequiresDuplicateDetection || DuplicateDetectionHistoryTimeWindow.Equals(otherOptions.DuplicateDete |
| 8 | 324 | | && EnableBatchedOperations == otherOptions.EnableBatchedOperations |
| 8 | 325 | | && DeadLetteringOnMessageExpiration == otherOptions.DeadLetteringOnMessageExpiration |
| 8 | 326 | | && EnablePartitioning == otherOptions.EnablePartitioning |
| 8 | 327 | | && string.Equals(ForwardDeadLetteredMessagesTo, otherOptions.ForwardDeadLetteredMessagesTo, StringCompar |
| 8 | 328 | | && string.Equals(ForwardTo, otherOptions.ForwardTo, StringComparison.OrdinalIgnoreCase) |
| 8 | 329 | | && LockDuration.Equals(otherOptions.LockDuration) |
| 8 | 330 | | && MaxDeliveryCount == otherOptions.MaxDeliveryCount |
| 8 | 331 | | && MaxSizeInMegabytes == otherOptions.MaxSizeInMegabytes |
| 8 | 332 | | && RequiresDuplicateDetection.Equals(otherOptions.RequiresDuplicateDetection) |
| 8 | 333 | | && RequiresSession.Equals(otherOptions.RequiresSession) |
| 8 | 334 | | && Status.Equals(otherOptions.Status) |
| 8 | 335 | | && string.Equals(_userMetadata, otherOptions._userMetadata, StringComparison.OrdinalIgnoreCase) |
| 8 | 336 | | && (AuthorizationRules != null && otherOptions.AuthorizationRules != null |
| 8 | 337 | | || AuthorizationRules == null && otherOptions.AuthorizationRules == null) |
| 8 | 338 | | && (AuthorizationRules == null || AuthorizationRules.Equals(otherOptions.AuthorizationRules))) |
| | 339 | | { |
| 8 | 340 | | return true; |
| | 341 | | } |
| | 342 | |
|
| 0 | 343 | | return false; |
| | 344 | | } |
| | 345 | |
|
| | 346 | | /// <summary></summary> |
| | 347 | | public static bool operator ==(CreateQueueOptions left, CreateQueueOptions right) |
| | 348 | | { |
| 0 | 349 | | if (ReferenceEquals(left, right)) |
| | 350 | | { |
| 0 | 351 | | return true; |
| | 352 | | } |
| | 353 | |
|
| 0 | 354 | | if (left is null || right is null) |
| | 355 | | { |
| 0 | 356 | | return false; |
| | 357 | | } |
| | 358 | |
|
| 0 | 359 | | return left.Equals(right); |
| | 360 | | } |
| | 361 | |
|
| | 362 | | /// <summary></summary> |
| | 363 | | public static bool operator !=(CreateQueueOptions left, CreateQueueOptions right) |
| | 364 | | { |
| 0 | 365 | | return !(left == right); |
| | 366 | | } |
| | 367 | | } |
| | 368 | | } |