Search code examples
springspring-bootapache-kafkaspring-kafka

How can I pause the polling behavior of Kafka Consumers?


I want to stop polling for a specific topic at a specific time.

  • spring-boot 2.X
  • spring-kafka 2.5.5
  • Kafka version 2.5.1

For example, even if a message comes in to the TEST topic partition, the message is piled up in the partition from 00 to 01, and there is no consumption.

After 01 o'clock, I want to consume the message again on the TEST topic.

How to pause and resume?


Solution

  • Use the KafkaListenerEndpointRegistry bean to control the lifecycle of the listener containers; you can stop and start them according to whatever conditions you desire.

    You can also configure them to be stopped until you explicitly start them.

    See https://docs.spring.io/spring-kafka/docs/current/reference/html/#kafkalistener-lifecycle.

    @KafkaListener(id = "myContainer", topics = "myTopic", autoStartup = "false")
    public void listen(...) { ... }
    
    

    and

    @Autowired
    private KafkaListenerEndpointRegistry registry;
    
    ...
    
        this.registry.getListenerContainer("myContainer").start();
    
    ...