2

I have to create a kafka consumer as a part of my application. But because of the use case I have to poll manually. Because whenever the poll is not returning any messages from topic i have to take certain steps. This i am not able to do using spring kafka as it only calls when a message is received.

Is there any way to run this consumer when the application starts? I was thinking of using CommandLineRunner but it doesn't seem to be clean(Are there any problems with this way of starting an infinite loop in a Spring Boot application?)

Or if i use spring kafka , is there any way to know if a particular partition or topic has not events

I could think of starting this in a separate thread or using CommandLineRunner. Any suggestions for a better solution?

1 Answers1

1

Simple solution. Don't use Spring-Kafka or @KafkaListener.

Make a plain KafkaConsumer object yourself. Call poll(). Check the count()

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Yes, that only i am doing.I have created a method which calls poll() . But that method needs to be called from somewhere and this method needs to be executing in the background all the time. There is not trigger to call this method but this method has to trigger something when topic is emply ie. poll() is returning empty recods – Sidhant Singh Mar 27 '20 at 21:10
  • I was simply answer what you posted initially. If you want a background job, make an ExecutorService or Thread class. Lookup "Observer Pattern". That isn't Kafka specific – OneCricketeer Mar 28 '20 at 00:57
  • As it's Spring Boot, use EnableScheduling annotation and mark the poll method as Scheduled. - it's what cricket_007 recommended but Spring handles the plumbing for you. – Chris Mar 29 '20 at 13:54
  • You can use Spring's `ConsumerFactory` to create the consumer. – Inego Feb 14 '22 at 05:01