Пример #1
0
 public void Process(EventStream eventStream, IEventProcessContext context)
 {
     var processingContext = new EventProcessingContext(eventStream, context);
     var queueIndex = processingContext.EventStream.AggregateRootId.GetHashCode() % WorkerCount;
     if (queueIndex < 0)
     {
         queueIndex = Math.Abs(queueIndex);
     }
     _queueList[queueIndex].Add(processingContext);
 }
Пример #2
0
        private void DispatchEventsToHandlers(EventProcessingContext context)
        {
            var dispatchEventsToHandlers = new Func<bool>(() =>
            {
                var eventStream = context.EventStream;
                switch (eventStream.Version)
                {
                    case 1:
                        return DispatchEventsToHandlers(eventStream);
                    default:
                        var lastPublishedVersion = _eventPublishInfoStore.GetEventPublishedVersion(eventStream.AggregateRootId);
                        if (lastPublishedVersion + 1 == eventStream.Version)
                        {
                            return DispatchEventsToHandlers(eventStream);
                        }
                        var canPublish = lastPublishedVersion + 1 > eventStream.Version;
                        if (!canPublish)
                        {
                            _logger.DebugFormat("wait to publish, [aggregateRootId={0},lastPublishedVersion={1},currentVersion={2}]", eventStream.AggregateRootId, lastPublishedVersion, eventStream.Version);
                        }
                        return canPublish;
                }
            });

            try
            {
                _actionExecutionService.TryAction("DispatchEventsToHandlers", dispatchEventsToHandlers, 5, new ActionInfo("DispatchEventsToHandlersCallback", obj =>
                {
                    var currentContext = obj as EventProcessingContext;
                    UpdatePublishedVersion(currentContext.EventStream);
                    currentContext.EventProcessContext.OnEventProcessed(currentContext.EventStream);
                    return true;
                }, context, null));
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Exception raised when dispatching event stream:{0}", context.EventStream), ex);
            }
        }