k94314517
2024-05-15 13b58d0fc355b6256d165cc2ae6a571ffa8e778d
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
package com.doumee.service.timer;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.doumee.dao.timer.entity.JobState;
import com.doumee.dao.timer.entity.QuartzJob;
import com.doumee.dao.timer.mapper.QuartzJobMapper;
import com.doumee.dao.timer.scheduler.QuartzManage;
import org.quartz.CronTrigger;
import org.springframework.stereotype.Service;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
 
/**
 * @author 公众号:知了一笑
 * @since 2023-07-26 11:08
 */
@Service
public class QuartzJobService {
 
    @Resource
    private QuartzJobMapper quartzJobMapper ;
 
    @Resource
    private QuartzManage quartzManage;
 
    /**
     * 任务主键查询
     */
    public QuartzJob getById(Integer id) {
        return quartzJobMapper.selectById(id) ;
    }
 
    /**
     * 新增任务
     */
    public int insert(QuartzJob quartzJob) {
        int flag = quartzJobMapper.insert(quartzJob) ;
        if (flag > 0){
            quartzManage.createJob(quartzJob) ;
        }
        return flag;
    }
 
    /**
     * 更新任务
     */
    public int update(QuartzJob quartzJob) {
        int flag = quartzJobMapper.updateById(quartzJob);
        if (flag > 0){
            quartzManage.updateJob(quartzJob);
        }
        return flag ;
    }
 
    /**
     * 暂停任务
     */
    public void pause(Integer id) {
        QuartzJob quartzJob = quartzJobMapper.selectById(id) ;
        if (!Objects.isNull(quartzJob)){
            quartzJob.setState(JobState.JOB_STOP.getStatus());
            if (quartzJobMapper.updateById(quartzJob)>0){
                quartzManage.checkStop(quartzJob);
            }
        }
    }
 
    /**
     * 恢复任务
     */
    public void resume(Integer id) {
        QuartzJob quartzJob = quartzJobMapper.selectById(id) ;
        if (!Objects.isNull(quartzJob)){
            quartzJob.setState(JobState.JOB_RUN.getStatus());
            if (quartzJobMapper.updateById(quartzJob)>0){
                quartzManage.resumeJob(id);
            }
        }
    }
 
    /**
     * 执行任务一次
     */
    public void runOnce(Integer id) {
        QuartzJob quartzJob = quartzJobMapper.selectById(id) ;
        if (!Objects.isNull(quartzJob) && quartzJob.getState() != JobState.JOB_DEL.getStatus()){
            quartzManage.run(quartzJob);
        }
    }
}