Advanced Patterns · Advanced · Topic 5 of 5
Kafka Consumer Lifecycle & Health Checks
Listener containers publish events (for example when they are idle) and can be paused, resumed and inspected through the KafkaListenerEndpointRegistry, which Spring Kafka declares for you.
@Configuration
@Slf4j
public class KafkaHealthConfig implements ApplicationListener<ListenerContainerIdleEvent> {
// Published when a container has received no records for the configured idleEventInterval
@Override
public void onApplicationEvent(ListenerContainerIdleEvent event) {
log.info("Consumer idle: listenerId={}, idleFor={}ms",
event.getListenerId(), event.getIdleTime());
}
}
// Programmatic container control
@RestController
@RequestMapping("/admin/kafka")
public class KafkaAdminController {
// The registry is an infrastructure bean that the framework declares automatically
@Autowired
private KafkaListenerEndpointRegistry registry;
@PostMapping("/pause/{listenerId}")
public void pauseListener(@PathVariable String listenerId) {
registry.getListenerContainer(listenerId).pause(); // takes effect before the next poll()
}
@PostMapping("/resume/{listenerId}")
public void resumeListener(@PathVariable String listenerId) {
registry.getListenerContainer(listenerId).resume();
}
@GetMapping("/status")
public Map<String, String> status() {
return registry.getListenerContainerIds().stream()
.collect(Collectors.toMap(
id -> id,
id -> registry.getListenerContainer(id).isRunning() ? "RUNNING" : "STOPPED"
));
}
}Sources
- Application events (Spring for Apache Kafka) (opens in a new tab)
- @KafkaListener lifecycle management (Spring for Apache Kafka) (opens in a new tab)
- Pausing and resuming listener containers (Spring for Apache Kafka) (opens in a new tab)
Checked against Spring for Apache Kafka 4.1.1, Spring Boot 4.1.1 and Kafka 4.2 documentation on 11 October 2026. Draft: not yet reviewed by a Kafka specialist, and releases move on, so confirm against the current documentation before you act on it.