首页 > 解决方案 > 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;
 }
 }

标签: javaspring-bootloggingshutdown-hookspring-logback

解决方案


我怀疑您在记录和关闭之间创建了竞争条件;也就是说,在记录可以完成之前关闭。尝试在 @PreDestroy 方法的末尾添加一个短暂的睡眠,看看它有什么不同。


推荐阅读