好的,我已经浏览了所有我能找到的东西,但还没有找到我的问题的答案。有几个项目很接近,但还不够接近。我有一个在 Tomcat 7 中运行的基于 Spring 3.0.7 的 Web 应用程序。在 applicationContext.xml 中,我有:
<task:scheduler id="scheduledReportsScheduler" pool-size="1"/>
<task:scheduled-tasks scheduler="scheduledReportsScheduler">
<task:scheduled ref="scheduledReportsQueuer" method="process" cron="0 */1 * * * *"/>
</task:scheduled-tasks>
<bean id="scheduledReportsQueuer" class="com.foo.scheduledServices.ScheduledReportsQueuerService"></bean>
这将每分钟执行一次我的 ScheduledReportsQueuerService 类。这很好用。然后在该类中,我检查了一些数据库表,如果尚未安排任务,则尝试根据 CronTrigger 安排任务:
if(!_workers.containsKey(schedule.getRptScheduleId())){
_logger.debug(Thread.currentThread().getName() + " Creating and scheduling ScheduledReportQueuerWorker for scheduled report ID: "+schedule.getRptScheduleId()+"("+schedule.getReportName()+") "+schedule.getCronPattern());
ScheduledReportQueuerWorker newWorker = new ScheduledReportQueuerWorker(schedule);
newWorker.setRptRequestDAO(getRptRequestDao());
newWorker.setRptScheduleDAO(getRptScheduleDao());
_workers.put(schedule.getRptScheduleId(), newWorker);
ScheduledFuture<?> newFuture = _scheduler.schedule(newWorker, new CronTrigger(schedule.getCronPattern()));
_futures.put(schedule.getRptScheduleId(), newFuture);
}
private ConcurrentTaskScheduler _scheduler;
这似乎也可以在执行新任务(上面的 newWorker)时起作用。问题是它们在正确的触发时间被多次执行。例如,如果一个新任务的 cron 模式为 '* */2 * * * *',它应该每 2 分钟执行一次。相反,该任务每 2 分钟连续执行 50-60 次。
这让我发疯了,你能提供的任何帮助都会很棒。
更多信息 这是一些日志输出,您可以看到基于 XML 的线程已启动,它告诉我们正在为每两分钟运行一份报告创建一个线程。然后您会看到报告每两分钟连续运行 60 次。
// here it schedules a single report to be run every 2 minutes
2012-11-19 08:30:02,876 DEBUG - ScheduledReportsQueuerService - scheduledReportsScheduler-1 Creating and scheduling ScheduledReportQueuerWorker for scheduled report ID: 20182(Jasper Test Report 1) * */2 * * * *
// here the worker thread gets kicked off 57 times
2012-11-19 08:30:03,016 INFO - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)
...
2012-11-19 08:30:59,012 INFO - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)
// two minutes later the worker thread gets kicked off 60 times
2012-11-19 08:32:00,017 INFO - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)
...
2012-11-19 08:32:59,003 INFO - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)
// two minutes later the worker thread gets kicked off 60 times
2012-11-19 08:34:00,019 INFO - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)
...
2012-11-19 08:34:59,014 INFO - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)
// two minutes later the worker thread gets kicked off 60 times
2012-11-19 08:36:00,010 INFO - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)
...
2012-11-19 08:36:59,006 INFO - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)