Esempio n. 1
0
 public KafkaProvider(IOptions <KafkaOptions> options, ISemanticLog log)
 {
     producer =
         new ProducerBuilder <Null, Null>(options.Value)
         .SetLogHandler(KafkaLogFactory <Null, Null> .ProducerLog(log))
         .SetErrorHandler(KafkaLogFactory <Null, Null> .ProducerError(log))
         .SetStatisticsHandler(KafkaLogFactory <Null, Null> .ProducerStats(log))
         .Build();
 }
Esempio n. 2
0
        private void Consume(object?parameter)
        {
            var config = (ConsumerConfig)parameter !;

            var kafkaConsumer =
                new ConsumerBuilder <string, T>(config)
                .SetLogHandler(KafkaLogFactory <string, T> .ConsumerLog(log))
                .SetErrorHandler(KafkaLogFactory <string, T> .ConsumerError(log))
                .SetStatisticsHandler(KafkaLogFactory <string, T> .ConsumerStats(log))
                .SetValueDeserializer(new KafkaJsonSerializer <T>(serializer))
                .Build();

            using (kafkaConsumer)
            {
                kafkaConsumer.Subscribe(topicName);

                try
                {
                    while (!cancellationTokenSource.IsCancellationRequested)
                    {
                        try
                        {
                            var result = kafkaConsumer.Consume(cancellationTokenSource.Token);

                            consumer.HandleAsync(result.Message.Value, cancellationTokenSource.Token).Wait();
                        }
                        catch (OperationCanceledException)
                        {
                            return;
                        }
                        catch (JsonException ex)
                        {
                            log.LogError(ex, w => w
                                         .WriteProperty("action", "ConsumeMessage")
                                         .WriteProperty("system", "Kafka")
                                         .WriteProperty("status", "Failed")
                                         .WriteProperty("reason", "Failed to deserialize from JSON.")
                                         .WriteProperty("topic", topicName));
                        }
                        catch (Exception ex)
                        {
                            log.LogError(ex, w => w
                                         .WriteProperty("action", "ConsumeMessage")
                                         .WriteProperty("system", "Kafka")
                                         .WriteProperty("status", "Failed")
                                         .WriteProperty("topic", topicName));
                        }
                    }
                }
                finally
                {
                    kafkaConsumer.Close();
                }
            }
        }