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("", 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)));

And Consumer basic codes are like below

Properties props = new Properties();
props.setProperty("bootstrap.servers", "localhost:9092");
props.setProperty("", "my-topic");
props.setProperty("auto.offset.reset", "earliest");
props.setProperty("", "true");
props.setProperty("", "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);
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 {

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 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.