目的:开发中遇上多个定时任务,并且每个定时任务时间都是用数据库表管理的
我们在修改表的定时任务时间时,也会同步更新程序中该任务的执行时间。
步骤
1.定时刷新数据库表中数据,如果是新增的数据则创建新的任务,如果只是针对时间修改的任务,则刷新改任务的时间
2.网络上的很多都是一个任务,没谈论到多个任务的案例
-------------------------------------------
第一步:创建一个任务处理类
@Slf4j
@Component
public class ScheduledChannelWarn implements Job {
@Resource
private PublicChannelWarnService publicChannelWarnService;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap jobDataMap = context.getMergedJobDataMap();
//这个位置是获取下一步保存的数据,写自己需要的业务
String warn=jobDataMap.getString("warn");
log.info("warn: "+warn);
//PublicChannelWarn ChannelWarn = JSONUtil.toBean(warn, PublicChannelWarn.class);
//if(ConstantsObject.ONE_STRING.equals(ChannelWarn.getWarnType())){
//publicChannelWarnService.warnClientUserJob(ChannelWarn);
// }
//通道警告
//if(ConstantsObject.TWO_STRING.equals(ChannelWarn.getWarnType())){
//publicChannelWarnService.warnChannelJob(ChannelWarn);
//}
//接入点警告
//if(ConstantsObject.THREE_STRING.equals(ChannelWarn.getWarnType())){
//publicChannelWarnService.warnAccessJob(ChannelWarn);
// }
}
}
第二步,定时任务配置类编写
/**
* 任务调度处理
* @author yvan
*
*/
@Slf4j
@Configuration
public class ScheduledChannelWarnJob {
@Resource
private PublicChannelWarnMapper publicChannelWarnMapper;
@Resource
private SchedulerFactoryBean schedulerFactoryBean;
/**
* 每20秒同步数据库数据一次
* 启动所有的定时任务
*/
@Scheduled(cron = "*/20 * * * * ?")
public void scheduleJobs() throws SchedulerException {
List warnList = publicChannelWarnMapper.selectList(new QueryWrapper<>());
if(CollUtil.isNotEmpty(warnList)){
Scheduler scheduler = schedulerFactoryBean.getScheduler();
for(PublicChannelWarn warn:warnList){
scheduleJob(scheduler,warn);
}
}
}
/**
* 配置Job
*/
private void scheduleJob(Scheduler scheduler,PublicChannelWarn warn) throws SchedulerException{
String jobName="jobName"+warn.getId();
String jobGroup="jobGroup"+warn.getId();
String triggerName="triggerName"+warn.getId();
String triggerGroup="triggerGroup"+warn.getId();
JobKey jobKey = new JobKey(jobName, jobGroup);
//获取当前的job 判断是否已经存在
JobDetail crrentjobDetail = scheduler.getJobDetail(jobKey);
//如果当前线程已存在,则刷新时间
if(ObjectUtil.isNotEmpty(crrentjobDetail)){
log.info("=====当前线程已存在====");
TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroup);
CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
String oldTime = cronTrigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(warn.getCorn())) {
log.info("=====刷新定时器时间====");
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(warn.getCorn());
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroup)
.withSchedule(cronScheduleBuilder).build();
}
}else{
log.info("=====创建新的定时任务====");
JobDetail jobDetail = JobBuilder.newJob(ScheduledChannelWarn.class)
//这个位置是你自己需要传的数据,处理业务逻辑,可传多个
.usingJobData("warn", JSONUtil.toJsonStr(warn))
.withIdentity(jobName, jobGroup).build();
// 每5s执行一次
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(warn.getCorn());
CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroup) .withSchedule(scheduleBuilder).build();
scheduler.scheduleJob(jobDetail,cronTrigger);
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)