Shutdownhook-控制台/文件@Predestroy方法中未打印日志

时间:2018-08-14 14:11:47

标签: java spring-boot logging shutdown-hook spring-logback

Hav spring引导应用程序,在调用Shutdown hook @Predestroy方法时,会发生应用程序关闭。 但是,日志不会同时打印在控制台和文件中,而是在打印日志后在sys out行中打印。 看来,在应用程序/进程关闭控制台之前,请先将Looger控制台关闭。

建议解决此问题。

关机脚本:

SET / P PID_FROM_FILE =

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;
 }
 }

1 个答案:

答案 0 :(得分:2)

我怀疑您在日志记录和关闭之间创建了竞争条件;也就是说,在完成日志记录之前会先进行关机。尝试在@PreDestroy方法的末尾添加短暂的睡眠,然后看看会有所作为。