hey channel … I’m Java developer and newcomer with...
# getting-started
a
hey channel … I’m Java developer and newcomer with Kotlin. I’ve been working on a Kotlin/Springboot and Kafka application. I am using the KafkaListener and it requires a
const val
for name of the topic. This information comes from a parameter. Even if I assign the value to a new variable I get this issue… Could you please help me to implement that correctly?
l
I don't think you can do that in kotlin or java. But it looks like you could use expression language: https://stackoverflow.com/questions/54038928/how-to-pass-dynamic-topic-name-to-kafkalistenertopics-from-environment-variab
b
Try passing the
@Value
through the constructor instead
a
humm… let me try that
nope
l
The value needs to be known at compilation time, that's why it won't allow you to use variables and suggests a constant
b
And with a
val
in the constructor?
a
yeah
let me try again
I guess you @Luis Mirabal is right. It can not be done like I tried with @Value
let me try with Autowired
not working…
j
pretty sure you can use a SPEL for the topic name
a
hey @Jacob you mean… something like this? @KafkaListener(topics = [(“\#{T(package.name.TopicNameValue.Class).CONST}“)], groupId = “ingestion-consumer-groupid”)
@KafkaListener(topics =["\${myTopicProperty}"]
My SPel skills are weak But I do recall using the spel capabilities on the annotation to be able to dynamically set the topicname from the spring "environment"
a
I see… thank you Jacob. I will update here once I manage finish this…
I get this…
my Class
Copy code
class TopicNameValue {
    companion object {
        @Value("\${ingestion.config.topic.name}")
        lateinit var topicIngestionName: String
    }
}
and this Service
Copy code
@KafkaListener(topics =["\${topicIngestionName}"], groupId = "ingestion-consumer-groupid")
fun consumeIngestion(message: String) {
    println("consumed message: $message")
}
Working fine, just like this:
Copy code
@KafkaListener(
    topics = ["#{'\${ingestion.config.topic.name}'.split(',')}"],
    groupId = "ingestion-consumer-groupid")
fun consumeIngestion(message: String) {
    println("consumed message: $message")
}