沙钢智慧能源系统后端代码
liriming
2024-12-02 8766cfc344d3635cd7e50a7c674cd5feb54d5a3d
提交 | 用户 | 时间
516ef4 1 package com.iailab.module.shasteel.job.init;
L 2
3 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4 import com.iailab.module.shasteel.job.dao.ScheduleJobDao;
5 import com.iailab.module.shasteel.job.entity.ScheduleJobEntity;
6 import com.iailab.module.shasteel.job.utils.ScheduleUtils;
7 import org.quartz.CronTrigger;
8 import org.quartz.Scheduler;
94c44e 9 import org.springframework.beans.factory.annotation.Autowired;
516ef4 10 import org.springframework.boot.CommandLineRunner;
L 11 import org.springframework.stereotype.Component;
12
13 import java.util.List;
14
15 /**
16  * 初始化定时任务数据
94c44e 17  *
D 18  * @author Mark sunlightcs@gmail.com
516ef4 19  */
L 20 @Component
21 public class JobCommandLineRunner implements CommandLineRunner {
94c44e 22     @Autowired
516ef4 23     private Scheduler scheduler;
94c44e 24     @Autowired
516ef4 25     private ScheduleJobDao scheduleJobDao;
L 26
27     @Override
28     public void run(String... args) {
29         List<ScheduleJobEntity> scheduleJobList = scheduleJobDao.selectList(new QueryWrapper<>());
30         for(ScheduleJobEntity scheduleJob : scheduleJobList){
31             CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getId());
32             //如果不存在,则创建
33             if(cronTrigger == null) {
34                 ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
35             }else {
36                 ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
37             }
38         }
39     }
40 }