| | 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.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | |
|
| | 9 | | namespace Azure.Messaging.ServiceBus |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// The <see cref="ProcessSessionMessageEventArgs"/> contain event args that are specific |
| | 13 | | /// to the <see cref="ServiceBusReceivedMessage"/> and session that is being processed. |
| | 14 | | /// </summary> |
| | 15 | | public class ProcessSessionMessageEventArgs : EventArgs |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// The <see cref="ServiceBusReceivedMessage"/> to be processed. |
| | 19 | | /// </summary> |
| 0 | 20 | | public ServiceBusReceivedMessage Message { get; } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// The processor's <see cref="System.Threading.CancellationToken"/> instance which will be |
| | 24 | | /// cancelled in the event that <see cref="ServiceBusProcessor.StopProcessingAsync"/> is called. |
| | 25 | | /// </summary> |
| 0 | 26 | | public CancellationToken CancellationToken { get; } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// The <see cref="ServiceBusSessionReceiver"/> that will be used for all settlement methods for the args. |
| | 30 | | /// </summary> |
| | 31 | | private readonly ServiceBusSessionReceiver _sessionReceiver; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// The Session Id associated with the <see cref="ServiceBusReceivedMessage"/>. |
| | 35 | | /// </summary> |
| 0 | 36 | | public string SessionId => _sessionReceiver.SessionId; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Gets the DateTime that the session corresponding to |
| | 40 | | /// the <see cref="ServiceBusReceivedMessage"/> is locked until. |
| | 41 | | /// </summary> |
| 0 | 42 | | public DateTimeOffset SessionLockedUntil => _sessionReceiver.SessionLockedUntil; |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Initializes a new instance of the <see cref="ProcessSessionMessageEventArgs"/> class. |
| | 46 | | /// </summary> |
| | 47 | | /// |
| | 48 | | /// <param name="message">The current <see cref="ServiceBusReceivedMessage"/>.</param> |
| | 49 | | /// <param name="receiver">The <see cref="ServiceBusSessionReceiver"/> that will be used for all settlement meth |
| | 50 | | /// for the args.</param> |
| | 51 | | /// <param name="cancellationToken">The processor's <see cref="System.Threading.CancellationToken"/> instance wh |
| 4 | 52 | | internal ProcessSessionMessageEventArgs( |
| 4 | 53 | | ServiceBusReceivedMessage message, |
| 4 | 54 | | ServiceBusSessionReceiver receiver, |
| 4 | 55 | | CancellationToken cancellationToken) |
| | 56 | | { |
| 4 | 57 | | Message = message; |
| 4 | 58 | | _sessionReceiver = receiver; |
| 4 | 59 | | CancellationToken = cancellationToken; |
| 4 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Gets the session state. |
| | 64 | | /// </summary> |
| | 65 | | /// |
| | 66 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 67 | | /// |
| | 68 | | /// <returns>The session state as byte array.</returns> |
| | 69 | | public virtual async Task<byte[]> GetSessionStateAsync( |
| | 70 | | CancellationToken cancellationToken = default) => |
| 2 | 71 | | await _sessionReceiver.GetSessionStateAsync(cancellationToken).ConfigureAwait(false); |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Set a custom state on the session which can be later retrieved using <see cref="GetSessionStateAsync"/> |
| | 75 | | /// </summary> |
| | 76 | | /// |
| | 77 | | /// <param name="sessionState">A byte array of session state</param> |
| | 78 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 79 | | /// |
| | 80 | | /// <remarks>This state is stored on Service Bus forever unless you set an empty state on it.</remarks> |
| | 81 | | /// |
| | 82 | | /// <returns>A task to be resolved on when the operation has completed.</returns> |
| | 83 | | public virtual async Task SetSessionStateAsync( |
| | 84 | | byte[] sessionState, |
| | 85 | | CancellationToken cancellationToken = default) => |
| 2 | 86 | | await _sessionReceiver.SetSessionStateAsync(sessionState, cancellationToken).ConfigureAwait(false); |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Abandons a <see cref="ServiceBusReceivedMessage"/>. This will make the message available again for immediate |
| | 90 | | /// </summary> |
| | 91 | | /// |
| | 92 | | /// <param name="message">The <see cref="ServiceBusReceivedMessage"/> to abandon.</param> |
| | 93 | | /// <param name="propertiesToModify">The properties of the message to modify while abandoning the message.</para |
| | 94 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 95 | | /// |
| | 96 | | /// <remarks> |
| | 97 | | /// Abandoning a message will increase the delivery count on the message. |
| | 98 | | /// This operation can only be performed on messages that were received by this receiver |
| | 99 | | /// when <see cref="ReceiveMode"/> is set to <see cref="ReceiveMode.PeekLock"/>. |
| | 100 | | /// </remarks> |
| | 101 | | /// |
| | 102 | | /// <returns>A task to be resolved on when the operation has completed.</returns> |
| | 103 | | public async Task AbandonMessageAsync( |
| | 104 | | ServiceBusReceivedMessage message, |
| | 105 | | IDictionary<string, object> propertiesToModify = default, |
| | 106 | | CancellationToken cancellationToken = default) |
| | 107 | | { |
| 4 | 108 | | await _sessionReceiver.AbandonMessageAsync(message, propertiesToModify, cancellationToken) |
| 4 | 109 | | .ConfigureAwait(false); |
| 2 | 110 | | message.IsSettled = true; |
| 2 | 111 | | } |
| | 112 | |
|
| | 113 | | /// <summary> |
| | 114 | | /// Completes a <see cref="ServiceBusReceivedMessage"/>. This will delete the message from the service. |
| | 115 | | /// </summary> |
| | 116 | | /// <param name="message">The message to complete.</param> |
| | 117 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 118 | | /// |
| | 119 | | /// <remarks> |
| | 120 | | /// This operation can only be performed on a message that was received by this receiver |
| | 121 | | /// when <see cref="ReceiveMode"/> is set to <see cref="ReceiveMode.PeekLock"/>. |
| | 122 | | /// </remarks> |
| | 123 | | /// |
| | 124 | | /// <returns>A task to be resolved on when the operation has completed.</returns> |
| | 125 | | public async Task CompleteMessageAsync( |
| | 126 | | ServiceBusReceivedMessage message, |
| | 127 | | CancellationToken cancellationToken = default) |
| | 128 | | { |
| 4 | 129 | | await _sessionReceiver.CompleteMessageAsync( |
| 4 | 130 | | message, |
| 4 | 131 | | cancellationToken) |
| 4 | 132 | | .ConfigureAwait(false); |
| 2 | 133 | | message.IsSettled = true; |
| 2 | 134 | | } |
| | 135 | |
|
| | 136 | | /// <summary> |
| | 137 | | /// Moves a message to the deadletter sub-queue. |
| | 138 | | /// </summary> |
| | 139 | | /// |
| | 140 | | /// <param name="message">The <see cref="ServiceBusReceivedMessage"/> to deadletter.</param> |
| | 141 | | /// <param name="deadLetterReason">The reason for deadlettering the message.</param> |
| | 142 | | /// <param name="deadLetterErrorDescription">The error description for deadlettering the message.</param> |
| | 143 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 144 | | /// |
| | 145 | | /// <remarks> |
| | 146 | | /// In order to receive a message from the deadletter queue, you can call |
| | 147 | | /// <see cref="ServiceBusClient.CreateDeadLetterReceiver(string, ServiceBusReceiverOptions)"/> |
| | 148 | | /// or <see cref="ServiceBusClient.CreateDeadLetterReceiver(string, string, ServiceBusReceiverOptions)"/> |
| | 149 | | /// to create a receiver for the queue or subscription. |
| | 150 | | /// This operation can only be performed when <see cref="ReceiveMode"/> is set to <see cref="ReceiveMode.PeekLoc |
| | 151 | | /// </remarks> |
| | 152 | | public async Task DeadLetterMessageAsync( |
| | 153 | | ServiceBusReceivedMessage message, |
| | 154 | | string deadLetterReason, |
| | 155 | | string deadLetterErrorDescription = default, |
| | 156 | | CancellationToken cancellationToken = default) |
| | 157 | | { |
| 4 | 158 | | await _sessionReceiver.DeadLetterMessageAsync( |
| 4 | 159 | | message, |
| 4 | 160 | | deadLetterReason, |
| 4 | 161 | | deadLetterErrorDescription, |
| 4 | 162 | | cancellationToken) |
| 4 | 163 | | .ConfigureAwait(false); |
| 2 | 164 | | message.IsSettled = true; |
| 2 | 165 | | } |
| | 166 | |
|
| | 167 | | /// <summary> |
| | 168 | | /// Moves a message to the deadletter sub-queue. |
| | 169 | | /// </summary> |
| | 170 | | /// |
| | 171 | | /// <param name="message">The <see cref="ServiceBusReceivedMessage"/> to deadletter.</param> |
| | 172 | | /// <param name="propertiesToModify">The properties of the message to modify while moving to sub-queue.</param> |
| | 173 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 174 | | /// |
| | 175 | | /// <remarks> |
| | 176 | | /// In order to receive a message from the deadletter queue, you can call |
| | 177 | | /// <see cref="ServiceBusClient.CreateDeadLetterReceiver(string, ServiceBusReceiverOptions)"/> |
| | 178 | | /// or <see cref="ServiceBusClient.CreateDeadLetterReceiver(string, string, ServiceBusReceiverOptions)"/> |
| | 179 | | /// to create a receiver for the queue or subscription. |
| | 180 | | /// This operation can only be performed when <see cref="ReceiveMode"/> is set to <see cref="ReceiveMode.PeekLoc |
| | 181 | | /// </remarks> |
| | 182 | | public async Task DeadLetterMessageAsync( |
| | 183 | | ServiceBusReceivedMessage message, |
| | 184 | | IDictionary<string, object> propertiesToModify = default, |
| | 185 | | CancellationToken cancellationToken = default) |
| | 186 | | { |
| 4 | 187 | | await _sessionReceiver.DeadLetterMessageAsync( |
| 4 | 188 | | message, |
| 4 | 189 | | propertiesToModify, |
| 4 | 190 | | cancellationToken) |
| 4 | 191 | | .ConfigureAwait(false); |
| 2 | 192 | | message.IsSettled = true; |
| 2 | 193 | | } |
| | 194 | |
|
| | 195 | | /// <summary> Defers the processing for a message.</summary> |
| | 196 | | /// |
| | 197 | | /// <param name="message">The <see cref="ServiceBusReceivedMessage"/> to defer.</param> |
| | 198 | | /// <param name="propertiesToModify">The properties of the message to modify while deferring the message.</param |
| | 199 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 200 | | /// |
| | 201 | | /// <remarks> |
| | 202 | | /// A lock token can be found in <see cref="ServiceBusReceivedMessage.LockToken"/>, |
| | 203 | | /// only when <see cref="ReceiveMode"/> is set to <see cref="ReceiveMode.PeekLock"/>. |
| | 204 | | /// In order to receive this message again in the future, you will need to save the <see cref="ServiceBusReceive |
| | 205 | | /// and receive it using <see cref="ServiceBusReceiver.ReceiveDeferredMessagesAsync(IEnumerable{long}, Cancellat |
| | 206 | | /// Deferring messages does not impact message's expiration, meaning that deferred messages can still expire. |
| | 207 | | /// This operation can only be performed on messages that were received by this receiver. |
| | 208 | | /// </remarks> |
| | 209 | | /// |
| | 210 | | /// <returns>A task to be resolved on when the operation has completed.</returns> |
| | 211 | | public async Task DeferMessageAsync( |
| | 212 | | ServiceBusReceivedMessage message, |
| | 213 | | IDictionary<string, object> propertiesToModify = default, |
| | 214 | | CancellationToken cancellationToken = default) |
| | 215 | | { |
| 4 | 216 | | await _sessionReceiver.DeferMessageAsync( |
| 4 | 217 | | message, |
| 4 | 218 | | propertiesToModify, |
| 4 | 219 | | cancellationToken) |
| 4 | 220 | | .ConfigureAwait(false); |
| 2 | 221 | | message.IsSettled = true; |
| 2 | 222 | | } |
| | 223 | | } |
| | 224 | | } |