0

net core console application. I have below implementation of kafka consumer

var config = new ConsumerConfig
{
    GroupId = groupId,
    BootstrapServers = brokerList,
    SaslMechanism = SaslMechanism.Plain,
    SaslUsername = saslUsername,
    SaslPassword = saslPassword,
    SecurityProtocol = SecurityProtocol.SaslSsl,
    AutoOffsetReset = AutoOffsetReset.Earliest
};
using (var consumer = new ConsumerBuilder<Ignore, string>(config)
  .SetErrorHandler((_, e) => logger.LogInformation($"Error: {e.Reason}"))
  .SetStatisticsHandler((_, json) => logger.LogInformation($"Statistic{json}"))
  .Build())
consumer.Subscribe(topic);

try
{
    while (true)
    {
        try
        {
            var consumeResult = consumer.Consume();

            if (consumeResult.IsPartitionEOF)
            {
                Console.WriteLine(
                    $"Reached end of topic {consumeResult.Topic}, partition {consumeResult.Partition}, offset {consumeResult.Offset}.");

                continue;
            }
            if (consumeResult?.Message == null) { return; }
            var mess = consumeResult.Message.Value;
            var vesselScoreFleetData = JsonConvert.DeserializeObject<VesselScoreFleet>(mess);
            vesselScoreFleets.Add(vesselScoreFleetData);
            logger.LogInformation($"Received message at {consumeResult.TopicPartitionOffset}: {consumeResult.Message.Value}");
            try
            {
                consumer.StoreOffset(consumeResult);
            }
            catch (KafkaException e)
            {
                logger.LogError($"Store Offset error: {e.Error.Reason}");
            }
        }
        catch (ConsumeException e)
        {
            logger.LogError($"Consume error: {e.Error.Reason}");
        }
    }
}
catch (OperationCanceledException)
{
    logger.LogError("Closing consumer.");
    consumer.Close();
}

using above code I am successfully able to consume events. but once my code read all the messages it stuck in the line var consumeResult = consumer.Consume();. My expectation is when there is no data to consume it should come out of loop and execute next steps in the sequence. May I know how to fix it? Any help would be appreciated. Thanks

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Niranjan
  • 63
  • 5
  • your while loop is always true so it won't stop. you would need to make a condition that gets updated to false once all messages have been consumed. Have you considered a foreach loop? This basically does exactly what you are looking for. – Roe Apr 25 '23 at 08:06
  • Thanks Roe. How would I know I have to make false or reached end of reading messages? – Niranjan Apr 25 '23 at 08:08
  • I think it would be wise if you followed a tutorial and read it thoroughly. It seems you are missing some core points. – Roe Apr 25 '23 at 08:26

0 Answers0