private void Consume() { while (!_cancellationToken.IsCancellationRequested) { try { var result = _innerConsumer.Consume(_cancellationToken); _consumed = true; _logger.LogTrace("Consuming message: {topic} {partition} @{offset}", result.Topic, result.Partition, result.Offset); Received?.Invoke(result.Message, result.TopicPartitionOffset); } catch (OperationCanceledException) { _logger.LogTrace("Consuming cancelled."); } catch (Exception ex) { _logger.LogCritical(ex, "Fatal error occurred consuming a message." + "The consumer will be stopped. See inner exception for details."); break; } } }
/// <summary> /// Consume Kafka event. /// </summary> /// <param name="cancellationToken">Cancellation token.</param> /// <returns>Kafka message.</returns> public Confluent.Kafka.Message <TKey, TValue> ConsumeEvent(CancellationToken cancellationToken = default) { var consumeResult = consumer.Consume(cancellationToken); if (consumeResult.Message != null) { logger?.LogInformation($"Message consumed: {consumeResult.Message.Key} {consumeResult.Message.Value}"); } return(consumeResult.Message); }
private async Task ReceiveMessage() { var result = _innerConsumer.Consume(_cancellationTokenSource.Token); if (result == null) { return; } if (result.IsPartitionEOF) { _logger.LogInformation("Partition EOF reached: {topic} {partition} @{offset}", result.Topic, result.Partition, result.Offset); return; } _consumedAtLeastOnce = true; _logger.LogTrace("Consuming message: {topic} {partition} @{offset}", result.Topic, result.Partition, result.Offset); if (Received != null) { await Received.Invoke(result.Message, result.TopicPartitionOffset); } }