具有特定启动时间的Quartz单作业多个触发器不起作用

时间:2013-03-13 16:37:55

标签: java quartz-scheduler

我正在尝试编写一个简单的程序,它在特定的日期时间运行相同的作业,输入文件的日期时间列表,这是我的代码:

SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler sched = schedFact.getScheduler();

sched.start();

Map<JobDetail, List<Trigger>> jobAndTriggers = new HashMap<JobDetail, List<Trigger>> ();
List<Trigger> triggers = new ArrayList<Trigger> ();
JobDetail jobDetail = newJob (TestJob.class)
    .withIdentity(Constants.TEST_JOB_PREFIX, onstants.TEST_JOB_GRUOP_PREFIX)
    .build();

Trigger trigger = null;
int triggerIndex = 1;

for (Date date : dates) {
    trigger = newTrigger ()
       .withIdentity(Constants.SIMPLE_TRIGGER_PREFIX + triggerIndex, Constants.SIMPLE_TRIGGER_GROUP_PREFIX)
       .startAt(date)
       .withSchedule(simpleSchedule().withRepeatCount(0))
       .build();

   triggers.add(trigger);
}
jobAndTriggers.put(jobDetail, triggers);

sched.scheduleJobs (jobAndTriggers, true);

现在,当程序运行时,比如当我传递日期时间列表时: 2013-03-13 12:21:33 | 2013-03-13 12:21:37 | 2013-03-13 12:22:41 | 2013-03-13 12:22:45 | 2013-03-13 12:23:49 |

这个工作只会被解雇一次,这是最后一个时间戳,我不确定我做错了什么,感谢任何帮助。以下是一些程序日志:

12:19:33.265 [main] INFO  org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
12:19:33.265 [main] INFO  org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.1.7
12:19:33.265 [main] INFO  org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
12:19:33.265 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
12:19:33.265 [main] DEBUG c.s.r.s.s.TriggerAndJobBuilder - Triggers created:
12:19:33.265 [main] DEBUG c.s.r.s.s.TriggerAndJobBuilder - SIMPLE_TRIGGER_GROUP.SIMPLE_TRIGGER1 which will fire job at 2013-03-13 12:21:33
12:19:33.265 [main] DEBUG c.s.r.s.s.TriggerAndJobBuilder - SIMPLE_TRIGGER_GROUP.SIMPLE_TRIGGER1 which will fire job at 2013-03-13 12:21:37
12:19:33.265 [main] DEBUG c.s.r.s.s.TriggerAndJobBuilder - SIMPLE_TRIGGER_GROUP.SIMPLE_TRIGGER1 which will fire job at 2013-03-13 12:22:41
12:19:33.265 [main] DEBUG c.s.r.s.s.TriggerAndJobBuilder - SIMPLE_TRIGGER_GROUP.SIMPLE_TRIGGER1 which will fire job at 2013-03-13 12:22:45
12:19:33.265 [main] DEBUG c.s.r.s.s.TriggerAndJobBuilder - SIMPLE_TRIGGER_GROUP.SIMPLE_TRIGGER1 which will fire job at 2013-03-13 12:23:49
12:19:33.265 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
12:19:34.109 [Timer-0] DEBUG org.quartz.utils.UpdateChecker - Checking for available updated version of Quartz...
12:19:34.265 [Timer-1] DEBUG org.quartz.utils.UpdateChecker - Checking for available updated version of Quartz...
12:19:49.265 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG o.q.simpl.PropertySettingJobFactory - Producing instance of Job 'TEST_GROUP.TEST_PING_JOB', class=com.test.TestJob
12:19:49.265 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
12:23:49.265 [DefaultQuartzScheduler_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job TEST_GROUP.TEST_PING_JOB
12:23:49.265 [DefaultQuartzScheduler_Worker-1] INFO  c.s.r.s.s.TestJob - Entered TestJob execute method at 2013-03-13 12:23:49

1 个答案:

答案 0 :(得分:0)

也许您一直在为所有触发器分配相同的标识符,因此它是实际被考虑的最后一个触发器。

triggerIndex = 1;
`Constants.SIMPLE_TRIGGER_PREFIX + triggerIndex`

将此更改为唯一的内容,并且您在列表中发送的每个日期都会有不同的触发器。