public void Add(Guid commandId, CommandAsyncResult commandAsyncResult)
 {
     if (!_commandAsyncResultDict.TryAdd(commandId, commandAsyncResult))
     {
         throw new Exception(string.Format("Command with id '{0}' is already exist.", commandId));
     }
 }
예제 #2
0
        public void Execute(ICommand command)
        {
            var commandQueue = _commandQueueRouter.Route(command);
            if (commandQueue == null)
            {
                throw new Exception("Could not route the command to an appropriate command queue.");
            }

            var waitHandle = new ManualResetEvent(false);
            var commandAsyncResult = new CommandAsyncResult(waitHandle);

            _commandAsyncResultManager.Add(command.Id, commandAsyncResult);
            commandQueue.Enqueue(command);
            waitHandle.WaitOne(command.MillisecondsTimeout);
            _commandAsyncResultManager.Remove(command.Id);

            if (!commandAsyncResult.IsCompleted)
            {
                throw new CommandTimeoutException(command.Id, command.GetType());
            }
            else if (commandAsyncResult.Exception != null)
            {
                throw new CommandExecuteException(command.Id, command.GetType(), commandAsyncResult.Exception);
            }
            else if (commandAsyncResult.ErrorMessage != null)
            {
                throw new CommandExecuteException(command.Id, command.GetType(), commandAsyncResult.ErrorMessage);
            }
        }