title: Spring Boot Job scheduler with Kafka consumer
body: <p>I am working on a POC where I want to consume messages from a Kafka topic &quot;users&quot;. 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.</p>
  <p>I have tried the below to achieve this though I am struggling to identify how to call <strong>consume(String message) method</strong> from my scheduler method <strong>schedularMsgConsumeKakfa</strong> 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.</p>
  <pre><code>@Configuration
  @Service
  public class SchedularMsgConsumeKafkaController2 {
  
      @Autowired
      private KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry;
  
      @KafkaListener(topics = &quot;users&quot;, id = &quot;full-part-id&quot;, containerGroup = &quot;full-part-group&quot;, autoStartup = &quot;false&quot;)
      public void consume(String message) throws IOException, InterruptedException {
          System.out.println(String.format(&quot;#### -&gt; Consumed message -&gt; %s&quot;, message));
          System.out.println(String.format(&quot;Really happy&quot;));
          MessageListenerContainer listenerContainer = kafkaListenerEndpointRegistry.getListenerContainer(&quot;full-part-id&quot;);
          listenerContainer.stop();
      }
  
      @Scheduled(fixedDelay = 30000, initialDelay = 15000)
      public void schedularMsgConsumeKakfa() throws Exception {
          MessageListenerContainer listenerContainer = kafkaListenerEndpointRegistry.getListenerContainer(&quot;full-part-id&quot;);
          listenerContainer.start();
      }
  
  }
  </code></pre>