Reputation: 963
Hav spring boot application, While invoking Shutdown hook @Predestroy method is called and application shutdown happens. But Logs are not printed in both console and file, sys out line after the log is printed. It seems, Looger console shutdown before the application/process shutodwn.
Suggest some work around to resolve the issue.
Shutdown script:
SET /P PID_FROM_FILE= < application.pid taskkill /pid %PID_FROM_FILE% /f
Gracefulshutdown hook class:
@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;
}
}
Upvotes: 0
Views: 463
Reputation: 2923
I suspect that you have created a race condition between logging and shutdown; that is, shutdown is taking place before logging can complete. Try adding a short sleep at the end of your @PreDestroy method and see it that makes a difference.
Upvotes: 2