Spring 3.0.7 CronTrigger在触发时多次执行任务

时间:2012-11-06 16:50:20

标签: spring schedule crontrigger

好的,我已经查看了我能找到的所有内容,但没有找到我的问题的答案。一些物品很接近但不够近。我有一个在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的线程开始,它告诉我们我们正在创建一个线程,每两分钟运行一个报告。然后你会看到该报告连续两次运行了两次。

// 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 * * * *)

2 个答案:

答案 0 :(得分:5)

知道了。现在我觉得很蠢。结果我不理解cron模式。我用了

* */2 * * * * 

认为应该每两分钟运行一次。真正做的是每两分钟运行一次,因为自上一次触发以来已经过去了。正确的模式是:

0 */2 * * * *

答案 1 :(得分:0)

您计划将更多任务安排为计划任务的一部分吗?

xml配置中的原始cron触发器将以无限的方式运行...您真的想要安排更多任务吗?