3
我正在使用Quartz Scheduler在我的Java-App中啓動作業。我現在的工作HelloJob.java
如何使用石英調度程序使用多個cron-triggers啓動作業?
package com.stackoverflow.test.java;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job {
private String message;
public HelloJob() {
Date akt = new Date();
this.message = "Quartz is running (" + akt + ").";
}
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println(this.message);
}
}
我想開始使用兩種不同的cron表達式(這裏:*/5 */3 * * * ?
和*/9 */2 * * * ?
)這份工作。我試過如下:
package com.stackoverflow.test.java;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.JobBuilder;
import org.quartz.CronScheduleBuilder;
public class TestQuartz {
public static void main(String[] args) {
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
/* ################################################################### */
JobDetail job = JobBuilder
.newJob(HelloJob.class)
.withIdentity("job1", "group1")
.build();
CronScheduleBuilder sb1 = CronScheduleBuilder
.cronSchedule("*/5 */3 * * * ?");
CronScheduleBuilder sb2 = CronScheduleBuilder
.cronSchedule("*/9 */2 * * * ?");
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(sb1)
.build();
Trigger trigger2 = TriggerBuilder
.newTrigger()
.withIdentity("trigger2", "group1")
.startNow()
.withSchedule(sb2)
.build();
scheduler.scheduleJob(job, trigger);
scheduler.scheduleJob(job, trigger2);
/* ################################################################### */
Thread.sleep(10 * 60 * 1000);
System.out.println("FIN");
/* ################################################################### */
scheduler.shutdown();
} catch (SchedulerException se) {
se.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
我收到以下異常:
org.quartz.ObjectAlreadyExistsException: Unable to store Job : 'group1.job1', because one already exists with this identification.
at org.quartz.simpl.RAMJobStore.storeJob(RAMJobStore.java:279)
at org.quartz.simpl.RAMJobStore.storeJobAndTrigger(RAMJobStore.java:251)
at org.quartz.core.QuartzScheduler.scheduleJob(QuartzScheduler.java:886)
at org.quartz.impl.StdScheduler.scheduleJob(StdScheduler.java:249)
at com.stackoverflow.test.java.TestQuartz.main(TestQuartz.java:47)
是無法啓動一個工作與幾個觸發器?我可以以某種方式生成考慮多個cron表達式的計劃嗎?