Home > Mobile >  How to use @TopicPartition inside @KafkaListener annotation in Spring Boot-Kotlin
How to use @TopicPartition inside @KafkaListener annotation in Spring Boot-Kotlin

Time:03-30

I'm new in Spring-Kafka, I created a class that is called KafkaListeners to read messages from kafka broker. However, the listener didn't print old messages in console. So, I added @TopicPartition annotation on my listener method. But, I encountered an error message as follows:

An annotation can't be used as the annotations argument

Script:

    @KafkaListener(
        topicPartitions = @TopicPartition(topic = "demo",
            partitionOffsets = {
                @PartitionOffset(partition = "0", initialOffset = "0")
            }
        )
    )
    fun listenerDemo(data: Any?){
        println(data)
    }

As Image :

enter image description here

enter image description here

I saw @TopicPartition usage in java and it is running but it isn't in kotlin.

Thank you.

CodePudding user response:

The Kotlin configuration has to be like this:

    @KafkaListener(
        topicPartitions = [TopicPartition(topic = "demo",
            partitionOffsets = 
                [PartitionOffset(partition = "0", initialOffset = "0")]
        )]
    )

Those nested annotations must be without @ and since those attributes are arrays, we have to specify these nested annotations as arrays as well.

  • Related