Java ScheduledExecutorService 在 servlet 初始化后停止了一段时间
Java ScheduledExecutorService stopped some time after servlet initialization
在我的 Tomcat
服务器中实现了一个功能:基本上 ScheduledExecutorService
将在 HttpServlet
初始化时定期设置为 运行 任务。代码如下:
@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);
}
如果servlet初始化成功,任务将永远保持运行ning每5秒一次。在 DeploymentsDataHandler
的 run
方法中,一开始会记录如下消息:
@Override
public void run() {
LOGGER.info("Now begin a new round of refreshing deployment data...");
this.doWorkFlow();
}
如果我错了请纠正我:无论 doWorkFlow
方法中是否有任何未处理的异常,该消息都应始终每 5 分钟记录一次。但事实是这个调度器 运行 正常运行了一段时间突然停止工作,甚至停止记录消息。任何可能的解释?我要如何处理这种情况,因为我想确保此调度程序服务始终处于 运行ning 状态。或者有什么办法可以调试这个问题?
我假设以下代码将确保 Scheduler
即使在 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
不,因为 the javadoc 明确表示:
If any execution of the task encounters an exception, subsequent executions are suppressed
在我的 Tomcat
服务器中实现了一个功能:基本上 ScheduledExecutorService
将在 HttpServlet
初始化时定期设置为 运行 任务。代码如下:
@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);
}
如果servlet初始化成功,任务将永远保持运行ning每5秒一次。在 DeploymentsDataHandler
的 run
方法中,一开始会记录如下消息:
@Override
public void run() {
LOGGER.info("Now begin a new round of refreshing deployment data...");
this.doWorkFlow();
}
如果我错了请纠正我:无论 doWorkFlow
方法中是否有任何未处理的异常,该消息都应始终每 5 分钟记录一次。但事实是这个调度器 运行 正常运行了一段时间突然停止工作,甚至停止记录消息。任何可能的解释?我要如何处理这种情况,因为我想确保此调度程序服务始终处于 运行ning 状态。或者有什么办法可以调试这个问题?
我假设以下代码将确保 Scheduler
即使在 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
不,因为 the javadoc 明确表示:
If any execution of the task encounters an exception, subsequent executions are suppressed