私ができることは、両方のジョブを1つに融合させることですが、2つの異なるトリガーが同じジョブを指していることです。各トリガにはそれぞれ独自の発射時間があり、パラメータデータは現在、ジョブデータマップではなく、各トラガーのデータマップに保持されています。また、失火ポリシーは、これはコードですMisfireHandlingInstructionFireAndProceed
に変更されました:
public abstract class MessagePrinter {
public MessagePrinter() {
}
public abstract void print();
}
:ここ
public class QuartzTest {
public static final String PROCESS_TRIGGER_MAP_KEY = "process";
public static void main(String[] args) throws SchedulerException, InterruptedException {
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();
JobDetail job1 = newJob(TestJob.class).withIdentity("job1", "group1").build();
CronTrigger trigger1 = newTrigger().withIdentity("trigger1", "group1").startAt(new Date()).withSchedule(cronSchedule(getCronExpression(1)).withMisfireHandlingInstructionFireAndProceed()).build();
trigger1.getJobDataMap().put(PROCESS_TRIGGER_MAP_KEY, new MessagePrinter() {
@Override
public void print() {
System.out.println(new Timestamp(System.currentTimeMillis()) + " This is process 1");
}
});
scheduler.scheduleJob(job1, trigger1);
CronTrigger trigger2 = newTrigger().withIdentity("trigger2", "group1").forJob(job1).startAt(new Date()).withSchedule(cronSchedule(getCronExpression(2)).withMisfireHandlingInstructionFireAndProceed()).build();
trigger2.getJobDataMap().put(PROCESS_TRIGGER_MAP_KEY, new MessagePrinter() {
@Override
public void print() {
System.out.println(new Timestamp(System.currentTimeMillis()) + " This is process 2");
}
});
scheduler.scheduleJob(trigger2);
Thread.sleep(5 * 60 * 1000);
}
private static String getCronExpression(int interval) {
return "0 */" + interval + " * * * ?";
}
}
は、ジョブ・クラス
@DisallowConcurrentExecution
public class TestJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
MessagePrinter mp = (MessagePrinter) context.getTrigger().getJobDataMap().get(QuartzTest.PROCESS_TRIGGER_MAP_KEY);
if (mp != null) {
mp.print();
} else {
System.out.println(new Timestamp(System.currentTimeMillis()) + " Job started");
}
System.out.println(new Timestamp(System.currentTimeMillis()) + " Job sleeping 10s...");
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(new Timestamp(System.currentTimeMillis()) + " Job finished.");
}
}
とプロセッサクラスです