@KafkaListener turns a method on a Spring component into a Kafka record handler. Spring creates a listener container that wraps a KafkaConsumer and subscribes it to the declared topic or topics. The annotated class can remain a message-driven POJO: the annotation supplies the Kafka-facing role. section 12.2.1
Declare topics and a consumer group directly on the annotation. Property placeholders keep those names outside compiled code, so deployments can change topic or group settings through properties. The method receives the deserialized record value; parameters can also bind Kafka headers such as the received timestamp and message key. section 12.2.1 section 12.2.2
Route a processed loan
A listener consumes a LoanApplication, decides whether it is approved, builds a processed copy, and sends it to either an accepted or rejected topic with KafkaTemplate. A separate listener component can then consume each outcome topic, while a QA listener receives selected processed loans. section 12.2.1
Place the annotation at either scope depending on the incoming message shape:
- Method level: annotate a handler when one method consumes the declared topic's record type. section 12.2.1
- Class level: pair @KafkaListener with @KafkaHandler methods to dispatch multiple payload types; mark a default handler for unknown objects. section 12.2.1
- Custom container factory: name a ConcurrentKafkaListenerContainerFactory bean and reference it with containerFactory when a listener needs nondefault behavior. section 12.2.2
Listener concurrency belongs to the container factory. Setting concurrency to the partition count creates a KafkaConsumer per partition for maximum throughput. Higher-than-one concurrency creates corresponding consumer threads, so the annotated method must be thread safe. section 12.2.2 section 12.4
Listing 12.9 Class level KafkaListener requires method annotations
@Component
@KafkaListener(topics = "${loan.app.input.topic}", groupId =
"${application.group}") #1
public class NewLoanApplicationProcessorListenerClassLevel {
@KafkaHandler #2
public void doProcessLoanApplication(LoanApplication loanApplication) {
// Handle the loan application
}
@KafkaHandler(isDefault = true) #3
public void handleUnknownObject(Object unknown) {
// Handle the unknown object
}
This listing shows class-level topic and group configuration, type-specific @KafkaHandler methods, and a default fallback handler. section 12.2.1
