Search code examples
apache-kafkakafka-consumer-apispring-kafka

How to Gracefully Turn Off Running Kafka Consumer


I have a need to turn Kafka consumer on/off on the basis of some Database driven property. How can it be achieved.

one way that i have thought of is : throwing exception from consumer when consumer flag is turned off. and container factory config is defined as

factory.setErrorHandler(new SeekToCurrentErrorHandler());

But it actively seeks the same message.

is there any way to turn heart-beat off and then on back again on demand.


Solution

  • You can stop() and start() the listener container.

    It appears you are using @KafkaListener since you are using a container factory.

    In that case

    @KafkaListener(id = "foo" ...)
    

    and then use the KafkaListenerEndpointRegistry bean ...

    registry.getListenerContainer("foo").stop();