Member-only story

Kafka Consumer Example Using Java

Gain Java Knowledge
3 min readSep 27, 2020

--

In this article, we will see how to Consume records/messages from Topic. Let’s get to it!

In last article, we discussed how to how to produce records/messages with Kafka brokers. In this article, we will see how to consume records/messages from Topic.

Now, before creating a Kafka producer in java, we need to define the essential Project dependencies. In our project, there will be three dependencies required:

Open URL start.spring.io and Create Maven Project with these three dependencies. Lombok is used to generate setter/getter methods.

So After Generating the project we will extract this file and will import kafka-consumer project into Eclipse.

Here In this project we will create one KafkaMessageListener Class that will be used to consume the message from topic. This class will be annotated with the annotation @KafkaListener(id = “my-listener”, topics=”MyTopic”), we will define id for kafka listener and also define Topic name that will be subscribed by the consumer. On function level we will use @KafkaHandler annotation . So this function will receive the message.

KafkaMessageListener.java

After that we will create KafkaConsumerConfig.java class in package com.example.kafkaconsumer.config. Here we will define…

--

--

Gain Java Knowledge
Gain Java Knowledge

Written by Gain Java Knowledge

The Java programming language is one of the most popular languages today. Stay up to date with news, certifications, free learning resources and much more.

No responses yet