Ejemplo n.º 1
0
        internal KafkaConsumer(IKafkaOptions kafkaOptions, ILogger <KafkaConsumer <TKey, TValue> > logger)
        {
            _kafkaOptions = Guard.IsNotNull(kafkaOptions, nameof(kafkaOptions));
            _logger       = Guard.IsNotNull(logger, nameof(logger));

            var config = new ConsumerConfig(_kafkaOptions.AdditionalConfiguration)
            {
                BootstrapServers = _kafkaOptions.BootstrapServers,
                GroupId          = _kafkaOptions.GroupId,
                AutoOffsetReset  = _kafkaOptions.AutoOffsetReset,
                EnableAutoCommit = _kafkaOptions.EnableAutoCommit,
                SaslUsername     = _kafkaOptions.SaslUsername,
                SaslPassword     = _kafkaOptions.SaslPassword,
                SecurityProtocol = _kafkaOptions.SecurityProtocol,
                SaslMechanism    = _kafkaOptions.SaslMechanism
            };

            _consumer = new ConsumerBuilder <TKey, TValue>(config)
                        .SetValueDeserializer(new JsonDeserializer <TValue>())
                        .SetErrorHandler((_, e) => _logger.LogError(e.Reason))
                        .SetLogHandler((_, e) => _logger.LogInformation(e.Message))
                        .SetPartitionsAssignedHandler((_, e) => _logger.LogDebug($"Assigned partitions: [{string.Join(", ", e.Select(x => x.Partition))}]"))
                        .SetPartitionsRevokedHandler((_, e) => _logger.LogDebug($"Revoked partitions: [{string.Join(", ", e.Select(x => x.Partition))}]"))
                        .Build();
        }
Ejemplo n.º 2
0
        public IKafkaConsumer <TKey, TValue> Create <TKey, TValue>(IKafkaOptions kafkaOptions, string topic)
        {
            var consumer = new KafkaConsumer <TKey, TValue>(kafkaOptions, _loggerFactory.CreateLogger <KafkaConsumer <TKey, TValue> >())
            {
                Topic = topic
            };

            return(consumer);
        }
Ejemplo n.º 3
0
        internal KafkaProducer(IKafkaOptions kafkaOptions, ILogger <KafkaProducer <TKey, TValue> > logger)
        {
            _kafkaOptions = Guard.IsNotNull(kafkaOptions, nameof(kafkaOptions));
            _logger       = Guard.IsNotNull(logger, nameof(logger));

            var config = new ProducerConfig(_kafkaOptions.AdditionalConfiguration)
            {
                BootstrapServers = _kafkaOptions.BootstrapServers,
                MessageTimeoutMs = _kafkaOptions.MessageTimeoutMs,
                SaslUsername     = _kafkaOptions.SaslUsername,
                SaslPassword     = _kafkaOptions.SaslPassword,
                SecurityProtocol = _kafkaOptions.SecurityProtocol,
                SaslMechanism    = _kafkaOptions.SaslMechanism
            };

            _producer = new ProducerBuilder <TKey, TValue>(config)
                        .SetValueSerializer(new JsonSerializer <TValue>())
                        .SetErrorHandler((_, e) => _logger.LogError(e.Reason))
                        .SetLogHandler((_, e) => _logger.LogInformation(e.Message))
                        .Build();
        }