我想重新安排石英作业。我经历了一些提出的答案(例如How to reschedule the job execution interval in Quartz?)。
我想做的是:
计划在给定时间运行
重新安排作业,使其每分钟重复两次。
因此,一般来说,该作业应被触发3次。
我的主班:
public class Main{
public static void main(String[] args) throws Exception {
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("dummyJobName", "group1").build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("dummyTriggerName", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0 41 16 1/1 * ? *"))
.build();
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
}
和Job类:
public class HelloJob implements Job{
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("Hello Quartz! " + context.getTrigger().toString());
Trigger newTrigger = TriggerBuilder.newTrigger()
.withIdentity("dummyTriggerName", "group1")
.withSchedule(
SimpleScheduleBuilder.repeatMinutelyForTotalCount(2))
.build();
Trigger oldTrigger = context.getTrigger();
Scheduler scheduler = null;
try {
scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.rescheduleJob(oldTrigger.getKey(), newTrigger);
} catch (SchedulerException e1) {
e1.printStackTrace();
}
}
}
当我运行代码时,它会给我:
Hello Quartz! Trigger 'group1.dummyTriggerName': triggerClass: 'org.quartz.impl.triggers.SimpleTriggerImpl calendar: 'null' misfireInstruction: 0 nextFireTime: Sun Jun 02 16:42:00 CEST 2019
Hello Quartz! Trigger 'group1.dummyTriggerName': triggerClass: 'org.quartz.impl.triggers.SimpleTriggerImpl calendar: 'null' misfireInstruction: 0 nextFireTime: Sun Jun 02 16:42:00 CEST 2019
Hello Quartz! Trigger 'group1.dummyTriggerName': triggerClass: 'org.quartz.impl.triggers.SimpleTriggerImpl calendar: 'null' misfireInstruction: 0 nextFireTime: Sun Jun 02 16:42:00 CEST 2019
Hello Quartz! Trigger 'group1.dummyTriggerName': triggerClass: 'org.quartz.impl.triggers.SimpleTriggerImpl calendar: 'null' misfireInstruction: 0 nextFireTime: Sun Jun 02 16:42:00 CEST 2019
Hello Quartz! Trigger 'group1.dummyTriggerName': triggerClass: 'org.quartz.impl.triggers.SimpleTriggerImpl calendar: 'null' misfireInstruction: 0 nextFireTime: Sun Jun 02 16:42:00 CEST 2019
Hello Quartz! Trigger 'group1.dummyTriggerName': triggerClass: 'org.quartz.impl.triggers.SimpleTriggerImpl calendar: 'null' misfireInstruction: 0 nextFireTime: Sun Jun 02 16:42:00 CEST 2019
Hello Quartz! Trigger 'group1.dummyTriggerName': triggerClass: 'org.quartz.impl.triggers.SimpleTriggerImpl calendar: 'null' misfireInstruction: 0 nextFireTime: Sun Jun 02 16:42:00 CEST 2019
Hello Quartz! Trigger 'group1.dummyTriggerName': triggerClass: 'org.quartz.impl.triggers.SimpleTriggerImpl calendar: 'null' misfireInstruction: 0 nextFireTime: Sun Jun 02 16:42:00 CEST 2019
Hello Quartz! Trigger 'group1.dummyTriggerName': triggerClass: 'org.quartz.impl.triggers.SimpleTriggerImpl calendar: 'null' misfireInstruction: 0 nextFireTime: Sun Jun 02 16:42:00 CEST 2019
Hello Quartz! Trigger 'group1.dummyTriggerName': triggerClass: 'org.quartz.impl.triggers.SimpleTriggerImpl calendar: 'null' misfireInstruction: 0 nextFireTime: Sun Jun 02 16:42:00 CEST 2019
Hello Quartz! Trigger 'group1.dummyTriggerName': triggerClass: 'org.quartz.impl.triggers.SimpleTriggerImpl calendar: 'null' misfireInstruction: 0 nextFireTime: Sun Jun 02 16:42:00 CEST 2019
Hello Quartz! Trigger 'group1.dummyTriggerName': triggerClass: 'org.quartz.impl.triggers.SimpleTriggerImpl calendar: 'null' misfireInstruction: 0 nextFireTime: Sun Jun 02 16:42:00 CEST 2019
......
这在控制台中就像每秒重复多次,并且无法停止。原始的触发器会按预期触发(来自Main类),但是在HelloJob类中重新安排时,它会发疯。
我不明白为什么会这样。我在做什么错了?