Beispiel #1
0
 public static void TryOnReceived(
     this IMessageAuditor messageAuditor,
     ILogger logger,
     IBus bus,
     MessageInformation messageInformation
     )
 {
     try
     {
         messageAuditor.OnReceived(bus, messageInformation);
     }
     catch (Exception ex)
     {
         logger.Output(LogLevel.Error, string.Format("messageAuditor.OnReceived - {0}", ex));
     }
 }
 public static void TryOnSucceeded(
     this IMessageAuditor messageAuditor,
     ILogger logger,
     IBus bus,
     MessageInformation messageInformation
 )
 {
     try
     {
         messageAuditor.OnSucceeded(bus, messageInformation);
     }
     catch (Exception ex)
     {
         logger.Output(LogLevel.Error, string.Format("messageAuditor.OnSucceeded - {0}", ex));
     }
 }
        public void HandleMessage(IMessage message)
        {
            var messageInformation = new MessageInformation
                                     {
                                         UniqueIdentifier = Guid.NewGuid(),
                                         Topic = _topic,
                                         Channel = _channel,
                                         HandlerType = _handlerType,
                                         MessageType = _messageType,
                                         Message = message,
                                         DeserializedMessageBody = null,
                                         Started = DateTime.UtcNow
                                     };

            _bus.SetCurrentMessageInformation(messageInformation);

            // Get handler
            object handler;
            try
            {
                handler = _objectBuilder.GetInstance(_handlerType);
                messageInformation.HandlerType = handler.GetType();
            }
            catch (Exception ex)
            {
                messageInformation.Finished = DateTime.UtcNow;

                _messageAuditor.TryOnFailed(_logger, _bus,
                    new FailedMessageInformation
                    (
                        messageInformation,
                        FailedMessageQueueAction.Finish,
                        FailedMessageReason.HandlerConstructor,
                        ex
                    )
                );

                message.Finish();
                return;
            }

            // Get deserialized value
            object value;
            try
            {
                value = _serializer.Deserialize(_concreteMessageType, message.Body);
            }
            catch (Exception ex)
            {
                messageInformation.Finished = DateTime.UtcNow;

                _messageAuditor.TryOnFailed(_logger, _bus,
                    new FailedMessageInformation
                    (
                        messageInformation,
                        FailedMessageQueueAction.Finish,
                        FailedMessageReason.MessageDeserialization,
                        ex
                    )
                );

                message.Finish();
                return;
            }

            // Handle message
            messageInformation.DeserializedMessageBody = value;
            _messageAuditor.TryOnReceived(_logger, _bus, messageInformation);

            try
            {
                _handleMethod.Invoke(handler, new[] { value });
            }
            catch (Exception ex)
            {
                bool requeue = (message.Attempts < message.MaxAttempts);

                messageInformation.Finished = DateTime.UtcNow;

                if (requeue)
                    message.Requeue();
                else
                    message.Finish();

                _messageAuditor.TryOnFailed(_logger, _bus,
                    new FailedMessageInformation
                    (
                        messageInformation,
                        requeue ? FailedMessageQueueAction.Requeue : FailedMessageQueueAction.Finish,
                        FailedMessageReason.HandlerException,
                        ex
                    )
                );

                return;
            }

            messageInformation.Finished = DateTime.UtcNow;

            _messageAuditor.TryOnSucceeded(_logger, _bus, messageInformation);
        }
        public void LogFailedMessage(IMessage message)
        {
            object handler;
            try
            {
                handler = _objectBuilder.GetInstance(_handlerType);
            }
            catch
            {
                handler = _handlerType;
            }

            object deserializedMessageBody;
            try
            {
                deserializedMessageBody = _serializer.Deserialize(_concreteMessageType, message.Body);
            }
            catch
            {
                deserializedMessageBody = null;
            }

            var messageInformation = new MessageInformation
            {
                UniqueIdentifier = Guid.NewGuid(),
                Topic = _topic,
                Channel = _channel,
                HandlerType = handler.GetType(),
                MessageType = _messageType,
                Message = message,
                DeserializedMessageBody = deserializedMessageBody,
                Started = DateTime.UtcNow
            };

            _messageAuditor.TryOnFailed(_logger, _bus,
                new FailedMessageInformation
                (
                    messageInformation,
                    FailedMessageQueueAction.Finish,
                    FailedMessageReason.MaxAttemptsExceeded,
                    null
                )
            );
        }