我有以下代码,但是producer
变量在构造函数中访问了非final属性,导致出现警告。
class KafkaService {
val producer: KafkaProducer<String, String>
init {
val props = Properties()
props[ProducerConfig.BOOTSTRAP_SERVERS_CONFIG] = "127.0.0.1:9092"
props[ProducerConfig.CLIENT_ID_CONFIG] = "DemoProducer"
props[ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG] = StringSerializer::class.java.name
props[ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG] = StringSerializer::class.java.name
producer = KafkaProducer(props)
}
fun sendToKafka(topic: String, message: String) {
val producerRecord: ProducerRecord<String?, String> = ProducerRecord(topic, null, message)
producer.send(producerRecord)
}
}
什么是最好的解决方法?
producer
没有被打开,你也没有访问它,只是初始化。 - Alexey Romanov