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