I wrote below scheduler which runs everyday midnight 12 am. This has to restart the spring boot web application ( self ) and it is working as expected most of the time. But once in a week ( approximately), application shutdown happens successfully, but not starting up.
Because this is failing intermittently, I have no clue why this code failing.
In my eclipse IDE environment, it works almost everytime. ( I changed the scheduler to run every 5 mins)
@Service
final class AutoRestartScheduler {
@Autowired
private RestartEndpoint restartEndpoint; //private to protect outside access.
final Logger logger = LoggerFactory.getLogger(AutoRestartScheduler.class);
@Scheduled(cron = "0 0 0 * * *", zone="America/Los_Angeles") //everyday mid-night 12 AM PST
public void restartApp(){
logger.info("Going to restart Tomcat, programmatically.");
logger.info("restarting MyPollerApplication...");
restartEndpoint.restart();
}
}
NOTE:
I am NOT using below property in configuration, because I am NOT using Actuator's /restart endpoint but Spring's Scheduler.
management.endpoint.restart.enabled=true
Since you don't see your own log messages when it fails to restart, that tells us that the application isn't going through the restart code that you're showing; maybe something else is causing the application to shut down.
From the log message you show (
"Going to restart Tomcat"), I suspect you might misunderstand whatRestartEndpointreally does. It restarts the SpringApplicationContext, not the JVM or OS process, and possibly not the Tomcat instance that's hosting the application. Take a look at the JavaDoc and source code for that class to make sure it's actually doing what you think it's doing, and that you're using it correctly. Consider enabling Spring's own logging for this class at DEBUG level to see more about what's going on.