Even Driven way to consume message from kafka consumer in java(without spring boot)

Hello all, i am trying to integrate kafka in our project , the project is in java (not in spring boot), Unlike Spring boot , as i understood we can consume based on the events. How can i achieve the same in java without using springboot,
As of now i implemented poll based mechanism, But i want , the consumer should only poll when there is a new message on the topic.

in Spring boot , where it works perfectly fine
@KafkaListener(topics = “${avro.topic.name}”, containerFactory = “kafkaListenerContainerFactory”, groupId = “foo”)
public void read(ConsumerRecord<String, FxAlarm> record,@Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition){

in java w/o spring boot
while (true) {
ConsumerRecords<Integer, Alarm> records = consumer.poll(Duration.ofMillis(200));
for(ConsumerRecord record: records) {
System.out.println("Received new record " + "key: "+ record.key()+"value: " + record.value()+ "topicname: "+ record.topic() + "partition id : " + record.partition() );
}
}
So whats the best way to avoid polling on a time interval , instead pull only when there is a event/message in topic

Hi @vivek2425,

The code you have here is how you want to consume from Kafka. Under the covers, Spring Boot is doing the same thing. You only need to provide a listener because when the consumer gets a record, it passes it along to registered listeners.

Kafka provides no callback mechanism, so polling for new records is necessary.

HTH

Bill