Example #1
0
        public async Task HandleMessage(Message message)
        {
            if (IsStopping)
            {
                await WaitAndAbandonMessage().ConfigureAwait(false);

                return;
            }

            using (_tracker.BeginDelivery())
            {
                var redelivered = message.Attributes.ContainsKey("ApproximateReceiveCount") &&
                                  (int.TryParse(message.Attributes["ApproximateReceiveCount"], out var receiveCount) &&
                                   receiveCount > 1);

                var context = new AmazonSqsReceiveContext(_inputAddress, message, redelivered, _context);

                context.GetOrAddPayload(() => _receiveSettings);
                context.GetOrAddPayload(() => _client);
                context.GetOrAddPayload(() => _client.ConnectionContext);

                try
                {
                    if (!_pending.TryAdd(message.MessageId, context))
                    {
                        if (_log.IsErrorEnabled)
                        {
                            _log.ErrorFormat("Duplicate BasicDeliver: {0}", message.MessageId);
                        }
                    }

                    await _context.ReceiveObservers.PreReceive(context).ConfigureAwait(false);

                    await _context.ReceivePipe.Send(context).ConfigureAwait(false);

                    await context.ReceiveCompleted.ConfigureAwait(false);

                    await _client.DeleteMessage(_receiveSettings.EntityName, message.ReceiptHandle);

                    await _context.ReceiveObservers.PostReceive(context).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    await _context.ReceiveObservers.ReceiveFault(context, ex).ConfigureAwait(false);
                }
                finally
                {
                    _pending.TryRemove(message.MessageId, out _);

                    context.Dispose();
                }
            }
        }
        public async Task HandleMessage(Message message)
        {
            if (IsStopping)
            {
                await WaitAndAbandonMessage(message).ConfigureAwait(false);

                return;
            }

            using (var delivery = _tracker.BeginDelivery())
            {
                var redelivered = message.Attributes.ContainsKey("ApproximateReceiveCount") && (int.TryParse(message.Attributes["ApproximateReceiveCount"], out var approximateReceiveCount) && approximateReceiveCount > 1);
                var context     = new AmazonSqsReceiveContext(_inputAddress, message, redelivered, _receiveObserver, _context);

                context.GetOrAddPayload(() => _errorTransport);
                context.GetOrAddPayload(() => _deadLetterTransport);

                context.GetOrAddPayload(() => _receiveSettings);
                context.GetOrAddPayload(() => _model);
                context.GetOrAddPayload(() => _model.ConnectionContext);

                try
                {
                    if (!_pending.TryAdd(message.MessageId, context))
                    {
                        if (_log.IsErrorEnabled)
                        {
                            _log.ErrorFormat("Duplicate BasicDeliver: {0}", message.MessageId);
                        }
                    }

                    await _receiveObserver.PreReceive(context).ConfigureAwait(false);

                    await _receivePipe.Send(context).ConfigureAwait(false);

                    await context.CompleteTask.ConfigureAwait(false);

                    // Acknowledge
                    await _model.DeleteMessage(_queueUrl, message.ReceiptHandle);

                    await _receiveObserver.PostReceive(context).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    await _receiveObserver.ReceiveFault(context, ex).ConfigureAwait(false);

                    try
                    {
                        //_model.BasicNack(deliveryTag, false, true);
                    }
                    catch (Exception ackEx)
                    {
                        if (_log.IsErrorEnabled)
                        {
                            _log.ErrorFormat("An error occurred trying to NACK a message with delivery tag {0}: {1}", message.MessageId, ackEx.ToString());
                        }
                    }
                }
                finally
                {
                    _pending.TryRemove(message.MessageId, out _);

                    context.Dispose();
                }
            }
        }