Consumer.poll() returns empty ConsumerRecords

I install kafka on windows 11 and it works successfully.
Below codes are simple and basic java client codes of Kafka.
First, Kafka producer java codes.

Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("linger.ms", 1);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

Producer<String, String> producer = new KafkaProducer<>(props);
for (int i = 0; i < 100; i++) {
	producer.send(new ProducerRecord<String, String>("my-topic", Integer.toString(i), Integer.toString(i)));
}
producer.close();

And Consumer basic codes are like below

Properties props = new Properties();
props.setProperty("bootstrap.servers", "localhost:9092");
props.setProperty("group.id", "my-topic");
props.setProperty("auto.offset.reset", "earliest");
props.setProperty("enable.auto.commit", "true");
props.setProperty("auto.commit.interval.ms", "1000");
props.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
		
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList("my-topic"));
		
try {
	while (true) {
		ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(0));
				
		if(records.isEmpty()) {
			System.out.println("It is Empty!!");
		} else {
			for (ConsumerRecord<String, String> record : records)
				System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
		}
	}
} finally {
	consumer.close();
}

Kafka producer send data successfully. But the consumer.poll method returns empty ConsumerRecords. I can not understand why poll method returns no data. As you see these codes are simple and basic. Any idea?

@aupres This is the expected behavior of the Consumer API poll function. The consumer polls for new records, and the return value is null if there are no new records to consume. You are passing the poll function a timeout of 0, which means the consumer is running in a very tight loop. All you see in the output is a stream of “It is Empty!!”, however, I tested the code and it does consume the records from the producer, you just can’t see them as the terminal is flooded with the “empty case”. Comment out the records.isEmpty case, and give your consumer a unique group.id value and you will see the records on the console. I would suggest you add a non-zero value to the poll function timeout so that you consumer is not spinning on the empty case instead it waits a bit for records to arrive before iterating on the loop.

Hope this helps.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.