Kafka Stream:优雅关闭

时间:2018-05-14 01:24:37

标签: apache-kafka apache-kafka-streams

如果我们在后台启动KafkaStream应用程序(比如Linux),有没有办法从外部发送到应用程序,可以启动正常关机?

1 个答案:

答案 0 :(得分:2)

正如文档(https://kafka.apache.org/11/documentation/streams/tutorial)中所述,建议注册一个调用KafkaStreams#close()进行干净关闭的关闭钩子:

final CountDownLatch latch = new CountDownLatch(1);

// attach shutdown handler to catch control-c
Runtime.getRuntime().addShutdownHook(new Thread("streams-shutdown-hook") {
    @Override
    public void run() {
        streams.close();
        latch.countDown();
    }
});

try {
    streams.start();
    latch.await();
} catch (Throwable e) {
    System.exit(1);
}
System.exit(0);