Skip to content
Advertisement

Spring Kafka multiple topic for one class dynamically

I recently wanted to add a new behavior in my project that uses spring-kafka.

The idea is really simple :

  • App1 create a new scenario name “SCENARIO_1” and publish this string in the topic “NEW_SCENARIO”
  • App1 publish some message on topic “APP2-SCENARIO_1” and “APP3-SCENARIO_1”
  • App2 (group-id=app2) listens on NEW_SCENARIO and creates a new consumer<Object,String> listening on a new topic “APP2-SCENARIO_1”
  • App3 (group-id=app3) listens on NEW_SCENARIO and creates a new consumer<Object,String> listening on a new topic “APP3-SCENARIO_1”

The goal is to create dynamically new topics and consumer. I cannot use spring kafka annotation since I need it to be dynamic so I did this :

JavaScript

And this does not work. I’m missing probably something, but I can’t figure what.

Here I have some logs that tells me that the leader is not available, which is weird since I got the new scenario event.

JavaScript

How do I create dynamically a kafka consumer on a topic ? I think I do it very wrong, but I searched a lot and really didn’t find anything.

Advertisement

Answer

There are several answers here about dynamically creating containers…

Trigger one Kafka consumer by using values of another consumer In Spring Kafka

Kafka Consumer in spring can I re-assign partitions programmatically? Create consumer dynamically spring kafka

Dynamically start and off KafkaListener just to load previous messages at the start of a session

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