示例#1
0
        public BoundedContextListener(
            BoundedContextListenerConfiguration configuration,
            IEventConverter eventConverter,
            IUncommittedEventStreamCoordinator uncommittedEventStreamCoordinator,
            ILogger logger,
            IApplicationResourceIdentifierConverter applicationResourceIdentifierConverter,
            IImplementationsOf <IEvent> eventTypes,
            IEventStore eventStore,
            IEventEnvelopes eventEnvelopes,
            IEventSequenceNumbers eventSequenceNumbers,
            IEventSourceVersions eventSourceVersions,
            ICommittedEventStreamBridge committedEventStreamBridge,
            IConsumer consumer)
        {
            _eventConverter = eventConverter;
            _uncommittedEventStreamCoordinator = uncommittedEventStreamCoordinator;
            _logger = logger;
            _applicationResourceIdentifierConverter = applicationResourceIdentifierConverter;
            _eventTypes                 = eventTypes;
            _eventSequenceNumbers       = eventSequenceNumbers;
            _eventStore                 = eventStore;
            _eventEnvelopes             = eventEnvelopes;
            _eventSourceVersions        = eventSourceVersions;
            _committedEventStreamBridge = committedEventStreamBridge;
            _consumer = consumer;

            _serializer    = new JsonSerializer();
            _configuration = configuration;
        }
 /// <summary>
 /// Initializes a new instance of <see cref="CommittedEventStreamReceiver"/>
 /// </summary>
 /// <param name="committedEventStreamBridge"><see cref="ICommittedEventStreamBridge"/> to use</param>
 /// <param name="logger"><see cref="ILogger"/> for logging</param>
 public CommittedEventStreamReceiver(ICommittedEventStreamBridge committedEventStreamBridge, ILogger logger)
 {
     committedEventStreamBridge.Received += (s) =>
     {
         logger.Trace("Committed event stream received");
         Received(s);
     };
 }
示例#3
0
 public CommittedEventStreamSender(
     IPublisher publisher,
     ICommittedEventStreamBridge bridge,
     IEventConverter eventConverter,
     ISerializer serializer,
     CommittedEventStreamSenderConfiguration senderConfiguration,
     ILogger logger)
 {
     _publisher           = publisher;
     _eventConverter      = eventConverter;
     _bridge              = bridge;
     _serializer          = serializer;
     _senderConfiguration = senderConfiguration;
     _logger              = logger;
 }
 /// <summary>
 /// Initializes a new instance of <see cref="CommittedEventStreamSender"/>
 /// </summary>
 public CommittedEventStreamSender(ICommittedEventStreamBridge bridge)
 {
     _bridge = bridge;
 }
        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");
                }
            };
        }
 /// <summary>
 /// Initializes a new instance of <see cref="CommittedEventStreamReceiver"/>
 /// </summary>
 /// <param name="committedEventStreamBridge"><see cref="ICommittedEventStreamBridge"/> to use</param>
 public CommittedEventStreamReceiver(ICommittedEventStreamBridge committedEventStreamBridge)
 {
     committedEventStreamBridge.Received += (s) => Received(s);
 }
 /// <summary>
 /// Initializes a new instance of <see cref="CommittedEventStreamSender"/>
 /// </summary>
 /// <param name="bridge"><see cref="ICommittedEventStreamBridge"/> to use for bridging</param>
 /// <param name="logger"><see cref="ILogger"/> for logging</param>
 public CommittedEventStreamSender(ICommittedEventStreamBridge bridge, ILogger logger)
 {
     _bridge = bridge;
     _logger = logger;
 }