tianhao
07/25/2018, 6:01 AMaccessing non-final property in constructor
not sure how is the fix for that? Thanks
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)
}
}
hho
07/25/2018, 1:09 PMval
and you're only accessing it in the constructor to initialize it.
The only thing I would change in this code snippet is to make producer private
.