All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.litongjava.tio.utils.quartz.QuartzUtils Maven / Gradle / Ivy

There is a newer version: 3.7.3.v202400213-RELEASE
Show newest version
package com.litongjava.tio.utils.quartz;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.litongjava.tio.utils.hutool.ResourceUtil;
import com.litongjava.tio.utils.hutool.StrUtil;

/**
 * 对Quartz的封装, 使用方法见:https://my.oschina.net/talenttan/blog/1550826
 * @author tanyaowu 
 * 2017年10月8日 下午3:20:23
 */
public class QuartzUtils {
  private static Logger log = LoggerFactory.getLogger(QuartzUtils.class);

  /**
   * 默认的配置文件
   */
  private static final String DEFAULT_FILE = "config/tio-quartz.properties";

  private static String file = DEFAULT_FILE;

  private static Scheduler scheduler;

  /**
   * 
   * @author: tanyaowu
   */
  private QuartzUtils() {
  }

  /**
   * 
   */
  private static final List JOB_CLASSES = new ArrayList<>(10);

  public static Scheduler getScheduler() {
    return scheduler;
  }

  /**
   * 配置文件为"/config/tio-quartz.properties"
   * 
   * @author: tanyaowu
   */
  public static void start() {
    start(null);
  }

  /**
   * 
   * @param file1 形如:"/config/tio-quartz.properties"
   * @author: tanyaowu
   */
  public static void start(String file1) {
    if (StrUtil.isBlank(file1)) {
      file = DEFAULT_FILE;
    } else {
      file = file1;
    }
    initJobClasses();
    if (JOB_CLASSES.size() <= 0) {
      log.error("File [{}] do not have any task", file);
      return;
    }
    try {
      scheduler = StdSchedulerFactory.getDefaultScheduler();
      int index = 1;
      for (QuartzTimeVo quartzTimeVo : JOB_CLASSES) {
        try {
          @SuppressWarnings("unchecked")
          Class clazzz = (Class) Class.forName(quartzTimeVo.getClazz());
          // @SuppressWarnings("unchecked")
          JobDetail job = JobBuilder.newJob(clazzz).withIdentity("job-" + index, "group-" + index).build();
          CronTrigger trigger = newTrigger().withIdentity("trigger-" + index, "group-" + index)
              .withSchedule(cronSchedule(quartzTimeVo.getCron())).build();

          @SuppressWarnings("unused")
          Date d = scheduler.scheduleJob(job, trigger);
          log.info("A scheduled task has been started:{}, cron:{}", clazzz.getName(), trigger.getCronExpression());

        } catch (ClassNotFoundException e) {
          log.error(e.toString(), e);
        } finally {
          index++;
        }
      }
      scheduler.start();
    } catch (SchedulerException e) {
      log.error(e.toString(), e);
      throw new RuntimeException(e);
    }
  }

  /**
   * 初始化QuartzTimeVo列表
   * 
   * @author: tanyaowu
   */
  private static void initJobClasses() {
    Properties props = new Properties();
    try {
      props.load(ResourceUtil.getResourceAsStream(file));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    Set> set = props.entrySet();// .keySet();
    if (set != null && set.size() > 0) {
      for (Entry entry : set) {

        String clazz = StrUtil.trim((String) entry.getKey());
        String cron = StrUtil.trim((String) entry.getValue());

        QuartzTimeVo quartzTimeVo = new QuartzTimeVo(clazz, cron);
        JOB_CLASSES.add(quartzTimeVo);
      }
    }
  }

  public static void stop() {
    try {
      scheduler.shutdown();
    } catch (SchedulerException e) {
      e.printStackTrace();
    }
  }

  private static class QuartzTimeVo {
    private String clazz = null;
    private String cron = null;

    public QuartzTimeVo(String clazz, String cron) {
      super();
      this.clazz = clazz;
      this.cron = cron;
    }

    public String getClazz() {
      return clazz;
    }

    public String getCron() {
      return cron;
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy