java - Shutdownhook - 日志未在控制台/文件 @Predestroy 方法中打印
问题描述
Hav spring boot 应用程序,在调用 Shutdown hook @Predestroy 方法时,应用程序关闭发生。但是日志不会打印在控制台和文件中,打印日志后的 sys out 行。看来,Looger 控制台在应用程序/进程关闭之前关闭。
建议一些解决方法来解决问题。
关机脚本:
SET /P PID_FROM_FILE= < application.pid taskkill /pid %PID_FROM_FILE% /f
Gracefulshutdown 钩子类:
@Component
public class GracefulShutdownHook {
private static final Logger LOGGER = LogManager.getLogger(GracefulShutdownHook.class);
@Autowired
@Qualifier("inboundChannel")
MessageChannel inboundChannel;
@Autowired
@Qualifier("pollingExecutor")
ThreadPoolTaskExecutor pollingExecutor;
@PreDestroy
public void onDestroy() throws Exception {
LOGGER.log(Level.INFO, "Application shutdown Initiated"); // this log is not printed
System.out.println(""Application shutdown Initiated""); // Sys out is printed
LOGGER.log(Level.INFO, "Application shutdown Processing"); // this log is not printed
inboundChannel.send(new GenericMessage<String>"@'filesInChannel.adapter'.stop()"));
pollingExecutor.shutdown();
LOGGER.log(Level.INFO, "Application shutdown succesfully"); // not printed
}
@Bean
public ExitCodeGenerator exitCodeGenerator() {
return () -> 0;
}
}
解决方案
我怀疑您在记录和关闭之间创建了竞争条件;也就是说,在记录可以完成之前关闭。尝试在 @PreDestroy 方法的末尾添加一个短暂的睡眠,看看它有什么不同。
推荐阅读
- c# - 如何路由到子控制器覆盖 API 而不是基本控制器 API
- javascript - Javascript 警报未显示
- r - 使用 R 进行布朗运动模拟
- ios - 拆分数组并将值分配给其中的每个值
- python - Nagios 检查给出错误而不是我期望的输出
- python - Anaconda基础环境VScode中无法导入库错误
- sqlite - 附加行之前的 SQLite 索引
- spring-boot - 如何定义递归 URL 消费逻辑
- getgauge - 如何在 taiko 中启动 chrome 浏览器而不是 chromium,因为我是 taiko 的新手
- python - 将文件保存到 Azure Blob 存储