Ejemplo n.º 1
0
 public EnqueueMessageResult EnqueueMessage(ProcessingEvent processingEvent)
 {
     lock (_lockObj)
     {
         if (processingEvent.Message.Version == _nextExpectingEventVersion)
         {
             EnqueueEventStream(processingEvent);
             TryEnqueueWaitingMessage();
             LastActiveTime = DateTime.Now;
             TryRun();
             return(EnqueueMessageResult.Success);
         }
         else if (processingEvent.Message.Version > _nextExpectingEventVersion)
         {
             if (_waitingProcessingEventDict.TryAdd(processingEvent.Message.Version, processingEvent))
             {
                 _logger.WarnFormat("{0} later version of message arrived, added it to the waiting list, aggregateRootType: {1}, aggregateRootId: {2}, commandId: {3}, eventVersion: {4}, eventStreamId: {5}, eventTypes: {6}, eventIds: {7}, _nextExpectingEventVersion: {8}",
                                    GetType().Name,
                                    processingEvent.Message.AggregateRootTypeName,
                                    processingEvent.Message.AggregateRootId,
                                    processingEvent.Message.CommandId,
                                    processingEvent.Message.Version,
                                    processingEvent.Message.Id,
                                    string.Join("|", processingEvent.Message.Events.Select(x => x.GetType().Name)),
                                    string.Join("|", processingEvent.Message.Events.Select(x => x.Id)),
                                    _nextExpectingEventVersion
                                    );
             }
             return(EnqueueMessageResult.AddToWaitingList);
         }
         return(EnqueueMessageResult.Ignored);
     }
 }
Ejemplo n.º 2
0
        public bool EnqueueMessage(ProcessingEvent message)
        {
            lock (_lockObj)
            {
                var eventStream = message.Message;
                if (eventStream.Version == LatestHandledEventVersion + 1)
                {
                    message.MailBox = this;
                    _messageQueue.Enqueue(message);
                    _logger.DebugFormat("{0} enqueued new message, aggregateRootType: {1}, aggregateRootId: {2}, commandId: {3}, eventVersion: {4}, eventStreamId: {5}, eventTypes: {6}, eventIds: {7}",
                                        GetType().Name,
                                        eventStream.AggregateRootTypeName,
                                        eventStream.AggregateRootId,
                                        eventStream.CommandId,
                                        eventStream.Version,
                                        eventStream.Id,
                                        string.Join("|", eventStream.Events.Select(x => x.GetType().Name)),
                                        string.Join("|", eventStream.Events.Select(x => x.Id))
                                        );
                    LatestHandledEventVersion = eventStream.Version;

                    var nextVersion = eventStream.Version + 1;
                    while (_waitingMessageDict.TryRemove(nextVersion, out ProcessingEvent nextMessage))
                    {
                        var nextEventStream = nextMessage.Message;
                        nextMessage.MailBox = this;
                        _messageQueue.Enqueue(nextMessage);
                        LatestHandledEventVersion = nextEventStream.Version;
                        _logger.DebugFormat("{0} enqueued new message, aggregateRootType: {1}, aggregateRootId: {2}, commandId: {3}, eventVersion: {4}, eventStreamId: {5}, eventTypes: {6}, eventIds: {7}",
                                            GetType().Name,
                                            eventStream.AggregateRootTypeName,
                                            nextEventStream.AggregateRootId,
                                            nextEventStream.CommandId,
                                            nextEventStream.Version,
                                            nextEventStream.Id,
                                            string.Join("|", eventStream.Events.Select(x => x.GetType().Name)),
                                            string.Join("|", nextEventStream.Events.Select(x => x.Id))
                                            );
                        nextVersion++;
                    }

                    LastActiveTime = DateTime.Now;
                    TryRun();

                    return(true);
                }
                else if (eventStream.Version > LatestHandledEventVersion + 1)
                {
                    _waitingMessageDict.TryAdd(eventStream.Version, message);
                    return(true);
                }
                return(false);
            }
        }
Ejemplo n.º 3
0
        private void EnqueueEventStream(ProcessingEvent processingEvent)
        {
            lock (_lockObj)
            {
                processingEvent.MailBox = this;
                _processingEventQueue.Enqueue(processingEvent);
                _nextExpectingEventVersion = processingEvent.Message.Version + 1;

                _logger.InfoFormat("{0} enqueued new message: {1}", GetType().Name, _jsonSerializer.Serialize(processingEvent.Message));
            }
        }
Ejemplo n.º 4
0
 private void EnqueueEventStream(ProcessingEvent processingEvent)
 {
     lock (_lockObj)
     {
         processingEvent.MailBox = this;
         _processingEventQueue.Enqueue(processingEvent);
         _nextExpectingEventVersion = processingEvent.Message.Version + 1;
         if (_logger.IsDebugEnabled)
         {
             _logger.DebugFormat("{0} enqueued new message, aggregateRootType: {1}, aggregateRootId: {2}, commandId: {3}, eventVersion: {4}, eventStreamId: {5}, eventTypes: {6}, eventIds: {7}",
                                 GetType().Name,
                                 processingEvent.Message.AggregateRootTypeName,
                                 processingEvent.Message.AggregateRootId,
                                 processingEvent.Message.CommandId,
                                 processingEvent.Message.Version,
                                 processingEvent.Message.Id,
                                 string.Join("|", processingEvent.Message.Events.Select(x => x.GetType().Name)),
                                 string.Join("|", processingEvent.Message.Events.Select(x => x.Id))
                                 );
         }
     }
 }
Ejemplo n.º 5
0
 public EnqueueMessageResult EnqueueMessage(ProcessingEvent processingEvent)
 {
     lock (_lockObj)
     {
         if (IsRemoved)
         {
             throw new Exception(string.Format("ProcessingEventMailBox was removed, cannot allow to enqueue message, aggregateRootTypeName: {0}, aggregateRootId: {1}", AggregateRootTypeName, AggregateRootId));
         }
         if (_nextExpectingEventVersion == null || processingEvent.Message.Version > _nextExpectingEventVersion.Value)
         {
             if (_waitingProcessingEventDict.TryAdd(processingEvent.Message.Version, processingEvent))
             {
                 _logger.WarnFormat("{0} waiting message added, aggregateRootType: {1}, aggregateRootId: {2}, commandId: {3}, eventVersion: {4}, eventStreamId: {5}, eventTypes: {6}, eventIds: {7}, _nextExpectingEventVersion: {8}",
                                    GetType().Name,
                                    processingEvent.Message.AggregateRootTypeName,
                                    processingEvent.Message.AggregateRootId,
                                    processingEvent.Message.CommandId,
                                    processingEvent.Message.Version,
                                    processingEvent.Message.Id,
                                    string.Join("|", processingEvent.Message.Events.Select(x => x.GetType().Name)),
                                    string.Join("|", processingEvent.Message.Events.Select(x => x.Id)),
                                    _nextExpectingEventVersion
                                    );
             }
             return(EnqueueMessageResult.AddToWaitingList);
         }
         else if (processingEvent.Message.Version == _nextExpectingEventVersion)
         {
             EnqueueEventStream(processingEvent);
             TryEnqueueValidWaitingMessage();
             LastActiveTime = DateTime.Now;
             TryRun();
             return(EnqueueMessageResult.Success);
         }
         return(EnqueueMessageResult.Ignored);
     }
 }