Skip to content

Instantly share code, notes, and snippets.

@soudmaijer
Created January 31, 2020 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soudmaijer/2a11cd0ef9b4831b2ad5adcc58472b93 to your computer and use it in GitHub Desktop.
Save soudmaijer/2a11cd0ef9b4831b2ad5adcc58472b93 to your computer and use it in GitHub Desktop.
Kotlin Spring Boot Kafka producer example
package nl.sourcelabs.kafka.producer
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.kafka.core.KafkaTemplate
@SpringBootApplication
class TestProducer : CommandLineRunner {
private val LOG = LoggerFactory.getLogger(TestProducer::class.java)
@Autowired
private lateinit var template: KafkaTemplate<Any, Any>
@Throws(Exception::class)
override fun run(vararg args: String) {
produceMessages(100, "test")
LOG.info("Messages sent successfully")
}
fun produceMessages(number: Int, topicName: String) {
for (i in 0 until number) {
template.send(topicName, Integer.toString(i), Integer.toString(i)).also {
LOG.info("Sending Message #$i")
}
}
}
}
fun main(args: Array<String>) {
SpringApplication.run(TestProducer::class.java, *args)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment