DEV Community

Coding Saint
Coding Saint

Posted on • Originally published at

Spring Kafka Stream with Custom Objects

Kafka is most sought after event system today. In this series we will look at Kafka event messaging and streaming.

Github Repository link :

What will we be creating

We will create a streaming application using Spring and kafka streams.
We will have a continuously coming stream of famous quotes which will be continously produced at quote topic. Every quote can be tagged with multiple categories i.e. business,education,faith,famous-quotes,friendship,future,happiness,inspirational,life,love,nature,politics,proverb,religion,science,success,technology .
We will have topics related to each of the category. Once a quote comes we will be streaming them to respective category. To keep things simple for demo we have one listener listening to all of the topics
We have taken quotes from

These quotes will be streamed and will be sent to respective topic based on their category.
Listener will keep track of all the quotes which are streamed to these topics.


Installing kafka

### Download
To start kafka we require zookeeper , on kafka website we have different versions you can get the latest stable vesion from Kafka download page Say you have downloaded kafka_2.11-2.4.0.tgz



Use below command to unzip the downloaded file tar xzf kafka_2.11-2.4.0.tgz
### Move to user directory Move content to user directory

mv kafka_2.11-2.4.0/* /usr/local/kafka
### Run Zookeeper
Go to kafka directory and run

cd /usr/local/kafka bin/ config/ ### Run Kafka Server Use below command to run kafka server

bin/ config/ You will see logs confirming the kafka is up an running.

Test kafka installation

#### Create a topic

bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic kafka-test-topic

Verify if topic got created

bin/ --list --zookeeper localhost:2181 kafka-test-topic
#### Send messages to topic

Use below command to activate message terminal to kafka-test-topic. Below command will activate message sending to a topic , key in some interesting messages. Lets call it producer window

bin/ --broker-list localhost:9092 --topic kafka-test-topic >Welcome to Kafka 

Consume the topic Open another terminal to consume messages

 bin/ --bootstrap-server localhost:9092 --topic kafka-test-topic --from-beginning    

Now whatever you key in to producer window will be consumed to consumer window

Other ways to install kafka

There could be many other ways to install and run like using apt installer on Ubuntu , using a docker image etc. The above one is a generic setup .

Create Topics for for app

bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic quotes  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic business  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic education  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic faith  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic famous-quotes  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic famous-quotes  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic friendship  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic future  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic happiness  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic inspirational  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic life  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic love  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic nature  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic politics  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic proverb  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic religion  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic science  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic success  
bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic technology  

Creating Spring Boot Kafka Stream project

We will create project using .
Go to website and dependencies

Spring Boot Kafka Stream dependencies

Download the project and open in your favourite IDE and open it.
Now we have Kafka installed up and running , as a First step we should be creating Quote bean which will flow the quotes from producer to consumer.

public class Quote {
private String content;
private Set<String> tags;
private String author;

Let's add required configurations for Kafka. This includes Serializer and Deserializer
for Quotes.We have configurations for Spring Kafka template , producer and consumer.
Serialization and Deserialiation for key and value objects(POJO) have been mentioned seperately.

      missing-topics-fatal: false
    client-id : quotes-app
      - localhost:9091
      - localhost:9001
      - localhost:9092
      default-topic: quotes
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: com.codingsaint.learning.kafkastreamspring.QuoteSerializer
            strategy: org.apache.kafka.clients.consumer.RoundRobinAssignor
      group-id: random-consumer
      auto-offset-reset: earliest
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: com.codingsaint.learning.kafkastreamspring.QuoteDeserializer
    input: quotes
    output: business,education,faith,famous-quotes,friendship,future,happiness,inspirational,life,love,nature,politics,proverb,religion,science,success,technology

We will create QuoteSerializer , QuoteDeserializer and QuoteSerde which will have both serializer and deserializer
.We are using simple ObjectMapper to serialize and deserialize

public class QuoteSerializer implements Serializer<Quote> {

    public byte[] serialize(String s, Quote quote) {
        byte[] retVal = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            retVal = objectMapper.writeValueAsString(quote).getBytes();
        } catch (Exception e) {
        return retVal;


public class QuoteDeserializer implements Deserializer<Quote> {

    public Quote deserialize(String s, byte[] bytes) {
        ObjectMapper mapper = new ObjectMapper();
        Quote quote = null;
        try {
            quote = mapper.readValue(bytes, Quote.class);
        } catch (Exception e) {
        return quote;



public class QuoteSerde implements Serde<Quote> {
    public QuoteSerde() {

    public Serializer<Quote> serializer() {
        return new QuoteSerializer();

    public Deserializer<Quote> deserializer() {
        return new QuoteDeserializer();

Now Let us see the AppConfig class for configuring Producer and Consumer configurations.

public class AppConfig {
    private static final Logger LOGGER= LoggerFactory.getLogger(AppConfig.class);

    private String inputTopic;

    private List<String> allTopics;

    private KafkaProperties kafkaProperties;

     * Configurations for KafkaStreams
     * @param kafkaProperties Will take defaults from application YAML or Properties file with spring.kafka
     * @return kafkaConfiguration
    @Bean(name= KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME)
    public KafkaStreamsConfiguration kafkaConfiguration(final KafkaProperties kafkaProperties){
        Map<String, Object> config = new HashMap<>();
        config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProperties.getBootstrapServers());
        config.put(StreamsConfig.APPLICATION_ID_CONFIG, kafkaProperties.getClientId());
        config.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        config.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, QuoteSerde.class.getName() );
        config.put(StreamsConfig.DEFAULT_DESERIALIZATION_EXCEPTION_HANDLER_CLASS_CONFIG, LogAndContinueExceptionHandler.class);
        return new KafkaStreamsConfiguration(config);

     * The Stream which delegates each incoming topic to respective destination topic
     * @param kStreamsBuilder
     * @return
    public KStream<String,Quote> kStream(StreamsBuilder kStreamsBuilder){
        for(String topic:allTopics){
            stream.filter((s, quote) -> quote.getTags().contains(topic)).to(topic);
        return stream;


     * Kafka ConsumerFactory configurations
     * @return
    public ConsumerFactory<String, Quote> consumerFactory() {
        Map<String, Object> props = new HashMap<>();
        return new DefaultKafkaConsumerFactory<>(props);

     * Required Configuration for POJO to JSON
     * @return ConcurrentKafkaListenerContainerFactory
    public ConcurrentKafkaListenerContainerFactory<String, Quote>
    kafkaListenerContainerFactory() {

        ConcurrentKafkaListenerContainerFactory<String, Quote> factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setMessageConverter(new StringJsonMessageConverter());
        return factory;
  • Since we have injected default KafkaProperties will have required bindings from application.yml .
  • Beans for default Kafka Stream ,consumer factory and ConcurrentKafkaListenerContainerFactory is created where ConcurrentKafkaListenerContainerFactory bean help us to convert incoming message from String to Json using StringJsonMessageConverter
  • KStream bean helps to create a stream on input topic (quotes) and filter based on tags. For each tag we already have created topics.

Now Let us look at Listener configuration which will listen all the topics

public class TopicConsumers {
    private static final Logger LOGGER = LoggerFactory.getLogger(TopicConsumers.class);

    private List<String> allTopics;

     * For simplicity we are listening all topics at one listener

    @KafkaListener(id = "allTopics", topics = "#{'${kafka.topic.output}'.split(',')}",
            containerFactory = "kafkaListenerContainerFactory")
    public void consume(@Payload Quote quote,
                        @Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition,
                        @Header(KafkaHeaders.RECEIVED_TOPIC) String incomingTopic,
                        @Header(KafkaHeaders.RECEIVED_TIMESTAMP) long ts
    ) {"Incoming quote {}-> {}", incomingTopic, quote);
  • For simplicity we are listening all of these topics at one Listener

Once we run the application we will see logs confirming publishing of quotes with different tags and streams sending them to their respective category. Listener will listen incoming quotes.

Top comments (0)