How to check if Kafka Consumer is ready

Nagireddy Hanisha picture Nagireddy Hanisha · Jan 3, 2018 · Viewed 8.5k times · Source

I have Kafka commit policy set to latest and missing first few messages. If I give a sleep of 20 seconds before starting to send the messages to the input topic, everything is working as desired. I am not sure if the problem is with consumer taking long time for partition rebalancing. Is there a way to know if the consumer is ready before starting to poll ?

Answer

donm picture donm · Jan 10, 2018
  • You can use consumer.assignment(), it will return set of partitions and verify whether all of the partitions are assigned which are available for that topic.

  • If you are using spring-kafka project, you can include spring-kafka-test dependancy and use below method to wait for topic assignment , but you need to have container. ContainerTestUtils.waitForAssignment(Object container, int partitions);