本文介绍了Kafka Stream:正常关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我们在后台启动 KafkaStream 应用程序(比如 Linux),有没有办法从外部向应用程序发送信号,以启动正常关闭?
If we start the KafkaStream app in the background (say Linux), is there a way to signal from external, to the app, that can initiate the graceful shutdown?
推荐答案
如文档中所述 (https://kafka.apache.org/11/documentation/streams/tutorial),建议注册一个调用 KafkaStreams#close()
的关闭钩子来干净关闭:
As describe in the docs (https://kafka.apache.org/11/documentation/streams/tutorial), it's recommended to register a shutdown hook that calls KafkaStreams#close()
for a clean shutdown:
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);
这篇关于Kafka Stream:正常关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!