apache-kafkakafka-consumer-apispring-kafkaspring-scheduled

Is there any example of Spring Schedule that reads Kafka topic?


We're trying to read the data from Kafka at specified window time (so we have Kafka consumer), that means avoiding the data read at other times. However we're not sure how to shut down the consumer after the time period is expired. I wonder if there are any example of how to do that? Many thanks in advance for helping us.


Solution

  • You can disable the autoStartup and then manually start the kafka containers using KafkaListenerEndpointRegistry start and stop methods @KafkaListener Lifecycle Management

    public class KafkaConsumer {
    
     @Autowired
     private KafkaListenerEndpointRegistry registry;
    
      @KafkaListener(id = "myContainer", topics = "myTopic", autoStartup = "false")
      public void listen(...) { ... }
    
      @Schedule(cron = "")
      public void scheduledMethod() {
    
       registry.start();
    
       registry.stop()
      }
    

    But in the above approach there is no guarantee that all messages from kafka will be consumed in that time frame (It depends on load and processing speed)