private void HandleCommand(HandlerStore handlerStore, IDomainCommand domainMessage) { var currentTuple = Tuple.Create(handlerStore, domainMessage); HandleMessage(currentTuple.Item1, currentTuple.Item2, 1); /* do * { * lock (_runningCommands) * { * if (_runningCommands.Contains(currentTuple.Item2.Id.ToString())) * { * _delayedCommands.Enqueue(currentTuple); * currentTuple = null; * } * else * { * _runningCommands.Add(domainMessage.Id.ToString()); * } * } * * if (currentTuple != null) * { * HandleMessage(currentTuple.Item1, currentTuple.Item2,1); * lock (_runningCommands) * { * _runningCommands.Remove(currentTuple.Item2.Id.ToString()); * } * } * _delayedCommands.TryDequeue(out currentTuple); * * } * while (currentTuple != null); */ }
private void HandleMessage(HandlerStore handlerStore, IDomainMessage domainMessage, int maxHandlers = int.MaxValue) { DomainResult domainResult; try { var targetHandlers = maxHandlers == int.MaxValue ? handlerStore.Handlers : handlerStore.Handlers.Take(maxHandlers); _logger.InfoFormat("Got {0} handlers to invoke", targetHandlers.Count()); Parallel.ForEach(targetHandlers, handler => { handler.AsDynamic().Handle(domainMessage); }); domainResult = new DomainResult(domainMessage, ResultCode.OK, string.Empty); } catch (Exception e) { _logger.Error("Error processing " + domainMessage.GetType(), e); var errorMessage = e.InnerException != null ? e.InnerException.Message : e.Message; _logger.Error("Error message " + errorMessage); domainResult = new DomainResult(domainMessage, ResultCode.Failed, errorMessage); } _resultPublisher.Publish(domainResult); }