Skip to content

Instantly share code, notes, and snippets.

@ankitthakur
Created September 3, 2018 13:41
Show Gist options
  • Save ankitthakur/2a814c5a9624ab14e681c9168041fbe3 to your computer and use it in GitHub Desktop.
Save ankitthakur/2a814c5a9624ab14e681c9168041fbe3 to your computer and use it in GitHub Desktop.
Kafka Service class which will use Kafka Stream to send message as producer
package com.thakur.kafkaserver.service
import com.thakur.kafkaserver.config.KafkaServerStreams
import com.thakur.kafkaserver.model.Person
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.messaging.MessageHeaders
import org.springframework.messaging.support.MessageBuilder
import org.springframework.stereotype.Service
import org.springframework.util.MimeTypeUtils
@Service
class KafkaService(producerStream: KafkaServerStreams) {
private val producerStream: KafkaServerStreams
private val log: Logger = LoggerFactory.getLogger(KafkaService::class.java)
init {
this.producerStream = producerStream
}
fun helloService(): String {
return "hello service"
}
fun sendMessage(person: Person) {
log.info("Sending person {}", person)
val messageChannel = producerStream.producerChannel()
messageChannel.send(MessageBuilder
.withPayload(person)
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
.build())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment