Example #1
0
 public Outcome(bool Succeeded, P Payload, IApplicationMessage Description)
 {
     this.Succeeded   = Succeeded;
     this._Payload    = Payload;
     this.Description = Description;
     this.Terminal    = not(Succeeded);
 }
Example #2
0
        private void CommitChangesAsync(ProcessingCommand processingCommand, IApplicationMessage message, int retryTimes)
        {
            var command        = processingCommand.Message;
            var handledCommand = new HandledCommand(command.Id, command.AggregateRootId, message);

            _ioHelper.TryAsyncActionRecursively <AsyncTaskResult <CommandAddResult> >("AddCommandAsync",
                                                                                      () => _commandStore.AddAsync(handledCommand),
                                                                                      currentRetryTimes => CommitChangesAsync(processingCommand, message, currentRetryTimes),
                                                                                      result =>
            {
                var commandAddResult = result.Data;
                if (commandAddResult == CommandAddResult.Success)
                {
                    PublishMessageAsync(processingCommand, message, 0);
                }
                else if (commandAddResult == CommandAddResult.DuplicateCommand)
                {
                    HandleDuplicatedCommandAsync(processingCommand, 0);
                }
                else
                {
                    _logger.ErrorFormat("Add command async failed, commandType:{0}, commandId:{1}, aggregateRootId:{2}", command.GetType().Name, command.Id, command.AggregateRootId);
                    NotifyCommandExecuted(processingCommand, CommandStatus.Failed, typeof(string).FullName, "Add command async failed.");
                }
            },
                                                                                      () => string.Format("[handledCommand:{0}]", handledCommand),
                                                                                      errorMessage => NotifyCommandExecuted(processingCommand, CommandStatus.Failed, typeof(string).Name, errorMessage ?? "Add command async failed."),
                                                                                      retryTimes);
        }
Example #3
0
 Outcome(bool Succeeded, P Payload, IApplicationMessage Description, bool Terminal)
 {
     this.Succeeded   = Succeeded;
     this._Payload    = Payload;
     this.Description = Description;
     this.Terminal    = Terminal;
 }
Example #4
0
 public void Notify(IApplicationMessage messageNotifier, string message)
 {
     if (messageNotifier != null && !string.IsNullOrEmpty(message))
     {
         messageNotifier.SendMessage(message);
     }
 }
Example #5
0
 /// <summary>Parameterized constructor.
 /// </summary>
 /// <param name="command"></param>
 /// <param name="aggregateRootId"></param>
 /// <param name="aggregateRootTypeCode"></param>
 /// <param name="message"></param>
 public HandledCommand(ICommand command, string aggregateRootId = null, int aggregateRootTypeCode = 0, IApplicationMessage message = null)
 {
     Command               = command;
     AggregateRootId       = aggregateRootId;
     AggregateRootTypeCode = aggregateRootTypeCode;
     Message               = message;
 }
        private void CommitChangesAsync(ProcessingCommand processingCommand, IApplicationMessage message, int retryTimes)
        {
            var command        = processingCommand.Message;
            var handledCommand = new HandledCommand(
                command,
                aggregateRootId: command.AggregateRootId,
                message: message);

            _ioHelper.TryAsyncActionRecursively <AsyncTaskResult <CommandAddResult> >("AddCommandAsync",
                                                                                      () => _commandStore.AddAsync(handledCommand),
                                                                                      currentRetryTimes => CommitChangesAsync(processingCommand, message, currentRetryTimes),
                                                                                      result =>
            {
                var commandAddResult = result.Data;
                if (commandAddResult == CommandAddResult.Success)
                {
                    PublishMessageAsync(processingCommand, message, 0);
                }
                else if (commandAddResult == CommandAddResult.DuplicateCommand)
                {
                    HandleDuplicatedCommandAsync(processingCommand, 0);
                }
                else
                {
                    NotifyCommandExecuted(processingCommand, CommandStatus.Failed, null, "Add command async failed.");
                }
            },
                                                                                      () => string.Format("[handledCommand:{0}]", handledCommand),
                                                                                      () => NotifyCommandExecuted(processingCommand, CommandStatus.Failed, null, "Add command async failed."),
                                                                                      retryTimes);
        }
Example #7
0
 /// <summary>Parameterized constructor.
 /// </summary>
 /// <param name="command"></param>
 /// <param name="aggregateRootId"></param>
 /// <param name="aggregateRootTypeCode"></param>
 /// <param name="message"></param>
 public HandledCommand(ICommand command, string aggregateRootId = null, int aggregateRootTypeCode = 0, IApplicationMessage message = null)
 {
     Command = command;
     AggregateRootId = aggregateRootId;
     AggregateRootTypeCode = aggregateRootTypeCode;
     Message = message;
 }
Example #8
0
 public Outcome(bool Succeeded, IApplicationMessage Description)
 {
     this.Succeeded = false;
     if (Description == null)
     {
         throw new ArgumentNullException(nameof(Description));
     }
     this.Description = Description;
     this._Payload    = default(P);
     this.Terminal    = not(Succeeded);
 }
