gpt4 book ai didi

java - 如何从 Storm 拓扑主类调用关闭钩子(Hook)?

转载 作者:行者123 更新时间:2023-12-01 11:57:31 26 4
gpt4 key购买 nike

我有一个 Storm 拓扑类,它启动一个kafka spout和bolts。本课为主课。我正在尝试清理退出 Storm 拓扑,因此我在该拓扑主方法中创建了一个关闭 Hook 。

//Shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Inside shutdown hook.");
Utils.sleep(1000000);
cluster.killTopology("netra-fault-management");
cluster.shutdown();
logger.info("Shutting down Topology.");
}
});

这是我的关闭 Hook ,位于拓扑学类的主要方法中。我从命令提示符运行它,当我执行 ctrl+c 时,预计会运行此关闭 Hook ,但它只是关闭并且没有调用任何 Hook 代码。有没有小伙伴知道如何在 ctrl+c (SIGINT) 上运行它。

最佳答案

使用Runtime#addShutdownHook,这是可能的。
该问题应该是由 Utils.sleep(1000000); 引起的。您不应该“hibernate ”关闭线程,因为 Java 虚拟机(和其他应用程序)不允许这样做。 shutdown hook 被设计为在应用程序即将关闭时被调用,以保存重要的东西或卸载资源等。即使线程没有完全执行,Java 虚拟机也会在几秒钟后终止。

In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.

来自运行时类的 JavaDoc。有时关闭钩子(Hook)不会被调用。祝你好运!

关于java - 如何从 Storm 拓扑主类调用关闭钩子(Hook)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28321305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com