Skip to content
Advertisement

Use Kakfa connection to dynamically Subscribe/Unsubscribe the Kafka Topics using Spring Boot

I’m developing a SpringBoot application which exposes the APIs to sub/unsub the Kafka topics. All we need to do is to pass the topic-name in the API call and the application will subscribe to it and consume messages.

Subscribe topic API :

JavaScript

Unsubscribe topic API :

JavaScript

Dependency

JavaScript

I have created KafkaConsumerConfiguration in which stated some beans (as follows).

JavaScript

and I have used the ConcurrentMessageListenerContainer.start() method to when the subscribe API is called against an topic-id.

JavaScript

This solution is working absolutely fine. Like,

  • This is sub/unsubscribing the kafka topics using the APIs
  • Also receiving the messages properly.

But the issue that , Every time when I call the API to subscribe the topic , it creates the AdminClient.create(props)(line 336 of below image)

AbstractMessageListenerContainer.class

This generates the logs as below ,

JavaScript

I don’t want the AdminClient to create this instance each time.Because in this way, it take round 2 seconds to create a subscription to the topic. Which is unacceptable in my use-case.

Required Solution:

The kafka connection will be created only once. Then I can use that connection sub/unub the kafka topics. So that each time it doesn’t create this connection and also the time efficient improved.

thanks.

Advertisement

Answer

2.3.x has been out of support for a long time now

https://spring.io/projects/spring-kafka#support

The last 2.3.x version was 2.3.14 last July.

The admin is used to check if the topic exists; with that old version, controlled by the missingTopicsFatal property in ContainerProperties; it is true in that version.

With modern versions (since 2.3.4), it is false, so an AdminClient will not be created when you start the container.

But you really need to upgrade to a supported version (2.8.5 advised – 2.7.x goes out of OSS support soon).

There is no support to reuse a consumer with different (topics); a new consumer will be created each time you start the container.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement