【发布时间】:2017-10-17 08:11:49
【问题描述】:
我希望在启动我的网络服务后运行一个进程,然后每隔 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() 方法会返回吗?
标签: java spring spring-mvc scheduled-tasks scheduler