0

I'm trying to create a simple Kafka consumer in c# using the below code

private static CancellationTokenSource StartConsumer(IAdminClient client, string topicName)
{
    ConsumerConfig config = new()
    {
        BootstrapServers = BootstrapServers,
        GroupId = "testConsumerGroup",
        AutoOffsetReset = AutoOffsetReset.Earliest,

    };
    IConsumer<Null, string> consumer = new ConsumerBuilder<Null, string>(config).Build();
    //consumer.Assign(new TopicPartitionOffset(topicName, 0, Offset.Beginning));//this works
    consumer.Subscribe(topicName);//this doesn't work

    CancellationTokenSource cancellationTokenSource = new();
    CancellationToken cancellationToken = cancellationTokenSource.Token;
    Task.Run(() =>
    {
        while (!cancellationToken.IsCancellationRequested)
        {
            ConsumeResult<Null, string> consumeResult = consumer.Consume();
            Console.WriteLine($"{consumeResult.Offset}: {consumeResult.Message.Value}");
        }
        consumer.Close();
        consumer.Dispose();
    });

    return cancellationTokenSource;
}

When I use the Assign method, my consumer is able to consume the messages just fine. But when I try to use the Subscribe method, my consumer is not able to consume any messages. The method consumer.Consume() never returns.

When I tried to debugging I found that after calling the consumer.Subscribe(topicName) the consumer.Assignment list is empty. Based on this https://github.com/confluentinc/confluent-kafka-dotnet/issues/278 my guess is that for some reason the coordinator is not assigning any partition to my consumer.

I'm creating the topic like this

private static async Task<string> CreateTopic(IAdminClient client, string topicName)
{
    await client.CreateTopicsAsync(new TopicSpecification[]
    {
        new TopicSpecification()
        {
            Name = topicName,
            ReplicationFactor = 1,
            NumPartitions = 1
        }
    });
    return topicName;
}

System Details

  • OS = Windows 10
  • Kafka Version = 3.4.0
  • Java = jdk 1.8.0_202 (32 bit)
  • Confluent.Kafka NuGet = 2.0.2

1 Answers1

0

After hours of trial and error finally I was able to resolve the issue. The problem was Java. I previously had 32bit version, when I changed it to 64bit version of Java 8 from OpenJDK it resolved the issue. Exact version: 8u362-b09.