Home > database >  Way to determine Kafka Topic for @KafkaListener on application startup?
Way to determine Kafka Topic for @KafkaListener on application startup?

Time:01-24

We have 5 topics and we want to have a service that scales for example to 5 instances of the same app.
This would mean that i would want to dynamically (via for example Redis locking or similar mechanism) determine which instance should listen to what topic.
I know that we could have 1 topic that has 5 partitions - and each node in the same consumer group would pick up a partition. Also if we have a separately deployed service we can set the topic via properties.
The issue is that those two are not suitable for our situation and we want to see if it is possible to do that via what i explained above.

@PostConstruct
private void postConstruct() {
   // Do logic via redis locking or something do determine topic
   dynamicallyDeterminedVariable = // SOME LOGIC
}

@KafkaListener(topics = "{dynamicallyDeterminedVariable")
  void listener(String data) {
    LOG.info(data);
  }

CodePudding user response:

Yes, you can use SpEL for the topic name.

#{@someOtherBean.whichTopicToUse()}.

  • Related