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