2

I am trying to follow https://github.com/spring-projects/spring-kafka/issues/361 to pass topic names from .yml file to the @kafkalistener. But the compiler throws following error

Type mismatch.
Required:
Array<String>
Found:
String
 Unresolved reference: spring

Below is the receiver code

@Component
class Receiver {

    companion object {
        private val LOGGER = LoggerFactory.getLogger(Receiver::class.java)
    }

    @Autowired
    private val taskExecutor: TaskExecutor? = null

    @Autowired
    private val applicationContext: ApplicationContext? = null

    @KafkaListener(topics = "#{'${spring.kafka.topics}'.split(',')}")
    fun receive(@Header(KafkaHeaders.RECEIVED_TOPIC) topic: String) {

    }    
}

Below is my build.gradle file

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.1.7.RELEASE"
    id("io.spring.dependency-management") version "1.0.8.RELEASE"
    kotlin("jvm") version "1.2.71"
    kotlin("plugin.spring") version "1.2.71"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.springframework.kafka:spring-kafka")
}


tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "1.8"
    }
}

What am i missing here?

Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
nitesh solanki
  • 157
  • 2
  • 9

1 Answers1

6

This works for me:

@KafkaListener(topics = ["#{'\${test.topics}'.split(',')}"])

Pay attention how I had to apply Kotlin syntax to the annotation attribute value. Also Keep in mind that $ is a template specific operator in Kotlin, so we need to escape it to make it as a plain symbol for further properties placeholder resolution.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118