如果我们在后台启动KafkaStream应用程序(比如Linux),有没有办法从外部发送到应用程序,可以启动正常关机?
答案 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);