protected virtual void Handle(ICommandHandler handler, ICommand command)
        {
            int  i    = 0;
            bool done = false;

            while (!done && i < 100)
            {
                try
                {
                    _messagesTracker.ElaborationStarted(command.MessageId, DateTime.UtcNow);
                    handler.Handle(command);
                    _messagesTracker.Completed(command.MessageId, DateTime.UtcNow);
                    done = true;
                }
                catch (ConflictingCommandException ex)
                {
                    MetricsHelper.MarkConcurrencyException();
                    // retry
                    if (Logger.IsInfoEnabled)
                    {
                        Logger.InfoFormat(ex, "Handled {0} {1}, concurrency exception. Retrying", command.GetType().FullName, command.MessageId);
                    }
                    if (i++ > 5)
                    {
                        Thread.Sleep(new Random(DateTime.Now.Millisecond).Next(i * 10));
                    }
                }
                catch (DomainException ex)
                {
                    _logger.ErrorFormat(ex, "DomainException on command {0} [MessageId: {1}] : {2} : {3}", command.GetType(), command.MessageId, command.Describe(), ex.Message);
                    MetricsHelper.MarkDomainException();
                    _messagesTracker.Failed(command.MessageId, DateTime.UtcNow, ex);
                    throw; //rethrow domain exception.
                }
                catch (Exception ex)
                {
                    _logger.ErrorFormat(ex, "Generic Exception on command {0} [MessageId: {1}] : {2} : {3}", command.GetType(), command.MessageId, command.Describe(), ex.Message);
                    _messagesTracker.Failed(command.MessageId, DateTime.UtcNow, ex);
                    throw; //rethrow exception.
                }
            }
            if (done == false)
            {
                _logger.ErrorFormat("Too many conflict on command {0} [MessageId: {1}] : {2}", command.GetType(), command.MessageId, command.Describe());
                var exception = new Exception("Command failed. Too many Conflicts");
                _messagesTracker.Failed(command.MessageId, DateTime.UtcNow, exception);

                throw exception;
            }
            if (Logger.IsDebugEnabled)
            {
                Logger.DebugFormat("Command {0} executed: {1}", command.MessageId, command.Describe());
            }
        }
예제 #2
0
        public void Handle(T message)
        {
            try
            {
                Logger.MarkCommandExecution(message);
                if (Logger.IsDebugEnabled)
                {
                    Logger.DebugFormat("Handling {0} {1}", message.GetType().FullName, message.MessageId);
                }
                int  i        = 0;
                bool done     = false;
                var  notifyTo = message.GetContextData(MessagesConstants.ReplyToHeader);
                while (!done && i < 100)
                {
                    try
                    {
                        _messagesTracker.ElaborationStarted(message.MessageId, DateTime.UtcNow);
                        _commandHandler.Handle(message);
                        _messagesTracker.Completed(message.MessageId, DateTime.UtcNow);
                        done = true;

                        if (notifyTo != null && message.GetContextData("disable-success-reply", "false") != "true")
                        {
                            var replyCommand = new CommandHandled(
                                notifyTo,
                                message.MessageId,
                                CommandHandled.CommandResult.Handled,
                                message.Describe()
                                );
                            replyCommand.CopyHeaders(message);
                            _bus.Reply(replyCommand);
                        }
                    }
                    catch (ConflictingCommandException ex)
                    {
                        MetricsHelper.MarkConcurrencyException();
                        // retry
                        if (Logger.IsInfoEnabled)
                        {
                            Logger.InfoFormat(ex, "Handled {0} {1} [{2}], concurrency exception. Retry count: {3}", message.GetType().FullName, message.MessageId, message.Describe(), i);
                        }
                        if (i++ > 5)
                        {
                            Thread.Sleep(new Random(DateTime.Now.Millisecond).Next(i * 10));
                        }
                    }
                    catch (DomainException ex)
                    {
                        MetricsHelper.MarkDomainException();
                        done = true;
                        _messagesTracker.Failed(message.MessageId, DateTime.UtcNow, ex);

                        if (notifyTo != null)
                        {
                            var replyCommand = new CommandHandled(
                                notifyTo,
                                message.MessageId,
                                CommandHandled.CommandResult.Failed,
                                message.Describe(),
                                ex.Message,
                                true
                                );
                            replyCommand.CopyHeaders(message);
                            _bus.Reply(replyCommand);
                        }
                        _logger.ErrorFormat(ex, "DomainException on command {0} [MessageId: {1}] : {2} : {3}", message.GetType(), message.MessageId, message.Describe(), ex.Message);
                    }
                    catch (Exception ex)
                    {
                        _logger.ErrorFormat(ex, "Generic Exception on command {0} [MessageId: {1}] : {2} : {3}", message.GetType(), message.MessageId, message.Describe(), ex.Message);
                        _messagesTracker.Failed(message.MessageId, DateTime.UtcNow, ex);
                        throw; //rethrow exception.
                    }
                }
                if (done == false)
                {
                    _logger.ErrorFormat("Too many conflict on command {0} [MessageId: {1}] : {2}", message.GetType(), message.MessageId, message.Describe());
                    var exception = new Exception("Command failed. Too many Conflicts");
                    _messagesTracker.Failed(message.MessageId, DateTime.UtcNow, exception);
                }
                if (Logger.IsDebugEnabled)
                {
                    Logger.DebugFormat("Handled {0} {1} {3}", message.GetType().FullName, message.MessageId, message.Describe());
                }
            }
            finally
            {
                Logger.ClearCommandExecution();
            }
        }