如果使用Kafka服务器DisconnectException,如何在@KafkaListener中使用ContainerStoppingErrorHandler终止应用程序

时间:2019-04-12 09:03:32

标签: spring-kafka

我想处理服务器DisconnectException并在发生服务器DisconnectException时终止应用程序

如何捕获此错误并停止应用程序?

@KafkaListener(topics = { "${kafka.status-topic}", "${kafka.start-topic}" }, containerFactory = "kafkaListenerContainerFactory")
public void listen(@Payload final String message,
        @Header(KafkaHeaders.RECEIVED_TOPIC) final String topic) {
    log.debug("Received '{}'-message {} from Kafka", topic, message);
    LinkedList<IMessageListener> topicListeners = listeners.get(topic);
    for (final IMessageListener l : topicListeners) {
        // call listeners in a separate thread
        executor.execute(new Runnable() {
            @Override
            public void run() {
                l.messageReceived(topic, message);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试捕获异常,然后在catch块内调用System.exit(0)