0

Is there a way to get hold on the ListenerContainer object used by @KafkaListener? I want to dynamically subscribe to new topics using the container if possible. Or there is another better way to subscribe new topics at runtime? Thanks. -aj

aj2
  • 3
  • 2

1 Answers1

0

If you use '@EnableKafka', then KafkaListenerEndpointRegistry is automatically registered as Bean ( code : https://git.io/JfQDQ )

So you can get a list of listener as below.

@Service
public class KafkaListenerHandler {
    private final KafkaListenerEndpointRegistry registry;
    public KafkaListenerHandler(KafkaListenerEndpointRegistry registry) {
        this.registry = registry;
    }
    public List<MessageListenerContainer> getListener() {
        return new ArrayList<>(registry.getListenerContainers());
    }
}

However, you cannot change the topic during Runtime...

See this link : https://stackoverflow.com/a/54467119/2462408

bistros
  • 1,139
  • 1
  • 9
  • 23
  • thanks, @bistro. Any other way to update the topic list in existing listener container? – aj2 Jun 14 '20 at 15:48
  • I don't know such a way. Kafka Consumer does not originally have that function. In other ways, create a new listener, or declare the topic name regular pattern – bistros Jun 14 '20 at 16:00