I need to catch Kafka warnings like "Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected". I've found the way to achieve this using @EventListener and NonResponsiveConsumerEvent. The issue is Spring Kafka doesn't send this event at all. I use Spring Boot 2.7.0 and Spring Kafka 2.8.6. When I run application ConsumerStartedEvent is triggered, so events work in general. But when I shutdown Docker Kafka container I can see warnings, but event is not triggered.
WARN Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
My configuration:
@Autowired
@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory(
KafkaProperties properties
) {
ConcurrentKafkaListenerContainerFactory<String, String> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory(properties));
factory.getContainerProperties().setMonitorInterval(10);
return factory;
}
and listener
@KafkaListener(topics = "topic", groupId = "foo")
public void listenMessages(ConsumerRecord<String, TickService.TickServiceEvent> record) {
...
}
//works
@EventListener(ConsumerStartedEvent.class)
public void eventHandler(ConsumerStartedEvent event) {
isConnected = true;
}
//works only for ConsumerStartedEvent
@EventListener
public void eventHandler(KafkaEvent event) {
if(event instanceof NonResponsiveConsumerEvent) {
isConnected = false;
}
}
//doesn't work at all
@EventListener(NonResponsiveConsumerEvent.class)
public void eventHandler(NonResponsiveConsumerEvent event) {
isConnected = false;
}
What have I missed? Thx!