Ejemplo n.º 1
0
        public IMessageSubscription Subscribe(string subscriberId, ISet <string> channels, MessageHandler handler)
        {
            var logContext = $"{nameof(Subscribe)} for subscriberId='{subscriberId}', " +
                             $"channels='{String.Join(",", channels)}', " +
                             $"handler='{handler.Method.Name}'";

            _logger.LogDebug($"+{logContext}");

            Action <SubscriberIdAndMessage, IServiceProvider> decoratedHandler = _decoratedMessageHandlerFactory.Decorate(handler);

            var swimLaneBasedDispatcher = new SwimlaneBasedDispatcher(subscriberId, _loggerFactory);

            EventuateKafkaConsumerMessageHandler kcHandler =
                (record, completionCallback) => swimLaneBasedDispatcher.Dispatch(ToMessage(record), record.Partition,
                                                                                 message => Handle(message, completionCallback, subscriberId, decoratedHandler));

            var kc = new EventuateKafkaConsumer(subscriberId,
                                                kcHandler,
                                                new List <string>(channels),
                                                _bootstrapServers,
                                                _eventuateKafkaConsumerConfigurationProperties,
                                                _loggerFactory);

            _consumers.Add(kc);

            kc.Start();

            _logger.LogDebug($"-{logContext}");
            return(new MessageSubscription(() =>
            {
                kc.Dispose();
                _consumers.Remove(kc);
            }));
        }
Ejemplo n.º 2
0
 public KafkaMessageProcessor(string subscriberId,
                              EventuateKafkaConsumerMessageHandler handler,
                              ILogger <KafkaMessageProcessor> logger)
 {
     _handler = handler;
     _logger  = logger;
     _loggingObjectContext = $"SubscriberId='{subscriberId}', handler='{handler.Method.Name}'";
     _offsetTracker        = new OffsetTracker(_logger);
 }
        public EventuateKafkaConsumer(string subscriberId,
                                      EventuateKafkaConsumerMessageHandler handler,
                                      IList <string> topics,
                                      string bootstrapServers,
                                      EventuateKafkaConsumerConfigurationProperties eventuateKafkaConsumerConfigurationProperties,
                                      ILoggerFactory loggerFactory)
        {
            _subscriberId  = subscriberId;
            _handler       = handler;
            _topics        = topics;
            _loggerFactory = loggerFactory;
            _logger        = loggerFactory.CreateLogger <EventuateKafkaConsumer>();

            _consumerProperties =
                ConsumerPropertiesFactory.MakeDefaultConsumerProperties(bootstrapServers, subscriberId)
                .ToDictionary(p => p.Key, p => p.Value);

            foreach (KeyValuePair <string, string> pair in eventuateKafkaConsumerConfigurationProperties.Properties)
            {
                _consumerProperties[pair.Key] = pair.Value;
            }
        }