Created
January 31, 2020 14:28
-
-
Save soudmaijer/2a11cd0ef9b4831b2ad5adcc58472b93 to your computer and use it in GitHub Desktop.
Kotlin Spring Boot Kafka producer example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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