Home > Blockchain >  How to specify multiple topics in separate config properties for one Kafka listener?
How to specify multiple topics in separate config properties for one Kafka listener?

Time:03-17

I would like to create a spring boot application that reads from several Kafka topics. I realise I can create a comma separated list of topics on my appliation.properties, however I would like the topic names to be listed separately for readability and so I can use each topic name to work out how to process the message.

I've found the following questions, but they all have the topics listed as a comma separated array:

Consume multiple topics in one listener in spring boot kafka

Using multiple topic names with KafkaListener annotation

Enabling @KafkaListener to take in variable topic names from application.yml file

Pass array list of topic names to @KafkaListener

The closest I've come is with the following:

application.properties

kafka.topic1=topic1
kafka.topic2=topic2

KafkaConsumer

@KafkaListener(topics = "#{'${kafka.topic1}'},#{'${kafka.topic2}'}")
public void receive(@Header(KafkaHeaders.RECEIVED_TOPIC) String topic,
      @Header(required = false, name= KafkaHeaders.RECEIVED_MESSAGE_KEY) String key,
      @Payload(required = false) String payload) throws IOException {
}       

This gives the error:

Caused by: org.apache.kafka.common.errors.InvalidTopicException: Invalid topics: [topic1,topic2]

I realise I need it to be {"topic1", "topic2} but I can't work out how.

Having the annotation @KafkaListener(topics = "#{'${kafka.topic1}'}") correctly subscribes to the first topic. And if I change it to @KafkaListener(topics = "#{'${kafka.topic2}'}") I can correctly subscribe to the second topic.

It's just the creating of the array of topics in the annotation that I can't fathom.

Any help would be wonderful

CodePudding user response:

@KafkaListener(id = "so71497475", topics = { "${kafka.topic1}", "${kafka.topic2}" })
  • Related