0
0
Spring Bootframework~10 mins

Kafka integration basics in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a Kafka topic bean in Spring Boot.

Spring Boot
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class KafkaTopicConfig {

    @Bean
    public NewTopic [1]() {
        return new NewTopic("my_topic", 1, (short) 1);
    }
}
Drag options to blanks, or click blank then click option'
AtopicBean
BcreateTopic
CnewTopic
DkafkaTopic
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not clearly indicate it creates a topic.
Not annotating the method with @Bean.
2fill in blank
medium

Complete the code to send a message to a Kafka topic using KafkaTemplate.

Spring Boot
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class ProducerService {

    private final KafkaTemplate<String, String> kafkaTemplate;

    public ProducerService(KafkaTemplate<String, String> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }

    public void sendMessage(String message) {
        kafkaTemplate.[1]("my_topic", message);
    }
}
Drag options to blanks, or click blank then click option'
Asend
Bpublish
Cproduce
Dwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that do not exist on KafkaTemplate.
Confusing send with other verbs like publish or write.
3fill in blank
hard

Fix the error in the Kafka listener method annotation to listen to the correct topic.

Spring Boot
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class ConsumerService {

    @KafkaListener(topics = "[1]", groupId = "group_id")
    public void consume(String message) {
        System.out.println("Received message: " + message);
    }
}
Drag options to blanks, or click blank then click option'
Amy_topic
Btopic1
Cdefault_topic
Dkafka_topic
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different topic name than the producer sends to.
Misspelling the topic name.
4fill in blank
hard

Fill both blanks to configure Kafka consumer properties in application.yml.

Spring Boot
spring:
  kafka:
    consumer:
      bootstrap-servers: localhost:9092
      group-id: [1]
      key-deserializer: [2]
Drag options to blanks, or click blank then click option'
Amy_group
Borg.apache.kafka.common.serialization.StringDeserializer
Corg.apache.kafka.common.serialization.IntegerDeserializer
Ddefault_group
Attempts:
3 left
💡 Hint
Common Mistakes
Using default group id that may conflict with others.
Using wrong deserializer class for string keys.
5fill in blank
hard

Fill all three blanks to create a Kafka listener method that listens to two topics and uses a specific group id.

Spring Boot
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class MultiTopicConsumer {

    @KafkaListener(topics = {"[1]", "[2]"}, groupId = "[3]")
    public void listen(String message) {
        System.out.println("Message received: " + message);
    }
}
Drag options to blanks, or click blank then click option'
AtopicA
BtopicB
Cmulti_group
Ddefault_group
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same topic twice.
Using a default group id that conflicts with others.