简体   繁体   中英

Java ScheduledExecutorService stopped some time after servlet initialization

A function is implemented in my Tomcat server: basically a ScheduledExecutorService will be setup to run a task periodically upon the initialization of a HttpServlet . The code is like below:

@Override
public void init() throws ServletException {
    HttpsTool.setupUncheckedHttps();
    LOGGER.info("DeploymentInfoScheduler Initialized successfully....");
    System.out.println("DeploymentInfoScheduler Initialized successfully....");
    this.retrieveServiceDeploymentsInfo();
}

private void retrieveServiceDeploymentsInfo() {
    final Runnable deploymentsDataHandler = new DeploymentsDataHandler();
    final ScheduledFuture<?> beeperHandle = deploymentsInfoRetrieverScheduler
            .scheduleAtFixedRate(deploymentsDataHandler, 0, 5, TimeUnit.MINUTES);


}

If the servlet is initialized successfully, the task will keep running every 5 seconds forever. In the DeploymentsDataHandler 's run method, A message is logged like below at the very beginning:

@Override
public void run() {
    LOGGER.info("Now begin a new round of refreshing deployment data...");
    this.doWorkFlow();
}

Correct me if I am wrong: no matter whether there is any exception unhandled in the doWorkFlow method, that message should always be logged every 5 minutes. But the fact is this scheduler runs well for some time and suddenly stops working, and even the message is stopped to be logged. Any possible explanation? How could I handle such an situation since I want to make sure this scheduler service is always running. Or is there any way to debug this issue?

I assume the following code will make sure the Scheduler will never stop executing even an exception occurs in the doworkFlow .

@Override
public void run() {
    try{
        LOGGER.info("Now begin a new round of refreshing deployment data...");
        this.doWorkFlow();
    }catch(Exception e){
        return;
    }

}

Correct me if I am wrong: no matter whether there is any exception unhandled in the doWorkFlow method, that message should always be logged every 5 minutes

No, since the javadoc explicitly says:

If any execution of the task encounters an exception, subsequent executions are suppressed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM