My application is listening to multiple kafka topics. Right now, my listener looks like below, and my.topics
in property file contains the list of comma separated kafka topics
@KafkaListener(topics = ["#{'\${my.topics}'.split(',')}"], groupId = "my.group", containerFactory = "myKafkaFactory")
fun genericMessageListener(myRequest: MyRequest, ack: Acknowledgment) {
//do Something with myRequest
ack.acknowledge()
}
My ConcurrentKafkaListenerContainerFactory
is
@Bean
fun myKafkaFactory(): ConcurrentKafkaListenerContainerFactory<String, MyRequest> {
val factory = ConcurrentKafkaListenerContainerFactory<String, MyRequest>()
factory.consumerFactory = DefaultKafkaConsumerFactory(configProps(), StringDeserializer(), MyRequestDeserializer())
factory.containerProperties.ackMode = ContainerProperties.AckMode.MANUAL
return factory
}
Is there a way i can create a separate consumer for each topic dynamically so that when I add one more topic to the list in my.topics
, spring will automatically creates a separate consumer for that topic.
The problem I am facing now is, if something goes wrong with one of the messages in any of the topics, messages in other topics are also getting impacted.
On a high level, i am looking for something like this
@Bean
fun myKafkaFactory(): ConcurrentKafkaListenerContainerFactory<String, MyRequest> {
val factory = ConcurrentKafkaListenerContainerFactory<String, MyRequest>()
factory.consumerFactory = DefaultKafkaConsumerFactory(configProps(), StringDeserializer(), MyRequestDeserializer())
factory.containerProperties.ackMode = ContainerProperties.AckMode.MANUAL
factory.isOneConsumerPerTopic(true)
return factory
}
so that factory.isOneConsumerPerTopic(true)
will ensure a separate consumer is created for each topic in the Array.
I did go through How to create separate Kafka listener for each topic dynamically in springboot?. I am looking a bit more 'cleaner' solution. :)