I created a C# Kafa client with Confluent.Kafka, and I have been trying to read the latest message only from two different topics, but it reads all messages, not just the latest one.
Here is my kafka client class, I'm not sure what I'm missing here.
public class KafkaClient
{
private readonly string _bootstrapServers;
private readonly string _clientId;
private readonly string _groupId;
private readonly Dictionary<string, string> _topicDictionary = new();
public KafkaClient(IOptions<KafkaSettings> kafkaSettings)
{
_bootstrapServers = kafkaSettings.Value.BootstrapServers;
_clientId = kafkaSettings.Value.ClientId;
_groupId = kafkaSettings.Value.GroupId;
_topicDictionary[TopicConstant.BetTopic] = kafkaSettings.Value.Topic.BetTopic;
_topicDictionary[TopicConstant.UserTopic] = kafkaSettings.Value.Topic.UserTopic;
_topicDictionary[TopicConstant.WalletTopic] = kafkaSettings.Value.Topic.WalletTopic;
}
public List<string> Consume(string[] topics, CancellationToken cancellation)
{
List<string> messages = new();
var config = new ConsumerConfig
{
BootstrapServers = _bootstrapServers,
GroupId = _groupId,
AutoOffsetReset = AutoOffsetReset.Latest,
EnableAutoCommit = true
};
using var consumer = new ConsumerBuilder<Ignore, string>(config).Build();
foreach (var topic in topics)
{
if (!_topicDictionary.TryGetValue(topic, out var topicValue))
throw new Exception($"{topic} Does not Exist");
consumer.Subscribe(topicValue);
var consumeResult = consumer.Consume(cancellation);
messages.Add(consumeResult.Message.Value);
consumer.Commit(consumeResult);
}
consumer.Close();
return messages;
}
}
Thank you