0

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
Sundararaj Govindasamy
  • 8,180
  • 5
  • 44
  • 77

1 Answers1

0

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 what RestartEndpoint really does. It restarts the Spring ApplicationContext, 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.

E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • Thanks for the direction, I thought refreshing applicationContext and restarting tomcat are same because when this code runs successfully I see tomcat restart in logs. Let me adjust my understanding :-) – Sundararaj Govindasamy Aug 04 '22 at 16:28
  • It's possible that Spring Boot includes Tomcat in the ApplicationContext restart, I'm not sure. Depending on your purpose, that might be sufficient. But it definitely does not restart the JVM process; again depending on what you're actually trying to accomplish, that may or may not be OK. – E-Riz Aug 04 '22 at 16:50