Example #9
0
    public Outcome(P Payload)
    {
        this.Succeeded = true;

        if (Payload == null)
        {
            throw new ArgumentNullException(nameof(Payload));
        }
        this._Payload = Payload;

        this.Description = ApplicationMessage.Empty;
        this.Terminal    = not(Succeeded);
    }
        private void PublishMessageAsync(ProcessingCommand processingCommand, IApplicationMessage message, int retryTimes)
        {
            var command = processingCommand.Message;

            _ioHelper.TryAsyncActionRecursively <AsyncTaskResult>("PublishApplicationMessageAsync",
                                                                  () => _messagePublisher.PublishAsync(message),
                                                                  currentRetryTimes => PublishMessageAsync(processingCommand, message, currentRetryTimes),
                                                                  result =>
            {
                NotifyCommandExecuted(processingCommand, CommandStatus.Success, null, null);
            },
                                                                  () => string.Format("[application message:[id:{0},type:{1}],command:[id:{2},type:{3}]]", message.Id, message.GetType().Name, command.Id, command.GetType().Name),
                                                                  () => NotifyCommandExecuted(processingCommand, CommandStatus.Failed, null, "Publish application message async failed."),
                                                                  retryTimes);
        }
        private Task PublishMessageAsync(ProcessingCommand processingCommand, IApplicationMessage message, int retryTimes, TaskCompletionSource <bool> taskSource)
        {
            var command = processingCommand.Message;

            _ioHelper.TryAsyncActionRecursivelyWithoutResult("PublishApplicationMessageAsync",
                                                             () => _applicationMessagePublisher.PublishAsync(message),
                                                             currentRetryTimes => PublishMessageAsync(processingCommand, message, currentRetryTimes, taskSource),
                                                             async() =>
            {
                await CompleteCommand(processingCommand, CommandStatus.Success, message.GetType().FullName, _jsonSerializer.Serialize(message)).ConfigureAwait(false);
                taskSource.SetResult(true);
            },
                                                             () => string.Format("[application message:[id:{0},type:{1}],command:[id:{2},type:{3}]]", message.Id, message.GetType().Name, command.Id, command.GetType().Name),
                                                             null,
                                                             retryTimes, true);

            return(taskSource.Task);
        }
        private void PublishMessageAsync(ProcessingCommand processingCommand, IApplicationMessage message, int retryTimes)
        {
            var command = processingCommand.Message;

            _ioHelper.TryAsyncActionRecursively("PublishApplicationMessageAsync",
                                                () => _applicationMessagePublisher.PublishAsync(message),
                                                currentRetryTimes => PublishMessageAsync(processingCommand, message, currentRetryTimes),
                                                result =>
            {
                CompleteCommand(processingCommand, CommandStatus.Success, message.GetType().FullName, _jsonSerializer.Serialize(message));
            },
                                                () => string.Format("[application message:[id:{0},type:{1}],command:[id:{2},type:{3}]]", message.Id, message.GetType().Name, command.Id, command.GetType().Name),
                                                errorMessage =>
            {
                _logger.Fatal(string.Format("Publish application message has unknown exception, the code should not be run to here, errorMessage: {0}", errorMessage));
            },
                                                retryTimes, true);
        }
 private void CommitChangesAsync(ProcessingCommand processingCommand, bool success, IApplicationMessage message, string errorMessage)
 {
     if (success)
     {
         if (message != null)
         {
             PublishMessageAsync(processingCommand, message, 0);
         }
         else
         {
             CompleteCommand(processingCommand, CommandStatus.Success, null, null);
         }
     }
     else
     {
         CompleteCommand(processingCommand, CommandStatus.Failed, typeof(string).FullName, errorMessage);
     }
 }
Example #14
0
 /// <summary>Parameterized constructor.
 /// </summary>
 /// <param name="commandId"></param>
 /// <param name="aggregateRootId"></param>
 /// <param name="message"></param>
 public HandledCommand(string commandId, string aggregateRootId = null, IApplicationMessage message = null)
 {
     CommandId = commandId;
     AggregateRootId = aggregateRootId;
     Message = message;
 }
Example #15
0
 /// <summary>Parameterized constructor.
 /// </summary>
 /// <param name="commandId"></param>
 /// <param name="aggregateRootId"></param>
 /// <param name="message"></param>
 public HandledCommand(string commandId, string aggregateRootId = null, IApplicationMessage message = null)
 {
     CommandId       = commandId;
     AggregateRootId = aggregateRootId;
     Message         = message;
 }
Example #16
0
 public void Trigger(IApplicationMessage applicationMessage)
 {
     Messenger.Trigger(applicationMessage);
 }
