Blame view

QuartzUtil.java 4.08 KB
涂亚平 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
package com.subsidy.util;

import com.subsidy.common.exception.HttpException;
import org.quartz.*;
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;

    public static void main(String[] args) {
        System.out.println(DateFormatUtil.parse("2022-06-28 11:30:47", "yyyy-MM-dd HH:mm:ss").getTime() -System.currentTimeMillis());
    }

    /**
     * 添加定时任务,只执行一次的定时任务
     *
     * @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) {
                Long time = (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(Math.toIntExact(time), DateBuilder.IntervalUnit.SECOND))
                        .build();
                scheduler.scheduleJob(jobDetail, trigger);
                if (!scheduler.isShutdown()) {
                    scheduler.start();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            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);
        }
    }
}