public void Send(CommittedEventStream committedEventStream) { if (_senderConfiguration.Topics.Count() == 0) { return; } _logger.Information("Sending committed event stream"); _bridge.Send(committedEventStream); var eventContentAndEnvelopes = _eventConverter.Convert(committedEventStream); var json = _serializer.ToJson(eventContentAndEnvelopes); _logger.Trace("Sending JSON : " + json); _senderConfiguration.Topics.ForEach(topic => { _logger.Information($"Send committed event stream to topic: '{topic}'"); _publisher.Publish(topic, json); }); }
void Received(Topic topic, string eventAsJson) { try { _logger.Trace($"Message received 'eventAsJson'"); dynamic raw = JsonConvert.DeserializeObject(eventAsJson); foreach (var rawContentAndEnvelope in raw) { var eventSourceId = (EventSourceId)Guid.Parse(rawContentAndEnvelope.Envelope.EventSourceId.ToString()); var eventIdentifier = _applicationResourceIdentifierConverter.FromString(rawContentAndEnvelope.Envelope.Event.ToString()); var version = EventSourceVersion.FromCombined((double)rawContentAndEnvelope.Envelope.Version); var correlationId = (TransactionCorrelationId)Guid.Parse(rawContentAndEnvelope.Envelope.CorrelationId.ToString()); var eventSource = new ExternalSource(eventSourceId); CorrelationId = correlationId; _logger.Trace($"Received event of with resource name '{eventIdentifier.Resource.Name}' from '{eventSourceId}' with version '{version}' in correlation '{correlationId}'"); var eventType = _eventTypes.SingleOrDefault(et => et.Name == eventIdentifier.Resource.Name); if (eventType != null) { _logger.Trace("Matching Event Type : " + eventType.AssemblyQualifiedName); var @event = GetEventFrom(rawContentAndEnvelope, eventSourceId, eventType); var uncommittedEventStream = new UncommittedEventStream(eventSource); uncommittedEventStream.Append(@event, version); _logger.Information($"Committing uncommitted event stream with correlationId '{correlationId}'"); var envelopes = _eventEnvelopes.CreateFrom(eventSource, uncommittedEventStream.EventsAndVersion); var envelopesAsArray = envelopes.ToArray(); var eventsAsArray = uncommittedEventStream.ToArray(); _logger.Trace("Create an array of events and envelopes"); var eventsAndEnvelopes = new List <EventAndEnvelope>(); for (var eventIndex = 0; eventIndex < eventsAsArray.Length; eventIndex++) { var envelope = envelopesAsArray[eventIndex]; var currentEvent = eventsAsArray[eventIndex]; eventsAndEnvelopes.Add(new EventAndEnvelope( envelope .WithTransactionCorrelationId(correlationId) .WithSequenceNumber(_eventSequenceNumbers.Next()) .WithSequenceNumberForEventType(_eventSequenceNumbers.NextForType(envelope.Event)), currentEvent )); } _logger.Trace("Committing events to event store"); _eventStore.Commit(eventsAndEnvelopes); _logger.Trace($"Set event source versions for the event source '{envelopesAsArray[0].EventSource}' with id '{envelopesAsArray[0].EventSourceId}'"); _eventSourceVersions.SetFor(envelopesAsArray[0].EventSource, envelopesAsArray[0].EventSourceId, envelopesAsArray[envelopesAsArray.Length - 1].Version); _logger.Trace("Create a committed event stream"); var committedEventStream = new CommittedEventStream(uncommittedEventStream.EventSourceId, eventsAndEnvelopes); _committedEventStreamBridge.Send(committedEventStream); CorrelationId = Guid.Empty; } } } catch (Exception ex) { _logger.Error(ex, "Error during receiving"); } }
/// <inheritdoc/> public void Send(CommittedEventStream committedEventStream) { _bridge.Send(committedEventStream); }
public BoundedContextListener( KafkaConnectionString connectionString, ListenerConfiguration configuration, IEventConverter eventConverter, IUncommittedEventStreamCoordinator uncommittedEventStreamCoordinator, ISerializer serializer, ILogger logger, IApplicationResourceIdentifierConverter applicationResourceIdentifierConverter, IImplementationsOf <IEvent> eventTypes, IEventStore eventStore, IEventEnvelopes eventEnvelopes, IEventSequenceNumbers eventSequenceNumbers, IEventSourceVersions eventSourceVersions, ICommittedEventStreamBridge committedEventStreamBridge) { _serializer = serializer; _eventConverter = eventConverter; _uncommittedEventStreamCoordinator = uncommittedEventStreamCoordinator; _logger = logger; _applicationResourceIdentifierConverter = applicationResourceIdentifierConverter; _eventTypes = eventTypes; _eventSequenceNumbers = eventSequenceNumbers; _eventStore = eventStore; _eventEnvelopes = eventEnvelopes; _eventSourceVersions = eventSourceVersions; _committedEventStreamBridge = committedEventStreamBridge; logger.Information($"Listening on topic '{configuration.Topic}' from '{connectionString}'"); var config = new Dictionary <string, object> { { "bootstrap.servers", connectionString }, { "group.id", "simple-consumer" }, { "enable.auto.commit", true }, { "auto.commit.interval.ms", 1000 }, { "default.topic.config", new Dictionary <string, object>() { { "auto.offset.reset", "smallest" } } } }; _consumer = new Consumer <Ignore, string>(config, null, new StringDeserializer(Encoding.UTF8)); _consumer.Assign(new [] { new TopicPartition(configuration.Topic, 0) }); _consumer.OnMessage += (_, msg) => { try { logger.Trace($"Message received '{msg.Value}'"); var raw = _serializer.FromJson <dynamic[]>(msg.Value); foreach (var rawContentAndEnvelope in raw) { var eventSourceId = (EventSourceId)Guid.Parse(rawContentAndEnvelope.Content.EventSourceId.ToString()); var eventIdentifier = _applicationResourceIdentifierConverter.FromString(rawContentAndEnvelope.Envelope.Event.ToString()); var version = EventSourceVersion.FromCombined((double)rawContentAndEnvelope.Envelope.Version); var correlationId = (TransactionCorrelationId)Guid.Parse(rawContentAndEnvelope.Envelope.CorrelationId.ToString()); CorrelationId = correlationId; _logger.Trace($"Received event of with resource name '{eventIdentifier.Resource.Name}' from '{eventSourceId}' with version '{version}' in correlation '{correlationId}'"); var eventType = _eventTypes.SingleOrDefault(et => et.Name == eventIdentifier.Resource.Name); if (eventType != null) { _logger.Trace("Matching Event Type : " + eventType.AssemblyQualifiedName); var @event = Activator.CreateInstance(eventType, eventSourceId) as IEvent; _serializer.FromJson(@event, rawContentAndEnvelope.Content.ToString()); var eventSource = new ExternalSource(eventSourceId); var uncommittedEventStream = new UncommittedEventStream(eventSource); uncommittedEventStream.Append(@event, version); _logger.Information($"Committing uncommitted event stream with correlationId '{correlationId}'"); var envelopes = _eventEnvelopes.CreateFrom(eventSource, uncommittedEventStream.EventsAndVersion); var envelopesAsArray = envelopes.ToArray(); var eventsAsArray = uncommittedEventStream.ToArray(); _logger.Trace("Create an array of events and envelopes"); var eventsAndEnvelopes = new List <EventAndEnvelope>(); for (var eventIndex = 0; eventIndex < eventsAsArray.Length; eventIndex++) { var envelope = envelopesAsArray[eventIndex]; var currentEvent = eventsAsArray[eventIndex]; eventsAndEnvelopes.Add(new EventAndEnvelope( envelope .WithTransactionCorrelationId(correlationId) .WithSequenceNumber(_eventSequenceNumbers.Next()) .WithSequenceNumberForEventType(_eventSequenceNumbers.NextForType(envelope.Event)), currentEvent )); } _logger.Trace("Committing events to event store"); _eventStore.Commit(eventsAndEnvelopes); _logger.Trace($"Set event source versions for the event source '{envelopesAsArray[0].EventSource}' with id '{envelopesAsArray[0].EventSourceId}'"); _eventSourceVersions.SetFor(envelopesAsArray[0].EventSource, envelopesAsArray[0].EventSourceId, envelopesAsArray[envelopesAsArray.Length - 1].Version); _logger.Trace("Create a committed event stream"); var committedEventStream = new CommittedEventStream(uncommittedEventStream.EventSourceId, eventsAndEnvelopes); _committedEventStreamBridge.Send(committedEventStream); CorrelationId = Guid.Empty; } } } catch (Exception ex) { _logger.Error(ex, "Error during receiving"); } }; }
/// <inheritdoc/> public void Send(CommittedEventStream committedEventStream) { _logger.Trace("Sending committed event stream"); _bridge.Send(committedEventStream); }