liveBook · Manning

KafkaListenerContainer

As explained in Kafka Streams in Action, Second Edition

A KafkaListenerContainer is a Spring Kafka runtime component that manages a Kafka consumer and invokes listener code for consumed records.

A KafkaListenerContainer is the runtime bridge between a Spring message-driven POJO and Kafka. Applying @KafkaListener causes ConcurrentKafkaListenerContainerFactory to create a listener container that wraps a KafkaConsumer subscribed to the declared topic or topics. section 12.2.1

Configure the container through listener annotations and externalized properties:

  • Set topics with a property placeholder such as ${loan.app.input.topic}; the underlying consumer subscribes to the resolved topic. section 12.2.1
  • Set groupId with ${application.group} to configure the underlying consumer group without recompiling when deployment properties change. section 12.2.1
  • Place @KafkaListener on methods for direct handling, or place it on a class and route payloads through @KafkaHandler methods, including a default handler for unknown objects. section 12.2.1
  • Use separate listeners and groups to process accepted, rejected, and quality-assurance loan topics independently. section 12.2.1
Listing 12.6 NewLoanApplicationProcessor declaration and constructor
@Component                                  #1
public class NewLoanApplicationProcessor {

  @Value("${accepted.loans.topic}")         #2
  private String acceptedLoansTopic;

  @Value("${rejected.loans.topic}")
  private String rejectedLoansTopic;

  @Value("${qa.application.topic}")
  private String qaLoansTopic;

  private final KafkaTemplate<String, LoanApplication>
   kafkaTemplate;                                 #3

  @Autowired                               #4
  public NewLoanApplicationProcessor(
           KafkaTemplate<String, LoanApplication> kafkaTemplate) {
      this.kafkaTemplate = kafkaTemplate;
  }

The declaration and constructor show the component that receives a KafkaTemplate; the listener method supplies the container’s topic and group configuration. section 12.2.1

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains factory creation, consumer wrapping, topic subscription, group configuration, and class-level dispatch, but omits container lifecycle internals.
Examples
Uses loan-application processing with accepted, rejected, and quality-assurance topics plus typed @KafkaHandler methods.
What you can do after
Apply it via @KafkaListener and @KafkaHandler to consume configured Kafka topics in Spring components.
How it connects
Connects Spring Kafka annotations with Kafka consumers, consumer groups, topics, message-driven POJOs, and KafkaTemplate.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.