MMA*_*ams 5 java spring spring-mvc scheduler scheduled-tasks
我希望在我启动webservice之后运行一个进程,然后每隔30分钟左右运行一次(我现在正以较小的延迟测试它,只是为了查看它是否有效),但我的进程从未运行过多次.我究竟做错了什么?
这是我的代码:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
Bas*_*que 12
run代码try catch只是一个猜测:抛出异常.一个ScheduledExecutorService暂停默默地如果遇到异常.
该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 Code Online (Sandbox Code Playgroud)
run方法采取婴儿步骤.从一个run除了a之外什么都不做的方法开始System.out.println.
| 归档时间: |
|
| 查看次数: |
2125 次 |
| 最近记录: |