QuartzUtil.java 4.19 KB
package com.subsidy.util;

import com.subsidy.common.exception.HttpException;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.DateBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static org.quartz.DateBuilder.futureDate;

@Component
public class QuartzUtil {

    @Autowired
    private Scheduler scheduler;

    /**
     * 添加定时任务,只执行一次的定时任务
     *
     * @param cls    执行类
     * @param date   时间
     * @param params 参数
     * @param name   定时器名称
     * @param group  定时器组名
     */
    public void addSimpleJob(Class cls, Date date, Map<String, Object> params, String name, String group) {
        try {
            JobKey key = new JobKey(name, group);
            List<Trigger> triggers = (List<Trigger>) scheduler.getTriggersOfJob(key);
            if (triggers.size() == 0) {
                int time = (int) (date.getTime() - System.currentTimeMillis()) / 1000;
                JobDataMap jobDataMap = new JobDataMap();
                jobDataMap.put("params", params);
                JobDetail jobDetail = JobBuilder.newJob(cls)
                        .withIdentity(name, group)
                        .usingJobData(jobDataMap)
                        .build();
                SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger()
                        .withIdentity(name, group)
                        .startAt(futureDate(time, DateBuilder.IntervalUnit.SECOND))
                        .build();
                scheduler.scheduleJob(jobDetail, trigger);
                if (!scheduler.isShutdown()) {
                    scheduler.start();
                }
            }
        } catch (Exception e) {
            throw new HttpException(70002);
        }
    }

    /**
     * 添加定时任务,循环不断执行的定时任务
     *
     * @param cls    执行类
     * @param cron   cron 表达式
     * @param params 参数
     * @param name   定时器名称
     * @param group  定时器组名
     */
    public void addCronJob(Class cls, String cron, Map<String, Object> params, String name, String group) {
        try {
            JobKey key = new JobKey(name, group);
            List<Trigger> triggers = (List<Trigger>) scheduler.getTriggersOfJob(key);
            if (triggers.size() == 0) {
                JobDataMap jobDataMap = new JobDataMap();
                jobDataMap.put("params", params);
                JobDetail jobDetail = JobBuilder.newJob(cls)
                        .withIdentity(name, group)
                        .usingJobData(jobDataMap)
                        .build();
                CronTrigger trigger = TriggerBuilder.newTrigger()
                        .withIdentity(name, group)
                        .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                        .build();
                scheduler.scheduleJob(jobDetail, trigger);
                if (!scheduler.isShutdown()) {
                    scheduler.start();
                }
            }
        } catch (Exception e) {
            throw new HttpException(70002);
        }
    }

    /**
     * 删除定时器
     *
     * @param name  定时器名称
     * @param group 定时器组名
     */
    public void deleteJob(String name, String group) {
        try {
            JobKey key = new JobKey(name, group);
            List<Trigger> triggers = (List<Trigger>) scheduler.getTriggersOfJob(key);
            if (triggers.size() > 0) {
                TriggerKey triggerKey = TriggerKey.triggerKey(name, group);
                scheduler.pauseTrigger(triggerKey);
                scheduler.unscheduleJob(triggerKey);
                scheduler.deleteJob(JobKey.jobKey(name, group));
            }
        } catch (Exception e) {
            throw new HttpException(70003);
        }
    }
}