private bool TryGetFromSnapshot(string aggregateRootId, Type aggregateRootType, out IAggregateRoot aggregateRoot) { aggregateRoot = null; var snapshot = _snapshotStore.GetLastestSnapshot(aggregateRootId, aggregateRootType); if (snapshot == null) { return(false); } aggregateRoot = _snapshotter.RestoreFromSnapshot(snapshot); if (aggregateRoot == null) { return(false); } if (aggregateRoot.UniqueId != aggregateRootId) { throw new Exception(string.Format("Aggregate root restored from snapshot not valid as the aggregate root id not matched. Snapshot aggregate root id:{0}, expected aggregate root id:{1}", aggregateRoot.UniqueId, aggregateRootId)); } var aggregateRootTypeCode = _aggregateRootTypeCodeProvider.GetTypeCode(aggregateRootType); var eventStreamsAfterSnapshot = _eventStore.QueryAggregateEvents(aggregateRootId, aggregateRootTypeCode, snapshot.Version + 1, int.MaxValue); aggregateRoot.ReplayEvents(eventStreamsAfterSnapshot); return(true); }
public DisptachingMessage(IMessage message, DisptachingMessageStream parentMessageStream, IEnumerable <IMessageHandlerProxy> handlers, ITypeCodeProvider typeCodeProvider) { Message = message; _parentMessageStream = parentMessageStream; _handlerDict = new ConcurrentDictionary <int, IMessageHandlerProxy>(); handlers.ForEach(x => _handlerDict.TryAdd(typeCodeProvider.GetTypeCode(x.GetInnerHandler().GetType()), x)); }
private EQueueMessage CreateEQueueMessage(IApplicationMessage message) { var messageTypeCode = _messageTypeCodeProvider.GetTypeCode(message.GetType()); var topic = _messageTopicProvider.GetTopic(message); var data = _jsonSerializer.Serialize(message); return(new EQueueMessage(topic, messageTypeCode, Encoding.UTF8.GetBytes(data))); }
/// <summary> /// 处理当前消息。 /// </summary> protected void ProcessHandler(Type messageType, IMessage message, IProxyHandler messageHandler) { var messageHandlerType = messageHandler.GetInnerHandler().GetType(); try { if (message is EventStream) { messageHandler.Handle(message); } else { var messageHandlerTypeCode = _typeCodeProvider.GetTypeCode(messageHandlerType); var messageTypeCode = _typeCodeProvider.GetTypeCode(messageType); var messageHandlerInfo = new HandlerRecordData(message.Id, messageHandlerTypeCode, messageTypeCode); if (!_handlerStore.IsHandlerInfoExist(messageHandlerInfo)) { messageHandler.Handle(message); _handlerStore.AddHandlerInfo(messageHandlerInfo); } _logger.Debug("Handle message success. messageHandlerType:{0}, messageType:{1}, messageId:{2}", messageHandlerType.FullName, messageType.FullName, message.Id); } } catch (Exception ex) { if (!(message is EventStream)) { string errorMessage = string.Format("Exception raised when {0} handling {1}. message info:{2}.", messageHandlerType.FullName, messageType.FullName, message.ToString()); _logger.Error(ex, errorMessage); } throw ex; } finally { if (messageHandler is IDisposable) { ((IDisposable)messageHandler).Dispose(); } } }
private DomainEventStream BuildDomainEventStream(IAggregateRoot aggregateRoot, IEnumerable <IDomainEvent> changedEvents, ProcessingCommand processingCommand) { return(new DomainEventStream( processingCommand.Message.Id, aggregateRoot.UniqueId, _aggregateRootTypeProvider.GetTypeCode(aggregateRoot.GetType()), aggregateRoot.Version + 1, DateTime.Now, changedEvents, processingCommand.Items)); }
public Snapshot CreateSnapshot(IAggregateRoot aggregateRoot) { if (aggregateRoot == null) { throw new ArgumentNullException("aggregateRoot"); } var payload = _binarySerializer.Serialize(aggregateRoot); var aggregateRootTypeCode = _aggregateRootTypeCodeProvider.GetTypeCode(aggregateRoot.GetType()); return(new Snapshot(aggregateRootTypeCode, aggregateRoot.UniqueId, aggregateRoot.Version, payload, DateTime.Now)); }
public IAggregateRoot Get(Type aggregateRootType, string aggregateRootId) { if (aggregateRootId == null) { throw new ArgumentNullException("aggregateRootId"); } var aggregateRoot = default(IAggregateRoot); if (TryGetFromSnapshot(aggregateRootId, aggregateRootType, out aggregateRoot)) { return(aggregateRoot); } var aggregateRootTypeCode = _aggregateRootTypeCodeProvider.GetTypeCode(aggregateRootType); var eventStreams = _eventStore.QueryAggregateEvents(aggregateRootId, aggregateRootTypeCode, minVersion, maxVersion); aggregateRoot = RebuildAggregateRoot(aggregateRootType, eventStreams); return(aggregateRoot); }
public IDictionary <int, string> Serialize(IEnumerable <IDomainEvent> evnts) { var dict = new Dictionary <int, string>(); foreach (var evnt in evnts) { var typeCode = _typeCodeProvider.GetTypeCode(evnt.GetType()); var eventData = _jsonSerializer.Serialize(evnt); dict.Add(typeCode, eventData); } return(dict); }
private EQueueMessage BuildCommandMessage(ICommand command) { var commandData = _jsonSerializer.Serialize(command); var topic = _commandTopicProvider.GetTopic(command); var commandTypeCode = _commandTypeCodeProvider.GetTypeCode(command.GetType()); var replyAddress = _commandResultProcessor != null?_commandResultProcessor.BindingAddress.ToString() : null; var messageData = _jsonSerializer.Serialize(new CommandMessage { CommandTypeCode = commandTypeCode, CommandData = commandData, ReplyAddress = replyAddress }); return(new EQueueMessage(topic, (int)EQueueMessageTypeCode.CommandMessage, Encoding.UTF8.GetBytes(messageData))); }
private EQueueMessage BuildCommandMessage(ICommand command) { var commandData = _jsonSerializer.Serialize(command); var topic = _commandTopicProvider.GetTopic(command); var commandTypeCode = _commandTypeCodeProvider.GetTypeCode(command.GetType()); var commandExecutedMessageTopic = _commandResultProcessor != null ? _commandResultProcessor.CommandExecutedMessageTopic : CommandExecutedMessageTopic; var domainEventHandledMessageTopic = _commandResultProcessor != null ? _commandResultProcessor.DomainEventHandledMessageTopic : DomainEventHandledMessageTopic; var messageData = _jsonSerializer.Serialize(new CommandMessage { CommandTypeCode = commandTypeCode, CommandData = commandData, CommandExecutedMessageTopic = commandExecutedMessageTopic, DomainEventHandledMessageTopic = domainEventHandledMessageTopic }); return(new EQueueMessage(topic, (int)EQueueMessageTypeCode.CommandMessage, Encoding.UTF8.GetBytes(messageData))); }
private EQueueMessage CreateEQueueMessage(IPublishableException exception) { var exceptionTypeCode = _exceptionTypeCodeProvider.GetTypeCode(exception.GetType()); var topic = _exceptionTopicProvider.GetTopic(exception); var serializableInfo = new Dictionary <string, string>(); exception.SerializeTo(serializableInfo); var sequenceMessage = exception as ISequenceMessage; var data = _jsonSerializer.Serialize(new PublishableExceptionMessage { UniqueId = exception.Id, AggregateRootTypeCode = sequenceMessage != null ? sequenceMessage.AggregateRootTypeCode : 0, AggregateRootId = sequenceMessage != null ? sequenceMessage.AggregateRootId : null, Timestamp = exception.Timestamp, ExceptionTypeCode = exceptionTypeCode, SerializableInfo = serializableInfo }); return(new EQueueMessage(topic, (int)EQueueMessageTypeCode.ExceptionMessage, Encoding.UTF8.GetBytes(data))); }
public DisptachingMessage(IMessage message, DisptachingMessageStream parentMessageStream, IEnumerable<IMessageHandlerProxy> handlers, ITypeCodeProvider typeCodeProvider) { Message = message; _parentMessageStream = parentMessageStream; _handlerDict = new ConcurrentDictionary<int, IMessageHandlerProxy>(); handlers.ForEach(x => _handlerDict.TryAdd(typeCodeProvider.GetTypeCode(x.GetInnerHandler().GetType()), x)); }
/// <summary> /// 保存聚合事件。 /// </summary> public void Save(IEventSourced aggregateRoot, string correlationId = null) { var events = aggregateRoot.GetEvents(); if (events.IsEmpty()) { return; } var aggregateRootType = aggregateRoot.GetType(); int aggregateRootTypeCode = _typeCodeProvider.GetTypeCode(aggregateRootType); string aggregateRootId = aggregateRoot.Id.ToString(); if (!_eventStore.IsExist(correlationId)) { var sourcedEvents = events.Select(@event => new EventData() { AggregateRootId = aggregateRootId, AggregateRootTypeCode = aggregateRootTypeCode, Version = @event.Version, CorrelationId = correlationId, Payload = _serializer.Serialize(@event) }).ToArray(); _eventStore.Append(sourcedEvents); _logger.Info("sourcing events persistent completed. aggregateRootId:{0},aggregateRootType:{1}.", aggregateRootId, aggregateRootType.FullName); _cache.Set(aggregateRoot, aggregateRoot.Id); } else { events = _eventStore.FindAll(correlationId).Select(Deserialize); _logger.Info("the command generates events have been saved, load from storage. command id:{0}", correlationId); } _eventBus.Publish(new EventStream { AggregateRootId = aggregateRootId, AggregateRootTypeCode = aggregateRootTypeCode, CommandId = correlationId, StartVersion = events.Min(item => item.Version), EndVersion = events.Max(item => item.Version), Events = events.OfType <IEvent>().ToArray() }); _logger.Info("publish all events. event ids: [{0}]", string.Join(",", events.Select(@event => @event.Id).ToArray())); if (_snapshotStore.StorageEnabled) { var snapshot = new SnapshotData(aggregateRootTypeCode, aggregateRootId) { Data = _serializer.Serialize(aggregateRoot), Version = aggregateRoot.Version }; _snapshotStore.Save(snapshot).ContinueWith(task => { if (task.Status == TaskStatus.Faulted) { _logger.Error(task.Exception, "snapshot persistent failed. aggregateRootId:{0},aggregateRootType:{1},version:{2}.", aggregateRootId, aggregateRootType.FullName, aggregateRoot.Version); } }); //Task.Factory.StartNew(() => { // try { // _snapshotStore.Save(snapshot); // } // catch (Exception ex) { // _logger.Error(ex, "snapshot persistent failed. aggregateRootId:{0},aggregateRootType:{1},version:{2}.", aggregateRootId, aggregateRootType.FullName, aggregateRoot.Version); // } //}); } }