'How to stop listening in a Spring Kafka consumer?

I use Spring for Apache Kafka. I'd like to stop listening to my topic and wait to escape OOM. How can I do it?



Solution 1:[1]

Kafka provides an option to pause() and resume() the consumption from a topic. You can use these methods by implementing a wait in between to revive from the memory issue.

Reference: https://kafka.apache.org/0100/javadoc/index.html?org/apache/kafka/clients/consumer/KafkaConsumer.html

Solution 2:[2]

You can simply use these Guides to stop listening Kafka consumers.

1- kafka-util

2- Start/Stop Kafka Consumers using Spring Kafka

Solution 3:[3]

 public static void stopConsumer(final String topic) {    
     ConcurrentMessageListenerContainer<String, String> container 
         = consumersMap.get(topic);
     container.stop();    
 }

Solution 4:[4]

With Spring boot and Spring Cloud, there is a way to stop a particular consumer using actuators.

Kafka Streams binder of Spring Cloud allows us to start or stop a consumer or function binding associated with it.

Add management.endpoints.web.exposure.include = bindings in application.properties ( or yaml formatted property in application.yml)

A GET call to http://localhost:9009/actuator/bindings would expose all the bindings.

A POST call to http://localhost:9009/actuator/bindings/{name} can start or stop the binding.

Sample cURL to Stop a consumer : curl -d '{"state":"STOPPED"}' -H "Content-Type: application/json" -X POST http://localhost:8080/actuator/bindings/consumer-in-0

Sample cURL to Start a consumer : curl -d '{"state":"STARTED"}' -H "Content-Type: application/json" -X POST http://localhost:8080/actuator/bindings/consumer-in-0

Visit Spring Cloud Stream Binder Documentation for further details.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Archie
Solution 2
Solution 3 svdragster
Solution 4 BlackViper