[英]ScheduledExecutorService only runs once
I want a process to run after I start my webservice, and then every 30 minutes or so afterwards, (I'm testing it with a smaller delay for now, just to see if it works), but my process never runs more than once.我希望在启动 Web 服务后运行一个进程,然后每 30 分钟左右运行一次,(我现在正在以较小的延迟测试它,只是为了看看它是否有效),但我的进程从未运行过一次. What am I doing wrong?我究竟做错了什么?
Here is my code:这是我的代码:
@WebListener
public class SchedulerService implements ServletContextListener{
@Autowired
UpdateSubscriberService updateSubscriberService;
ScheduledExecutorService scheduledExecService;
public SchedulerService(){
scheduledExecService = Executors.newSingleThreadScheduledExecutor();
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
scheduledExecService.shutdown();
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
scheduledExecService.scheduleWithFixedDelay(new Runnable(){
@Override
public void run() {
Date date = new Date(System.currentTimeMillis());
System.out.println("Running scheduled update check " + date.toString());
updateSubscriberService.checkForUpdates();
}
}, 60, 30, TimeUnit.SECONDS);
}
}
See this longer Answer of mine on a similar Question.在一个类似的问题上看到我的这个更长的答案。
run
code with try catch
用try catch
包装run
代码Just a guess: An exception is being thrown.只是一个猜测:正在抛出异常。 A ScheduledExecutorService
halts silently if it encounters an Exception, with no further scheduled work performed.如果ScheduledExecutorService
遇到异常,则它会静默停止,不再执行任何计划的工作。
The run
method's code should always be surrounded by a try-catch to handle and absorb any thrown Exception. run
方法的代码应该始终被 try-catch 包围,以处理和吸收任何抛出的异常。
@Override
public void run() {
try { // Let no Exception reach the ScheduledExecutorService.
Date date = new Date(System.currentTimeMillis());
System.out.println("Running scheduled update check " + date.toString());
updateSubscriberService.checkForUpdates();
} catch ( Exception e ) {
System.out.println( "ERROR - unexpected exception" );
}
}
run
method存根run
方法Take baby steps.采取婴儿的步骤。 Begin with a run
method that does nothing but a System.out.println
.从一个只run
System.out.println
的run
方法开始。
Just in case you are ever in a position where the code MUST run once every-so-many-seconds even if the last run hasn't completed yet (which can be very dangerous if not managed properly), you can launch your process inside a different thread inside the timer.以防万一您处于代码必须每隔几秒运行一次的位置,即使最后一次运行尚未完成(如果管理不当,这可能非常危险),您可以在内部启动您的流程计时器内的不同线程。 Here is sample code.这是示例代码。
ScheduledExecutorService scheduledExecService = newSingleThreadScheduledExecutor();
scheduledExecService.scheduleWithFixedDelay(new Runnable()
{
@Override
public void run()
{
// This should be in a try-catch because any error here
// will stop the recurrence
try
{
// The timer will only repeat if the last run is finished. So
// we put each new process in a different thread than the timer
// itself, so the last timer call "finishes" as soon as the process
// leaves the timer's thread.
Thread t = new Thread(new Runnable()
{
public void run()
{
try
{
android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
MyProcessThatShouldRunEverySoManySecondsNoMatterWhat();
}
catch (Exception erTimerThread)
{
Log.e("Error", erTimerThread.toString());
}
}
});
t.setPriority(2);
t.start();
}
catch (Exception erTimer)
{
Log.e("Error", erTimer.toString());
}
}
}, 0, 60, java.util.concurrent.TimeUnit.SECONDS);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.