cigs4/ym-schedule-task/src/main/java/com/cnbm/scheduletask/init/JobCommandLineRunner.java
2022-06-24 10:25:17 +08:00

40 lines
1.2 KiB
Java

package com.cnbm.scheduletask.init;
import com.cnbm.scheduletask.dao.ScheduleJobDao;
import com.cnbm.scheduletask.entity.ScheduleJobEntity;
import com.cnbm.scheduletask.utils.ScheduleUtils;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @Author weihongyang
* @Date 2022/6/23 4:46 PM
* @Version 1.0
*/
@Component
public class JobCommandLineRunner implements CommandLineRunner {
@Autowired
private Scheduler scheduler;
@Autowired
private ScheduleJobDao scheduleJobDao;
@Override
public void run(String... args) {
List<ScheduleJobEntity> scheduleJobList = scheduleJobDao.selectList(null);
for(ScheduleJobEntity scheduleJob : scheduleJobList){
CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getId());
//如果不存在,则创建
if(cronTrigger == null) {
ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
}else {
ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
}
}
}
}