📖 Scenario: You are building a simple Spring Boot application that sends and receives messages using Kafka. This is like sending letters through a post office and then reading them when they arrive.
🎯 Goal: Create a Spring Boot application that sets up a Kafka topic, sends a message to it, and listens for messages from that topic.
📋 What You'll Learn
Create a Kafka topic configuration bean
Create a Kafka producer bean to send messages
Create a Kafka consumer bean to listen for messages
Send a test message to the Kafka topic
💡 Why This Matters
🌍 Real World
Kafka is used in many applications to send messages between different parts of a system reliably and quickly, like sending notifications or processing orders.
💼 Career
Understanding Kafka integration with Spring Boot is valuable for backend developers working on scalable, event-driven systems.
Progress0 / 4 steps
1
Create Kafka topic configuration
Create a Spring bean method called topic that returns a NewTopic object with the name "myTopic", 1 partition, and 1 replica.
Spring Boot
Hint
Use @Bean annotation and return new NewTopic("myTopic", 1, (short) 1).
2
Create Kafka producer bean
Add a Spring bean method called kafkaTemplate that returns a KafkaTemplate<String, String> using a ProducerFactory<String, String> parameter named producerFactory.
Spring Boot
Hint
Define a @Bean method named kafkaTemplate that takes ProducerFactory<String, String> and returns new KafkaTemplate<>(producerFactory).
3
Create Kafka consumer listener
Create a Spring component class called KafkaConsumer with a method listen annotated with @KafkaListener(topics = "myTopic") that takes a String message parameter.
Spring Boot
Hint
Use @Component on the class and @KafkaListener(topics = "myTopic") on the method that takes a String message.
4
Send a test message to Kafka topic
In a Spring @Component class called KafkaSender, create a method sendMessage that takes a KafkaTemplate<String, String> named kafkaTemplate and sends the message "Hello Kafka" to the topic "myTopic" using kafkaTemplate.send("myTopic", "Hello Kafka").
Spring Boot
Hint
Define a @Component class with a method sendMessage that calls kafkaTemplate.send("myTopic", "Hello Kafka").
Practice
(1/5)
1. What is the primary purpose of @KafkaListener in a Spring Boot application?
easy
A. To listen and consume messages from a Kafka topic
B. To send messages to a Kafka topic
C. To configure Kafka broker settings
D. To create new Kafka topics automatically
Solution
Step 1: Understand the role of @KafkaListener
This annotation marks a method to receive messages from a Kafka topic automatically.
Step 2: Differentiate sending and listening
Sending messages is done with KafkaTemplate, not @KafkaListener.
Final Answer:
To listen and consume messages from a Kafka topic -> Option A
What will be printed if a message "Hello Kafka" is sent to myTopic?
medium
A. Received: Hello Kafka
B. Hello Kafka
C. Received message: Hello Kafka
D. Error: No message received
Solution
Step 1: Analyze the listener method output
The method prints "Received: " concatenated with the message string.
Step 2: Substitute the message value
Message is "Hello Kafka", so output is "Received: Hello Kafka".
Final Answer:
Received: Hello Kafka -> Option A
Quick Check:
Prints "Received: " + message [OK]
Hint: Check print statement format carefully [OK]
Common Mistakes:
Ignoring the prefix in the print statement
Assuming only the message prints
Confusing topic name with output
4. Consider this Spring Boot Kafka configuration snippet:
@Bean
public NewTopic topic() {
return new NewTopic("testTopic", 1, (short) 1);
}
What is the likely error if the application fails to create the topic?
medium
A. Incorrect topic name format
B. Missing Kafka broker connection settings
C. NewTopic requires 3 parameters but only 2 provided
D. NewTopic bean must be annotated with @KafkaListener
Solution
Step 1: Understand NewTopic bean usage
NewTopic defines topic name, partitions, and replication factor correctly here.
Step 2: Identify common cause for topic creation failure
Failure usually happens if Kafka broker connection settings are missing or incorrect.
Final Answer:
Missing Kafka broker connection settings -> Option B
Quick Check:
Broker settings needed for topic creation [OK]
Hint: Check Kafka broker config if topic creation fails [OK]
Common Mistakes:
Thinking @KafkaListener is needed on NewTopic bean
Assuming topic name format is invalid
Miscounting NewTopic constructor parameters
5. You want to create a Spring Boot Kafka consumer that listens to two topics: topicA and topicB. Which of the following @KafkaListener annotations correctly achieves this?
hard
A. @KafkaListener(topics = "topicA, topicB")
B. @KafkaListener(topic = "topicA,topicB")
C. @KafkaListener(topics = {"topicA", "topicB"})
D. @KafkaListener(topics = ["topicA", "topicB"])
Solution
Step 1: Recall correct syntax for multiple topics
Spring Boot expects an array of strings for multiple topics: topics = {"topicA", "topicB"}.
Step 2: Evaluate other options
@KafkaListener(topic = "topicA,topicB") uses wrong attribute name and comma string, C uses string with comma but not array, D uses invalid syntax with square brackets.
Final Answer:
@KafkaListener(topics = {"topicA", "topicB"}) -> Option C
Quick Check:
Multiple topics require array syntax [OK]
Hint: Use curly braces for multiple topics in @KafkaListener [OK]
Common Mistakes:
Using comma-separated string instead of array
Wrong attribute name 'topic' instead of 'topics'
Using square brackets instead of curly braces
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.