Quartz 工具类

public class JobUtil {
  /**
   * 添加基于日历的任务
   *
   * @param scheduler 调度器
   * @param identity 标识
   * @param clazz 任务类
   * @param cron 表达式
   * @param jobDataMap 任务数据
   * @throws SchedulerException 调度器异常
   */
  public static void addCronJob(Scheduler scheduler, String identity, Class clazz,
      String cron, JobDataMap jobDataMap) throws SchedulerException {

    Trigger trigger = TriggerBuilder.newTrigger()
        .withSchedule(CronScheduleBuilder
            .cronSchedule(cron))
        .startNow()
        .build();
    addJob(scheduler, buildJobDetail(identity, clazz, jobDataMap), trigger);
  }


  /**
   * 添加基于日历的任务
   *
   * @param scheduler 调度器
   * @param identity 标识
   * @param clazz 任务类
   * @param cron 表达式
   * @throws SchedulerException 调度器异常
   */
  public static void addCronJob(Scheduler scheduler, String identity, Class clazz,
      String cron) throws SchedulerException {

    addCronJob(scheduler, identity, clazz, cron, null);
  }

  /**
   * 添加基于日历的任务
   *
   * @param scheduler 调度器
   * @param clazz 任务类
   * @param cron 表达式
   * @param jobDataMap 任务数据
   * @throws SchedulerException 调度器异常
   */
  public static void addCronJob(Scheduler scheduler, Class clazz, String cron, JobDataMap jobDataMap)
      throws SchedulerException {
    addCronJob(scheduler, null, clazz, cron, jobDataMap);
  }

  /**
   * 添加基于日历的任务
   *
   * @param scheduler 调度器
   * @param clazz 任务类
   * @param cron 表达式
   * @throws SchedulerException 调度器异常
   */
  public static void addCronJob(Scheduler scheduler, Class clazz, String cron)
      throws SchedulerException {
    addCronJob(scheduler, null, clazz, cron, null);
  }


  /**
   * 添加基于定时的任务
   *
   * @param scheduler 调度器
   * @param identity 标识
   * @param clazz 任务类
   * @param interval 间隔秒数
   * @param jobDataMap 任务数据
   * @throws SchedulerException 调度器异常
   */
  public static void addSimpleJob(Scheduler scheduler, String identity, Class clazz, int interval,
      JobDataMap jobDataMap)
      throws SchedulerException {
    Trigger trigger = TriggerBuilder.newTrigger()
        .withSchedule(SimpleScheduleBuilder.simpleSchedule()
            .withIntervalInSeconds(interval).repeatForever())
        .startNow()
        .build();
    addJob(scheduler, buildJobDetail(identity, clazz, jobDataMap), trigger);
  }


  /**
   * 添加基于定时的任务
   *
   * @param scheduler 调度器
   * @param identity 标识
   * @param clazz 任务类
   * @param interval 间隔秒数
   * @throws SchedulerException 调度器异常
   */
  public static void addSimpleJob(Scheduler scheduler, String identity, Class clazz, int interval)
      throws SchedulerException {
    addSimpleJob(scheduler, identity, clazz, interval, null);
  }


  /**
   * 添加基于定时的任务
   *
   * @param scheduler 调度器
   * @param clazz 任务类
   * @param interval 间隔秒数
   * @param jobDataMap 任务数据
   * @throws SchedulerException 调度器异常
   */
  public static void addSimpleJob(Scheduler scheduler, Class clazz, int interval, JobDataMap jobDataMap)
      throws SchedulerException {
    addSimpleJob(scheduler, null, clazz, interval, jobDataMap);
  }


  /**
   * 添加基于定时的任务
   *
   * @param scheduler 调度器
   * @param clazz 任务类
   * @param interval 间隔秒数
   * @throws SchedulerException 调度器异常
   */
  public static void addSimpleJob(Scheduler scheduler, Class clazz, int interval)
      throws SchedulerException {
    addSimpleJob(scheduler, null, clazz, interval, null);
  }


  private static JobDetail buildJobDetail(String identity, Class clazz, JobDataMap jobDataMap) {
    JobBuilder jobBuilder = JobBuilder.newJob(clazz);
    if (!Assert.isEmpty(identity)) {
      jobBuilder.withIdentity(identity);
    }
    if (jobDataMap != null) {
      jobBuilder.setJobData(jobDataMap);
    }
    return jobBuilder.build();
  }

  private static void addJob(Scheduler scheduler, JobDetail jobDetail, Trigger trigger) throws SchedulerException {
    if (!scheduler.checkExists(jobDetail.getKey())) {
      scheduler.scheduleJob(jobDetail, trigger);
    }
  }
}

你可能感兴趣的:(quartz)