コード例 #1
0
        public async Task DispatchMessage(SQSMessage message)
        {
            Message typedMessage;

            try
            {
                typedMessage = _serialisationRegister.DeserializeMessage(message.Body);
            }
            catch (MessageFormatNotSupportedException ex)
            {
                Log.Trace(
                    "Didn't handle message [{0}]. No serialiser setup",
                    message.Body ?? string.Empty);
                DeleteMessageFromQueue(message.ReceiptHandle);
                _onError(ex, message);
                return;
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Error deserialising message");
                _onError(ex, message);
                return;
            }

            try
            {
                var handlingSucceeded = true;

                if (typedMessage != null)
                {
                    typedMessage.ReceiptHandle = message.ReceiptHandle;
                    typedMessage.QueueUrl      = _queue.Url;
                    handlingSucceeded          = await CallMessageHandlers(typedMessage).ConfigureAwait(false);
                }

                if (handlingSucceeded)
                {
                    DeleteMessageFromQueue(message.ReceiptHandle);
                }
            }
            catch (Exception ex)
            {
                var errorText = string.Format("Error handling message [{0}]", message.Body);
                Log.Error(ex, errorText);

                if (typedMessage != null)
                {
                    _messagingMonitor.HandleException(typedMessage.GetType().Name);
                }

                _onError(ex, message);
            }
        }
コード例 #2
0
        protected override void Given()
        {
            _serialisationRegister
            .DeserializeMessage(Arg.Any <string>())
            .Returns(x => { throw new TestException("Test from WhenThereAreExceptionsInMessageProcessing"); });
            _sqs.ReceiveMessageAsync(
                Arg.Any <ReceiveMessageRequest>(),
                Arg.Any <CancellationToken>())
            .Returns(x => Task.FromResult(GenerateEmptyMessage()));

            _sqs.When(x => x.ReceiveMessageAsync(
                          Arg.Any <ReceiveMessageRequest>(),
                          Arg.Any <CancellationToken>()))
            .Do(x => _callCount++);
        }
コード例 #3
0
        protected override void Given()
        {
            Sqs = Substitute.For <IAmazonSQS>();
            SerialisationRegister = Substitute.For <IMessageSerialisationRegister>();
            Monitor = Substitute.For <IMessageMonitor>();
            Handler = Substitute.For <IHandler <GenericMessage> >();

            var response = GenerateResponseMessage(_messageTypeString, Guid.NewGuid());

            Sqs.ReceiveMessageAsync(
                Arg.Any <ReceiveMessageRequest>(),
                Arg.Any <CancellationToken>())
            .Returns(
                x => Task.FromResult(response),
                x => Task.FromResult(new ReceiveMessageResponse()));

            DeserialisedMessage = new GenericMessage {
                RaisingComponent = "Component"
            };
            SerialisationRegister.DeserializeMessage(Arg.Any <string>()).Returns(DeserialisedMessage);
        }
コード例 #4
0
        public void ProcessMessageAction(Amazon.SQS.Model.Message message)
        {
            Message typedMessage = null;
            string  rawMessage   = null;

            try
            {
                typedMessage = _serialisationRegister.DeserializeMessage(message.Body);

                var handlingSucceeded = true;

                if (typedMessage != null)
                {
                    List <Func <Message, bool> > handlers;
                    if (!_handlers.TryGetValue(typedMessage.GetType(), out handlers))
                    {
                        return;
                    }

                    foreach (var handle in handlers)
                    {
                        var watch = new System.Diagnostics.Stopwatch();
                        watch.Start();

                        handlingSucceeded = handle(typedMessage);

                        watch.Stop();
                        Log.Trace("Handled message - MessageType: {0}", typedMessage.GetType().Name);
                        _messagingMonitor.HandleTime(watch.ElapsedMilliseconds);
                    }
                }

                if (handlingSucceeded)
                {
                    _queue.Client.DeleteMessage(new DeleteMessageRequest {
                        QueueUrl = _queue.Url, ReceiptHandle = message.ReceiptHandle
                    });
                }
            }
            catch (KeyNotFoundException ex)
            {
                Log.Trace(
                    "Didn't handle message [{0}]. No serialiser setup",
                    rawMessage ?? "");
                _queue.Client.DeleteMessage(new DeleteMessageRequest
                {
                    QueueUrl      = _queue.Url,
                    ReceiptHandle = message.ReceiptHandle
                });
                _onError(ex, message);
            }
            catch (Exception ex)
            {
                var msg = string.Format(
                    "Issue handling message... {0}. StackTrace: {1}",
                    message,
                    ex.StackTrace);
                Log.Error(ex, msg);
                if (typedMessage != null)
                {
                    _messagingMonitor.HandleException(typedMessage.GetType().Name);
                }
                _onError(ex, message);
            }
        }
コード例 #5
0
        public async Task DispatchMessage(SQSMessage message)
        {
            Message typedMessage;

            try
            {
                typedMessage = _serialisationRegister.DeserializeMessage(message.Body);
            }
            catch (MessageFormatNotSupportedException ex)
            {
                _log.LogTrace($"Didn't handle message [{message.Body ?? string.Empty}]. No serialiser setup");
                await DeleteMessageFromQueue(message.ReceiptHandle);

                _onError(ex, message);
                return;
            }
            catch (Exception ex)
            {
                _log.LogError(0, ex, "Error deserialising message");
                _onError(ex, message);
                return;
            }

            var       handlingSucceeded = false;
            Exception lastException     = null;

            try
            {
                if (typedMessage != null)
                {
                    typedMessage.ReceiptHandle = message.ReceiptHandle;
                    typedMessage.QueueUrl      = _queue.Url;
                    handlingSucceeded          = await CallMessageHandler(typedMessage).ConfigureAwait(false);
                }

                if (handlingSucceeded)
                {
                    await DeleteMessageFromQueue(message.ReceiptHandle);
                }
            }
            catch (Exception ex)
            {
                var errorText = $"Error handling message [{message.Body}]";
                _log.LogError(0, ex, errorText);

                if (typedMessage != null)
                {
                    _messagingMonitor.HandleException(typedMessage.GetType().Name);
                }

                _onError(ex, message);

                lastException = ex;
            }
            finally
            {
                if (!handlingSucceeded && _messageBackoffStrategy != null)
                {
                    await UpdateMessageVisibilityTimeout(message, message.ReceiptHandle, typedMessage, lastException);
                }
            }
        }