Video in progress
What does the @KafkaListener annotation do under the hood?
Spring creates a listener container for each @KafkaListener. The container calls poll() in its own thread, deserializes the records, passes them to your method, and commits the offset depending on the configuration.
📖 Answer
@KafkaListener(topics = "payments", groupId = "ledger")
public void onPayment(PaymentEvent event,
@Header(KafkaHeaders.RECEIVED_PARTITION) int partition,
@Header(KafkaHeaders.OFFSET) long offset) {
log.info("p={} o={} {}", partition, offset, event.id());
ledger.apply(event);
}
In the background a ConcurrentMessageListenerContainer runs. It creates a KafkaConsumer, joins the group, and pulls messages in a loop. If your method finishes normally, the message counts as successful; if it throws an exception, the error handler kicks in (by default it retries a few times, then logs it and moves on).
The important part: your method runs on the container thread. If you do long work in it, the next poll() is delayed. If it takes too long, the consumer gets kicked out of the group. We'll get to that under max.poll.interval.ms.
Sign up to mark studied questions and save your progress.