Example #17
0
        async Task PublishApplicationMessageAsync(ProcessingCommand processingCommand, IApplicationMessage applicationMessage)
        {
            var result = await Policy
                         .Handle <Exception>()
                         .OrResult <AsyncExecutedResult>(r => !r.Succeeded)
                         .WaitAndRetryForeverAsync(
                retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                (delegateResult, retryCount, retryAttempt) => logger.LogError(delegateResult.Exception ?? delegateResult.Result.Exception, $"命令产生的应用消息发布失败,重试。 [CommandType = {processingCommand.Command.GetType()}, CommandId = {processingCommand.Command.Id}, ApplicationMessageType = {applicationMessage.GetType()}, ApplicationMessageId = {applicationMessage.Id}, ApplicationMessageRoutingKey = {applicationMessage.GetRoutingKey()}, RetryCount = {retryCount}, RetryAttempt = {retryAttempt}]"))
                         .ExecuteAsync(() => applicationMessagePublisher.PublishAsync(applicationMessage));

            if (result.Succeeded)
            {
                logger.LogDebug($"命令产生的应用消息发布成功。 [CommandType = {processingCommand.Command.GetType()}, CommandId = {processingCommand.Command.Id}, ApplicationMessageType = {applicationMessage.GetType()}, ApplicationMessageId = {applicationMessage.Id}, ApplicationMessageRoutingKey = {applicationMessage.GetRoutingKey()}]");
                await processingCommand.OnQueueProcessedAsync(CommandExecutedStatus.Succeeded, objectSerializer.Serialize(applicationMessage), applicationMessage.GetTag());
            }
            else
            {
                logger.LogDebug(result.Exception, $"命令产生的应用消息发布失败。 [CommandType = {processingCommand.Command.GetType()}, CommandId = {processingCommand.Command.Id}, ApplicationMessageType = {applicationMessage.GetType()}, ApplicationMessageId = {applicationMessage.Id}, ApplicationMessageRoutingKey = {applicationMessage.GetRoutingKey()}]");
            }
        }
        private void CommitChangesAsync(ProcessingCommand processingCommand, bool success, IApplicationMessage message, string errorMessage, int retryTimes)
        {
            var command        = processingCommand.Message;
            var handledCommand = new HandledCommand(command.Id, command.AggregateRootId, message);

            _ioHelper.TryAsyncActionRecursively("AddCommandAsync",
                                                () => _commandStore.AddAsync(handledCommand),
                                                currentRetryTimes => CommitChangesAsync(processingCommand, success, message, errorMessage, currentRetryTimes),
                                                result =>
            {
                var commandAddResult = result.Data;
                if (commandAddResult == CommandAddResult.Success)
                {
                    if (success)
                    {
                        if (message != null)
                        {
                            PublishMessageAsync(processingCommand, message, 0);
                        }
                        else
                        {
                            CompleteCommand(processingCommand, CommandStatus.Success, null, null);
                        }
                    }
                    else
                    {
                        CompleteCommand(processingCommand, CommandStatus.Failed, typeof(string).FullName, errorMessage);
                    }
                }
                else if (commandAddResult == CommandAddResult.DuplicateCommand)
                {
                    HandleDuplicatedCommandAsync(processingCommand, 0);
                }
            },
                                                () => string.Format("[handledCommand:{0}]", handledCommand),
                                                error =>
            {
                _logger.Fatal(string.Format("Add command has unknown exception, the code should not be run to here, errorMessage: {0}", error));
            },
                                                retryTimes, true);
        }
Example #19
0
 public void Trigger(IApplicationMessage message)
 {
     applicationMessageEvent.Trigger(message);
 }
 public ProcessingApplicationMessage(IApplicationMessage applicationMessage) => ApplicationMessage = applicationMessage;
Example #21
0
 public ProcessingApplicationMessage(IApplicationMessage message, IMessageProcessContext processContext)
 {
     Message         = message;
     _processContext = processContext;
 }
        private async Task CommitChangesAsync(ProcessingCommand processingCommand, bool success, IApplicationMessage message, string errorMessage, TaskCompletionSource <bool> taskSource)
        {
            if (success)
            {
                if (message != null)
                {
                    message.MergeItems(processingCommand.Message.Items);
                    await PublishMessageAsync(processingCommand, message, 0, new TaskCompletionSource <bool>()).ConfigureAwait(false);

                    taskSource.SetResult(true);
                }
                else
                {
                    await CompleteCommand(processingCommand, CommandStatus.Success, null, null).ConfigureAwait(false);

                    taskSource.SetResult(true);
                }
            }
            else
            {
                await CompleteCommand(processingCommand, CommandStatus.Failed, typeof(string).FullName, errorMessage).ConfigureAwait(false);

                taskSource.SetResult(true);
            }
        }
Example #23
0
 public Task PublishAsync(IApplicationMessage message)
 {
     return(Task.CompletedTask);
 }
Example #24
0
 public void Trigger(IApplicationMessage message)
 {
     Events(message);
 }
Example #25
0
 public void SetApplicationMessage(IApplicationMessage applicationMessage)
 {
     _applicationMessage = applicationMessage;
 }
Example #26
0
 public void SendLocal(IApplicationMessage applicationEvent)
 {
     var queuePath = String.Format(Config.Get <string>("msmqNameTmpl"), Environment.MachineName);
     //MassTransitServiceBus.GetSendEndpoint(new Uri(queuePath)).Start(); .Send(applicationEvent);
 }
Example #27
0
 public Task <AsyncTaskResult> PublishAsync(IApplicationMessage message)
 {
     return(_successResultTask);
 }