【问题标题】:Shutdownhook - Log not printed in console/File @Predestroy methodShutdownhook - 日志未在控制台/文件 @Predestroy 方法中打印
【发布时间】:2019-01-21 10:26:30
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 添加睡眠没有帮助。有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多