# Consumer.poll() returns empty ConsumerRecords

**URL:** <https://forum.confluent.io/t/consumer-poll-returns-empty-consumerrecords/4364>\
**Category:** Java Clients\
**Created:** [5 March 2022 09:14 UTC](https://forum.confluent.io/t/consumer-poll-returns-empty-consumerrecords/4364 "2022-03-05T09:14:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aupres](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@aupres](https://forum.confluent.io/u/aupres)\
**Post date:** [5 March 2022 09:14 UTC](https://forum.confluent.io/t/consumer-poll-returns-empty-consumerrecords/4364/1 "2022-03-05T09:14:04Z")

</div>

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.

```auto
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

```auto
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?

---

<div class="post-metadata">

**Author:** ![rick](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/rick/32/49_2.png) [@rick](https://forum.confluent.io/u/rick)\
**Post date:** [5 March 2022 21:12 UTC](https://forum.confluent.io/t/consumer-poll-returns-empty-consumerrecords/4364/2 "2022-03-05T21:12:19Z")

</div>

@aupres This is the expected behavior of the [Consumer API poll function](https://kafka.apache.org/31/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html#poll(java.time.Duration)). 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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/confluentcommunity/original/1X/c49438c90c9df282e9996fdf6971be890c71b65a.svg) [@system](https://forum.confluent.io/u/system)\
**Post date:** [12 March 2022 21:13 UTC](https://forum.confluent.io/t/consumer-poll-returns-empty-consumerrecords/4364/3 "2022-03-12T21:13:12Z")

</div>

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