Skip to content

Instantly share code, notes, and snippets.

@ankitthakur
Created September 6, 2018 05:48
Show Gist options
  • Save ankitthakur/415c08e6bdf30e31067b476fcf809d16 to your computer and use it in GitHub Desktop.
Save ankitthakur/415c08e6bdf30e31067b476fcf809d16 to your computer and use it in GitHub Desktop.
Kafka Consumer based Service class listening to the kafka topic.
package com.thakur.kafkaclient.service
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.thakur.kafkaserver.model.Person
import org.apache.kafka.clients.consumer.ConsumerRecord
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.kafka.annotation.KafkaListener
import org.springframework.stereotype.Component
@Component
class KafkaService{
fun clientService(): String {
return "hello from kafka client service"
}
private val logger: Logger = LoggerFactory.getLogger(KafkaService::class.java)
@KafkaListener(topics = arrayOf("#{'\${spring.kafka.topic}'.split(',')}"), groupId = "#{'\${spring.kafka.topic.groupId}'}")
fun recieveMessage2(record:ConsumerRecord<String, Any>) {
val jsonString: String = String(record.value() as ByteArray)
logger.info("Received person.topic: {}", record.topic())
logger.info("Received person.partition: {}", record.partition())
logger.info("Received person.offset: {}", record.offset())
logger.info("json string: {}", jsonString)
val objectMapper = ObjectMapper()
val node: JsonNode = objectMapper.readTree(record.value() as ByteArray)
logger.info("Node: {}", node)
val person:Person = Person(firstName = node["firstName"].asText(), lastName = node["lastName"].asText(), messageTime = node["messageTime"].asLong())
logger.info("Person: {}", person)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment