spring-kafka

What is the recommended way to obtain a list of all partitions that a ConcurrentMessageListenerContainer is currently handling


I am looking for the best/recommended way to obtain a list of partitions that a ConcurrentMessageListenerContainer is using while reading from a topic. Currently I am doing this manually, by having an implementation of ConsumerAwareRebalanceListener that is tracking assignments/revocations but I wonder whether there isn't a better/simpler way to achieve this.

The goal is to use this information within the application, so I always need an up-to-date list of partitions. Looking at the API for ConcurrentMessageListenerContainer, I see that it is exposing getAssignedPartitions().

A similar question has been asked here but there is no accepted answer until now.


Solution

  • If your ConcurrentMessageListenerContainer is managed by KafkaListenerEndpointRegistry, you could use KafkaListenerEndpointRegistry to get information about your listener containers through container's id and you could see the ContainerProperties, AssignedPartitions,... of this listener container.

    Example of using KafkaListenerEndpointRegistry: https://docs.spring.io/spring-kafka/docs/current/reference/html/#pause-resume