private async Task ProcessMessagesAsync(Message message, CancellationToken token) { _logger.LogInformation($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber}"); // Process the message try { var tasks = _messageProcessors.Select(p => p.ProcessMessageAsync(Encoding.UTF8.GetString(message.Body))); await Task.WhenAll(tasks); // Complete the message so that it is not received again. // This can be done only if the queueClient is created in ReceiveMode.PeekLock mode (which is default). await _client.CompleteAsync(message.SystemProperties.LockToken); } catch (Exception ex) { await _client.AbandonAsync(message.SystemProperties.LockToken); _logger.LogError(ex, $"Cannot process message [Id: {message.MessageId}]"); } // Note: Use the cancellationToken passed as necessary to determine if the queueClient has already been closed. // If queueClient has already been Closed, you may chose to not call CompleteAsync() or AbandonAsync() etc. calls // to avoid unnecessary exceptions. }
public async Task RequestAsync(Message message, Func <Message, Task <bool> > replyHandler, CancellationToken token) { var tcs = new TaskCompletionSource <Message>(); if (!_pendingRequests.TryAdd(message.MessageId, tcs)) { throw new InvalidOperationException("Request with the MessageId is already pending."); } await using (token.Register(t => ((TaskCompletionSource <Message>)t).TrySetCanceled(), tcs)) { await _sender.SendAsync(message); do { Message reply = await tcs.Task; try { bool processed = await replyHandler(reply); if (processed) { _pendingRequests.TryRemove(reply.CorrelationId, out _); } } catch { await _receiver.AbandonAsync(reply.SystemProperties.LockToken); _pendingRequests.TryRemove(reply.CorrelationId, out _); throw; } } while (token.IsCancellationRequested); } }
private async Task AbandonMessageAsync(Microsoft.Azure.ServiceBus.Message sbMessage, CancellationToken cancelToken) { if (cancelToken.IsCancellationRequested == false) { await receiverClient.AbandonAsync(sbMessage.SystemProperties.LockToken).ConfigureAwait(false); } }
public static Task AbandonByErrorAsync(this Message message, IReceiverClient client, Exception e) { return(client.AbandonAsync(message.SystemProperties.LockToken, new Dictionary <string, object> { { "ErrorMessage", e.Message }, { "Exception", e.ToString() } })); }
public static async Task SafeAbandonAsync(this Message message, string subscription, IReceiverClient session, Action <bool> callback, ILogger logger, Stopwatch roundtripStopwatch) { await SafeMessagingActionAsync( session.AbandonAsync(message.SystemProperties.LockToken), message, callback, "An error occurred while abandoning message {0} in subscription {1} with processing time {3} (scheduling {4} request {5} roundtrip {6}). Error message: {2}", message.MessageId, subscription, logger, roundtripStopwatch); }
protected async Task WaitAndAbandonMessage(IReceiverClient receiverClient, Message message) { try { await _deliveryComplete.Task.ConfigureAwait(false); await receiverClient.AbandonAsync(message.SystemProperties.LockToken).ConfigureAwait(false); } catch (Exception exception) { LogContext.Error?.Log(exception, "Abandon message faulted during shutdown: {InputAddress}", _context.InputAddress); } }
public async Task When_Abandon_Message_Delegate_LockToken_To_ReceiverClient_AbandonAsync() { //Dummy message var message = CreateFakeMessage(); await _sut.OpenAsync(CancellationToken.None); await _sut.Abandon(message); A.CallTo(() => _receiverClient.AbandonAsync(message.SystemProperties.LockToken, A <IDictionary <string, object> > ._)) .MustHaveHappenedOnceExactly(); }
protected async Task WaitAndAbandonMessage(IReceiverClient receiverClient, Message message) { try { await _deliveryComplete.Task.ConfigureAwait(false); await receiverClient.AbandonAsync(message.SystemProperties.LockToken).ConfigureAwait(false); } catch (Exception exception) { if (_log.IsErrorEnabled) { _log.Debug($"Stopping receiver, abandoned message faulted: {_context.InputAddress}", exception); } } }
public async Task RejectAsync(bool requeue = false) { try { if (requeue) { await _receiverClient.AbandonAsync(_message.SystemProperties.LockToken); } else { await _receiverClient.DeadLetterAsync(_message.SystemProperties.LockToken); } } catch (Exception ex) { throw new MessageException("Failed to reject the message.", ex); } }
private async Task ProcessMessageAsync(IReceiverClient client, Message message, CancellationToken cancellationToken) { _logger.LogInformation("Type : {ClientType}", client.GetType()); try { _logger.LogInformation("SystemProperties:{SystemProperties}", JsonConvert.SerializeObject(message.SystemProperties, Formatting.Indented)); string body = Encoding.UTF8.GetString(message.Body); if (message.Label == nameof(PersonMessage)) { var person = _serializer.Deserialize <PersonMessage>(message.Body); _logger.LogInformation("person = " + JsonConvert.SerializeObject(person)); } else { _logger.LogInformation("body = {body}", body); if (body == "1") { await Task.Delay(TimeSpan.FromSeconds(3), cancellationToken); // simulate delay _logger.LogError("Error: " + DateTime.UtcNow); throw new Exception("!!!"); } } await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); // simulate delay await client.CompleteAsync(message.SystemProperties.LockToken); } catch (Exception ex) { _logger.LogError(ex, "Error while Process Message with MessageId: {MessageId}", message.MessageId); if (!client.IsClosedOrClosing) { await client.AbandonAsync(message.SystemProperties.LockToken); } } }
public Task Abandon(Exception exception) { return(_receiverClient.AbandonAsync(_message.SystemProperties.LockToken, ExceptionUtil.GetExceptionHeaderDictionary(exception))); }
protected async Task abandonAsync(Message message, IReceiverClient client) { await client.AbandonAsync(message.SystemProperties.LockToken); }
public virtual async Task Abandon(Message message) { await receiver.AbandonAsync(message.SystemProperties.LockToken); }