Search in sources :

Example 1 with ScheduleTrigger

use of com.monitor.model.task.ScheduleTrigger in project new-cloud by xie-summer.

the class ScheduleTriggerServiceImpl method refreshTrigger.

/**
 * 定时刷新扫描配置表
 */
@Scheduled(fixedRate = 1000 * 30)
@Override
public void refreshTrigger() {
    try {
        List<ScheduleTrigger> jobList = scheduleTriggerMapper.selectAll();
        if (null != jobList && jobList.size() > 0) {
            for (ScheduleTrigger scheduleJob : jobList) {
                String status = scheduleJob.getStatus();
                TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
                CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
                // 说明本条任务还没有添加到quartz中
                if (null == trigger) {
                    if (STATUS.equals(status)) {
                        continue;
                    }
                    JobDetail jobDetail = null;
                    try {
                        // 创建JobDetail(数据库中job_name存的任务全路径,这里就可以动态的把任务注入到JobDetail中)
                        jobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(scheduleJob.getJobName())).withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).build();
                        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCron());
                        trigger = TriggerBuilder.newTrigger().withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).withSchedule(scheduleBuilder).build();
                        scheduler.scheduleJob(jobDetail, trigger);
                    } catch (ClassNotFoundException e) {
                    }
                } else {
                    // 如果是禁用,从quartz中删除这条任务
                    if (STATUS.equals(status)) {
                        JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
                        scheduler.deleteJob(jobKey);
                        continue;
                    }
                    String searchCron = scheduleJob.getCron();
                    String currentCron = trigger.getCronExpression();
                    if (!searchCron.equals(currentCron)) {
                        // 表达式调度构建器
                        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(searchCron);
                        // 按新的cronExpression表达式重新构建trigger
                        trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
                        // 按新的trigger重新设置job执行
                        scheduler.rescheduleJob(triggerKey, trigger);
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.error("定时任务每日刷新触发器任务异常,在ScheduleTriggerService的方法refreshTrigger中,异常信息:", e);
    }
}
Also used : ScheduleTrigger(com.monitor.model.task.ScheduleTrigger) TriggerKey(org.quartz.TriggerKey) CronTrigger(org.quartz.CronTrigger) JobDetail(org.quartz.JobDetail) CronScheduleBuilder(org.quartz.CronScheduleBuilder) JobKey(org.quartz.JobKey) Job(org.quartz.Job) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Aggregations

ScheduleTrigger (com.monitor.model.task.ScheduleTrigger)1 CronScheduleBuilder (org.quartz.CronScheduleBuilder)1 CronTrigger (org.quartz.CronTrigger)1 Job (org.quartz.Job)1 JobDetail (org.quartz.JobDetail)1 JobKey (org.quartz.JobKey)1 TriggerKey (org.quartz.TriggerKey)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1