| | 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.Globalization; |
| | 7 | | using System.Linq; |
| | 8 | | using Azure.Core; |
| | 9 | | using Azure.Messaging.ServiceBus.Core; |
| | 10 | | using Microsoft.Azure.Amqp; |
| | 11 | |
|
| | 12 | | namespace Azure.Messaging.ServiceBus.Amqp |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// A set of messages with known size constraints, based on messages to be sent |
| | 16 | | /// using an AMQP-based transport. |
| | 17 | | /// </summary> |
| | 18 | | /// |
| | 19 | | internal class AmqpMessageBatch : TransportMessageBatch |
| | 20 | | { |
| | 21 | | /// <summary>The amount of bytes to reserve as overhead for a small message.</summary> |
| | 22 | | private const byte OverheadBytesSmallMessage = 5; |
| | 23 | |
|
| | 24 | | /// <summary>The amount of bytes to reserve as overhead for a large message.</summary> |
| | 25 | | private const byte OverheadBytesLargeMessage = 8; |
| | 26 | |
|
| | 27 | | /// <summary>The maximum number of bytes that a message may be to be considered small.</summary> |
| | 28 | | private const byte MaximumBytesSmallMessage = 255; |
| | 29 | |
|
| | 30 | | /// <summary>The size of the batch, in bytes, to reserve for the AMQP message overhead.</summary> |
| | 31 | | private readonly long _reservedSize; |
| | 32 | |
|
| | 33 | | /// <summary>A flag that indicates whether or not the instance has been disposed.</summary> |
| | 34 | | private bool _disposed = false; |
| | 35 | |
|
| | 36 | | /// <summary>The size of the batch, in bytes, as it will be sent via the AMQP transport.</summary> |
| | 37 | | private long _sizeBytes = 0; |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// The maximum size allowed for the batch, in bytes. This includes the messages in the batch as |
| | 41 | | /// well as any overhead for the batch itself when sent to the Queue/Topic. |
| | 42 | | /// </summary> |
| | 43 | | /// |
| 72 | 44 | | public override long MaxSizeInBytes { get; } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// The size of the batch, in bytes, as it will be sent to the Queue/Topic |
| | 48 | | /// service. |
| | 49 | | /// </summary> |
| | 50 | | /// |
| 8 | 51 | | public override long SizeInBytes => _sizeBytes; |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// The count of messages contained in the batch. |
| | 55 | | /// </summary> |
| | 56 | | /// |
| 12 | 57 | | public override int Count => BatchMessages.Count; |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// The set of options to apply to the batch. |
| | 61 | | /// </summary> |
| | 62 | | /// |
| 0 | 63 | | private CreateMessageBatchOptions Options { get; } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// The set of messages that have been added to the batch. |
| | 67 | | /// </summary> |
| | 68 | | /// |
| 120 | 69 | | private List<ServiceBusMessage> BatchMessages { get; } = new List<ServiceBusMessage>(); |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Initializes a new instance of the <see cref="AmqpMessageBatch"/> class. |
| | 73 | | /// </summary> |
| | 74 | | /// |
| | 75 | | /// <param name="options">The set of options to apply to the batch.</param> |
| | 76 | | /// |
| 30 | 77 | | public AmqpMessageBatch(CreateMessageBatchOptions options) |
| | 78 | | { |
| 30 | 79 | | Argument.AssertNotNull(options, nameof(options)); |
| 28 | 80 | | Argument.AssertNotNull(options.MaxSizeInBytes, nameof(options.MaxSizeInBytes)); |
| | 81 | |
|
| 26 | 82 | | Options = options; |
| 26 | 83 | | MaxSizeInBytes = options.MaxSizeInBytes.Value; |
| | 84 | |
|
| | 85 | | // Initialize the size by reserving space for the batch envelope. |
| | 86 | |
|
| 26 | 87 | | using AmqpMessage envelope = AmqpMessageConverter.BatchSBMessagesAsAmqpMessage(Enumerable.Empty<ServiceBusMe |
| 26 | 88 | | _reservedSize = envelope.SerializedMessageSize; |
| 26 | 89 | | _sizeBytes = _reservedSize; |
| 52 | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Attempts to add a message to the batch, ensuring that the size |
| | 94 | | /// of the batch does not exceed its maximum. |
| | 95 | | /// </summary> |
| | 96 | | /// |
| | 97 | | /// <param name="message">The message to attempt to add to the batch.</param> |
| | 98 | | /// |
| | 99 | | /// <returns><c>true</c> if the message was added; otherwise, <c>false</c>.</returns> |
| | 100 | | /// |
| | 101 | | public override bool TryAddMessage(ServiceBusMessage message) |
| | 102 | | { |
| 74 | 103 | | Argument.AssertNotNull(message, nameof(message)); |
| 72 | 104 | | Argument.AssertNotDisposed(_disposed, nameof(ServiceBusMessageBatch)); |
| | 105 | |
|
| 70 | 106 | | AmqpMessage amqpMessage = AmqpMessageConverter.SBMessageToAmqpMessage(message); |
| | 107 | |
|
| | 108 | | try |
| | 109 | | { |
| | 110 | | // Calculate the size for the message, based on the AMQP message size and accounting for a |
| | 111 | | // bit of reserved overhead size. |
| | 112 | |
|
| 70 | 113 | | var size = _sizeBytes |
| 70 | 114 | | + amqpMessage.SerializedMessageSize |
| 70 | 115 | | + (amqpMessage.SerializedMessageSize <= MaximumBytesSmallMessage |
| 70 | 116 | | ? OverheadBytesSmallMessage |
| 70 | 117 | | : OverheadBytesLargeMessage); |
| | 118 | |
|
| 70 | 119 | | if (size > MaxSizeInBytes) |
| | 120 | | { |
| 4 | 121 | | return false; |
| | 122 | | } |
| | 123 | |
|
| 66 | 124 | | _sizeBytes = size; |
| 66 | 125 | | BatchMessages.Add(message); |
| | 126 | |
|
| 66 | 127 | | return true; |
| | 128 | | } |
| | 129 | | finally |
| | 130 | | { |
| 70 | 131 | | amqpMessage?.Dispose(); |
| 70 | 132 | | } |
| 70 | 133 | | } |
| | 134 | |
|
| | 135 | | /// <summary> |
| | 136 | | /// Clears the batch, removing all messages and resetting the |
| | 137 | | /// available size. |
| | 138 | | /// </summary> |
| | 139 | | /// |
| | 140 | | public override void Clear() |
| | 141 | | { |
| 10 | 142 | | BatchMessages.Clear(); |
| 10 | 143 | | _sizeBytes = _reservedSize; |
| 10 | 144 | | } |
| | 145 | |
|
| | 146 | | /// <summary> |
| | 147 | | /// Represents the batch as an enumerable set of transport-specific |
| | 148 | | /// representations of a message. |
| | 149 | | /// </summary> |
| | 150 | | /// |
| | 151 | | /// <typeparam name="T">The transport-specific message representation being requested.</typeparam> |
| | 152 | | /// |
| | 153 | | /// <returns>The set of messages as an enumerable of the requested type.</returns> |
| | 154 | | /// |
| | 155 | | public override IEnumerable<T> AsEnumerable<T>() |
| | 156 | | { |
| 4 | 157 | | if (typeof(T) != typeof(ServiceBusMessage)) |
| | 158 | | { |
| 2 | 159 | | throw new FormatException(string.Format(CultureInfo.CurrentCulture, Resources.UnsupportedTransportEventT |
| | 160 | | } |
| | 161 | |
|
| 2 | 162 | | return (IEnumerable<T>)BatchMessages; |
| | 163 | | } |
| | 164 | |
|
| | 165 | | /// <summary> |
| | 166 | | /// Performs the task needed to clean up resources used by the <see cref="AmqpMessageBatch" />. |
| | 167 | | /// </summary> |
| | 168 | | /// |
| | 169 | | public override void Dispose() |
| | 170 | | { |
| 6 | 171 | | _disposed = true; |
| 6 | 172 | | Clear(); |
| 6 | 173 | | } |
| | 174 | | } |
| | 175 | | } |