拥有 Spring Boot 应用程序,在调用 Shutdown hook @Predestroy 方法时,会调用应用程序关闭。 但在打印日志后,控制台和文件、sys out 行中均不打印日志。 看起来,Looger 控制台在应用程序/进程关闭之前关闭。
建议一些解决方法来解决该问题。
关闭脚本:
SET/P PID_FROM_FILE= < 应用程序.pid taskkill/pid %PID_FROM_FILE%/f
Gracefulshutdown 钩子(Hook)类:
@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 方法末尾添加一个短暂的 sleep ,看看它会产生什么变化。
关于java - Shutdownhook - 日志未在控制台/文件 @Predestroy 方法中打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51843526/