xiangpei
2025-04-18 ccadf9480d4e6a9dcc227a2a0b1f9ae0612e36fd
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
package com.monkeylessey.job.utis;
 
import com.monkeylessey.enums.general.JobStatusEnum;
import com.monkeylessey.job.domain.entity.SysJob;
import org.quartz.*;
 
/**
 * @author 29443
 * @date 2022/6/9
 */
public class JobUtil {
 
    /**
     * 创建定时任务
     *
     * @param scheduler
     * @param job
     * @throws ClassNotFoundException
     * @throws SchedulerException
     */
    public static void createJob(Scheduler scheduler, SysJob job) throws ClassNotFoundException, SchedulerException {
        // 获取jib类的Class
        Class<? extends Job> jobClass = (Class<? extends Job>) Class.forName(job.getJobClass());
 
        // 创建任务实例
        JobDetail jobDetail = JobBuilder.newJob(jobClass)
                .withIdentity(job.getJobKey(), job.getJobGroup())
                .build();
        // 创建触发器
        CronTrigger trigger = TriggerBuilder.newTrigger()
                .forJob(job.getJobKey(), job.getJobGroup())
                .withIdentity(job.getJobKey(), job.getJobGroup())
                .withSchedule(CronScheduleBuilder.cronSchedule(job.getCronExpress()))
                .build();
 
        // 如果任务存在,删除再添加
        checkAndDeleteJob(scheduler, job);
        scheduler.scheduleJob(jobDetail, trigger);
        // 判断任务是否暂停状态
        if (JobStatusEnum.OFF.equals(job.getJobStatus())) {
            scheduler.pauseJob(new JobKey(job.getJobKey(), job.getJobGroup()));
        }
    }
 
    /**
     * 执行一次定时任务
     *
     * @param job
     */
    public static void executeOnce(Scheduler scheduler, SysJob job) throws SchedulerException {
        scheduler.triggerJob(new JobKey(job.getJobKey(), job.getJobGroup()));
    }
 
    /**
     * 暂停定时任务
     *
     * @param scheduler
     * @param job
     */
    public static void stopJob(Scheduler scheduler, SysJob job) throws SchedulerException {
        scheduler.pauseJob(new JobKey(job.getJobKey(), job.getJobGroup()));
    }
 
    /**
     * 启动定时任务
     *
     * @param scheduler
     * @param job
     */
    public static void startJob(Scheduler scheduler, SysJob job) throws SchedulerException {
        scheduler.resumeJob(new JobKey(job.getJobKey(), job.getJobGroup()));
    }
 
    /**
     * 检查任务是否存在,存在则删除该任务
     *
     * @param scheduler
     * @param job
     * @throws SchedulerException
     */
    public static void checkAndDeleteJob(Scheduler scheduler, SysJob job) throws SchedulerException {
        if (scheduler.checkExists(new JobKey(job.getJobKey(), job.getJobGroup()))) {
            deleteJob(scheduler, job);
        }
    }
 
    /**
     * 删除定时任务
     *
     * @param scheduler
     * @param job
     * @throws SchedulerException
     */
    public static void deleteJob(Scheduler scheduler, SysJob job) throws SchedulerException {
        scheduler.deleteJob(new JobKey(job.getJobKey(), job.getJobGroup()));
    }
}