- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读 this article
这里是完成消费者线程的代码:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Starting exit...");
consumer.wakeup(); 1
try {
mainThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
据我了解,ShutdownHook 在所有非守护线程都已完成但进程被操作系统销毁之前调用。
1. 从我的角度来看 mainThread.join() 是无用的。主线程总是会在 ShutdownHook 执行的那一刻结束。它是正确的还是我误解了什么?
2.其实我不明白为什么我们需要等待main thred?我们需要等待关闭方法执行吗?
书中提供了以下主要方法代码:
try {
// looping until ctrl-c, the shutdown hook will cleanup on exit
while (true) {
ConsumerRecords<String, String> records =
movingAvg.consumer.poll(1000);
System.out.println(System.currentTimeMillis() + "-- waiting for data...");
for (ConsumerRecord<String, String> record :
records) {
System.out.printf("offset = %d, key = %s,
value = %s\n",
record.offset(), record.key(),
record.value());
}
for (TopicPartition tp: consumer.assignment())
System.out.println("Committing offset at position:" + consumer.position(tp));
movingAvg.consumer.commitSync();
}
} catch (WakeupException e) {
// ignore for shutdown 2
} finally {
consumer.close(); 3
System.out.println("Closed consumer and we are done");
}
}
最佳答案
您执行 consumer.wakeup()
来中断当前消费者的操作(可能长时间运行(例如轮询)或什至被阻止(在 beginningOffsets(. ..)
.
将 mainThread.join()
放在那里是为了确保主线程真正完成并且不会在唤醒后的处理过程中关闭。请记住,shutdownHook 还负责处理中断,而不仅仅是普通的程序关闭。
因此,如果您用例如Ctrl-C:
1. shutdown hook gets called
2. main thread is still running, most often waiting for data in `poll`
3. shutdown hook `wakeup`-s the main thread
4. main thread enters the exception handler, breaks the loop
5. main thread closes the consumer with `.close()`
6. shutdown hook waits for 5. and finishes
如果不等待,您可能没有执行步骤 4 和 5 中的消费者关闭步骤。
关于java - 如何完成kafka消费者安全?(在shutdownHook里面调用thread#join有什么意思吗?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46581674/
据我所知,System.exit 会触发关闭钩子(Hook)(如果注册了任何钩子(Hook)),因此关闭钩子(Hook)线程正在等待任何其他线程加入如果其他线程调用 System.exit() 我认为
我有一个应用程序,它的主要方法生成一百个线程(假设我们模拟一百个帐户)。我正在试验它,我希望它在被 Control-C 中断时只打印终止。 我读到您可以使用 ShutDownHooks 来做到这一点,
我发现要显示 JOptionPane 消息对话框,我需要 shutdownHooks 许可。它有什么作用?向第三方代码授予此权限可能存在哪些风险? 最佳答案 关闭 Hook 是虚拟机退出前(所有非守护
我遇到了这个异常: org.quartz.SchedulerException: SchedulerPlugin class not specified for plugin 'shutdownhoo
我需要从 ShutdownHook 调用该方法的非静态方法 removeLocksOnExit()。 该方法在 main 所在的同一个类中声明。 主要我有以下代码: //final Thread ma
假设 shutdownHook 为: class ShutdownHolder extends Thread { public void run() { Logger logg
我有一个 shutdownhook,它在进程终止时执行。然而,程序对 h2 数据库所做的更改不是持久的。只有在提交后让关闭线程等待一段时间,我才能在再次启动时看到数据库中的更改。 Runtime.ge
如果我有一个 Java 程序可以由于各种原因退出,例如: 因为设置为“关闭时退出”的主窗口已关闭 因为代码中有一些System.exit(0) 因为根本没有更多的窗口(并且没有设置为在关闭时退出)但是
这是我的主要方法,它包含一个 shutdownhook: public static void main(String args[]) { Runtime.getRuntime().addSh
我正在使用带有常规配置的 logback 1.1.10。我最近发现了shutdownHook preference但我还没有找到使用 groovy 配置启用它的方法。这可能吗?还是缺失的功能? 最佳答
有什么办法可以在关闭 Hook 中显示 joptionpanei.s 我需要在我的 shutdownhook 事件中显示一个确认对话框 最佳答案 如果有,它不会帮助你。 关闭 Hook 作为 JVM
我有以下 ShutdownHook 来检测应用程序何时退出: Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
我试图提供一个非常简单的“你想保存这个文档吗?”关闭我的应用程序时的对话框。 我使用了 WindowAdapter 来做到这一点。如果使用任何 GUI 操作关闭应用程序(单击 CLOSE 按钮、从任务
我们如何在 Web 应用程序中注册Shutdown hook? 在 web.xml 或 applicationContext.xml 中注册它有什么好处吗? 我知道如果我们使用带有主类的应用程序很简单
我正在使用 logback 中的元素; jmxConfigurator 声明如下: Thus, unless your application is a standalone Java applic
拥有 Spring Boot 应用程序,在调用 Shutdown hook @Predestroy 方法时,会调用应用程序关闭。但在打印日志后,控制台和文件、sys out 行中均不打印日志。看起来,
在下面的代码片段中,永远不会调用 finalize() 方法。但是当我在 addShutdownHook 中将 finalize() 替换为 close() 时。 finalize() 方法将被调用。
我正在 Tomcat 中使用 Jax-ws 发布 Web 服务 @WebService() public class ChatService ...followed by a constructor
这个问题在这里已经有了答案: 关闭 9 年前。 Possible Duplicate: When Shutdown Hooks Break Bad 想知道在JVM崩溃的场景下是否保证shutdown
我有一些 Java 代码来创建一个关闭钩子(Hook),以便在客户端按下 ctrl+C 时干净地退出: private static void shutdownHandler(Thread mainT
我是一名优秀的程序员,十分优秀!