How can i stop my kafka consumer from consuming messages ?

I am using below functions but my kafkaListener is keeps on consuming messages even if my consumer is in paused state.

import org.apache.kafka.clients.consumer.Consumer;

private Consumer<String, String> kafkaConsumer;

public void pauseKafkaC…


This content originally appeared on DEV Community and was authored by Shikha

I am using below functions but my kafkaListener is keeps on consuming messages even if my consumer is in paused state.

import org.apache.kafka.clients.consumer.Consumer;

private Consumer<String, String> kafkaConsumer;

public void pauseKafkaConsumer() {
  if (!kafkaConsumer.paused().isEmpty()) {
     return;
  }
  // Pause all assigned partitions
  Collection<TopicPartition> assignedPartitions = 
  kafkaConsumer.assignment();
  kafkaConsumer.poll(0);
  kafkaConsumer.pause(assignedPartitions);
}

// Resume the Kafka consumer
public void resumeKafkaConsumer() {
  // Resume all paused partitions
  Collection<TopicPartition> pausedPartitions = 
  kafkaConsumer.paused();
      kafkaConsumer.resume(pausedPartitions);
}


@KafkaListener(topics = "#{'${spring.kafka.consumer.topic}'}", groupId = "#{'${spring.kafka.consumer.groupId}'}", containerFactory = "kafkaListenerContainerFactory")
public void consume(String stream, Consumer<?, ?> consumer, Acknowledgment acknowledgment) {
  this.kafkaConsumer = (Consumer<String, String>) consumer;


  if (getEventCount() > 10) {   
        pauseKafkaConsumer();
    return; 
  }
}


This content originally appeared on DEV Community and was authored by Shikha


Print Share Comment Cite Upload Translate Updates
APA

Shikha | Sciencx (2024-07-29T08:19:02+00:00) How can i stop my kafka consumer from consuming messages ?. Retrieved from https://www.scien.cx/2024/07/29/how-can-i-stop-my-kafka-consumer-from-consuming-messages/

MLA
" » How can i stop my kafka consumer from consuming messages ?." Shikha | Sciencx - Monday July 29, 2024, https://www.scien.cx/2024/07/29/how-can-i-stop-my-kafka-consumer-from-consuming-messages/
HARVARD
Shikha | Sciencx Monday July 29, 2024 » How can i stop my kafka consumer from consuming messages ?., viewed ,<https://www.scien.cx/2024/07/29/how-can-i-stop-my-kafka-consumer-from-consuming-messages/>
VANCOUVER
Shikha | Sciencx - » How can i stop my kafka consumer from consuming messages ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/29/how-can-i-stop-my-kafka-consumer-from-consuming-messages/
CHICAGO
" » How can i stop my kafka consumer from consuming messages ?." Shikha | Sciencx - Accessed . https://www.scien.cx/2024/07/29/how-can-i-stop-my-kafka-consumer-from-consuming-messages/
IEEE
" » How can i stop my kafka consumer from consuming messages ?." Shikha | Sciencx [Online]. Available: https://www.scien.cx/2024/07/29/how-can-i-stop-my-kafka-consumer-from-consuming-messages/. [Accessed: ]
rf:citation
» How can i stop my kafka consumer from consuming messages ? | Shikha | Sciencx | https://www.scien.cx/2024/07/29/how-can-i-stop-my-kafka-consumer-from-consuming-messages/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.