潘志宝
2024-08-20 92d87eda945d68346475e6766673a3cdccf3d41c
提交 | 用户 | 时间
a6de49 1 /**
H 2  * Copyright (c) 2018 人人开源 All rights reserved.
3  *
4  * https://www.renren.io
5  *
6  * 版权所有,侵权必究!
7  */
8
9 package com.iailab.module.job.init;
10
11 import com.iailab.module.job.dao.ScheduleJobDao;
12 import com.iailab.module.job.entity.ScheduleJobEntity;
13 import com.iailab.module.job.utils.ScheduleUtils;
14 import org.quartz.CronTrigger;
15 import org.quartz.Scheduler;
16 import javax.annotation.Resource;
17 import org.springframework.boot.CommandLineRunner;
18 import org.springframework.stereotype.Component;
19
20 import java.util.List;
21
22 /**
23  * 初始化定时任务数据
24  *
25  * @author Mark sunlightcs@gmail.com
26  */
27 @Component
28 public class JobCommandLineRunner implements CommandLineRunner {
29     @Resource
30     private Scheduler scheduler;
31     @Resource
32     private ScheduleJobDao scheduleJobDao;
33
34     @Override
35     public void run(String... args) {
36         List<ScheduleJobEntity> scheduleJobList = scheduleJobDao.selectList(null);
37         for(ScheduleJobEntity scheduleJob : scheduleJobList){
38             CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getId());
39             //如果不存在,则创建
40             if(cronTrigger == null) {
41                 ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
42             }else {
43                 ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
44             }
45         }
46     }
47 }