package com.iailab.module.shasteel.job.init; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.iailab.module.shasteel.job.dao.ScheduleJobDao; import com.iailab.module.shasteel.job.entity.ScheduleJobEntity; import com.iailab.module.shasteel.job.utils.ScheduleUtils; import org.quartz.CronTrigger; import org.quartz.Scheduler; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.List; /** * 初始化定时任务数据 */ @Component public class JobCommandLineRunner implements CommandLineRunner { @Resource private Scheduler scheduler; @Resource private ScheduleJobDao scheduleJobDao; @Override public void run(String... args) { List scheduleJobList = scheduleJobDao.selectList(new QueryWrapper<>()); for(ScheduleJobEntity scheduleJob : scheduleJobList){ CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getId()); //如果不存在,则创建 if(cronTrigger == null) { ScheduleUtils.createScheduleJob(scheduler, scheduleJob); }else { ScheduleUtils.updateScheduleJob(scheduler, scheduleJob); } } } }