I am working on a POC where I want to consume messages from a Kafka topic "users". Trying to achieve that consumer should read message from Kafka topic once spring boot scheduler triggers on the scheduled time or cron time then we should start consuming the existing messages one by one from the kafka topic and process those and when all the messages consumed then kafka consumer should stop. scheduler should trigger on the cron time and start the process again.
I have tried the below to achieve this though I am struggling to identify how to call consume(String message) method from my scheduler method schedularMsgConsumeKakfa and any example where we have better structure to consume messages from Kafka in the spring boot scheduler and write kafka consumer method in the class implementing tasklet interface etc. appreciating for any suggestions.
@Configuration
@Service
public class SchedularMsgConsumeKafkaController2 {
@Autowired
private KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry;
@KafkaListener(topics = "users", id = "full-part-id", containerGroup = "full-part-group", autoStartup = "false")
public void consume(String message) throws IOException, InterruptedException {
System.out.println(String.format("#### -> Consumed message -> %s", message));
System.out.println(String.format("Really happy"));
MessageListenerContainer listenerContainer = kafkaListenerEndpointRegistry.getListenerContainer("full-part-id");
listenerContainer.stop();
}
@Scheduled(fixedDelay = 30000, initialDelay = 15000)
public void schedularMsgConsumeKakfa() throws Exception {
MessageListenerContainer listenerContainer = kafkaListenerEndpointRegistry.getListenerContainer("full-part-id");
listenerContainer.start();
}
}