如何使用quartz-scheduler使用多个cron-triggers启动Job?

时间:2015-04-02 11:53:05

标签: java cron quartz-scheduler

我使用Quartz Scheduler在我的Java-App中启动作业。我有工作HelloJob.java

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)

用几个触发器启动一个Job是不可能的?我可以以某种方式生成一个考虑多个cron表达式的计划吗?

1 个答案:

答案 0 :(得分:2)

您必须使用TriggerBuilder的{​​{1}}方法为作业分配更多触发器。

forJob(JobKey jobKey)

在您的情况下,请应用以下修改:

Trigger trigger = TriggerBuilder.newTrigger().forJob(job.getKey()). ...
scheduler.scheduleJob(trigger)