Search in sources :

Example 51 with CronTrigger

use of org.quartz.CronTrigger 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)

Example 52 with CronTrigger

use of org.quartz.CronTrigger in project openolat by klemens.

the class ReminderModule method configureQuartzJob.

/**
 * Default 0 0 9/1 * * ?
 */
private void configureQuartzJob() {
    try {
        Trigger trigger = scheduler.getTrigger(reminderTriggerKey);
        if (trigger instanceof CronTrigger) {
            CronTrigger cronTrigger = (CronTrigger) trigger;
            String currentCronExpression = cronTrigger.getCronExpression();
            String cronExpression = getCronExpression();
            if (!cronExpression.equals(currentCronExpression)) {
                log.info("Start reminder with this cron expression: " + cronExpression);
                Trigger newTrigger = cronTrigger.getTriggerBuilder().withSchedule(cronSchedule(cronExpression)).build();
                scheduler.rescheduleJob(reminderTriggerKey, newTrigger);
            }
        }
    } catch (Exception e) {
        log.error("", e);
    }
}
Also used : CronTrigger(org.quartz.CronTrigger) Trigger(org.quartz.Trigger) CronTrigger(org.quartz.CronTrigger)

Example 53 with CronTrigger

use of org.quartz.CronTrigger in project kylo by Teradata.

the class QuartzScheduler method buildTriggerInfo.

private TriggerInfo buildTriggerInfo(JobIdentifier jobIdentifier, Trigger trigger) {
    TriggerInfo triggerInfo = new DefaultTriggerInfo(jobIdentifier, triggerIdentifierForTriggerKey(trigger.getKey()));
    triggerInfo.setDescription(trigger.getDescription());
    triggerInfo.setTriggerClass(trigger.getClass());
    String cronExpression = null;
    triggerInfo.setCronExpressionSummary("");
    if (trigger instanceof CronTrigger) {
        CronTrigger ct = (CronTrigger) trigger;
        cronExpression = ct.getCronExpression();
        triggerInfo.setCronExpressionSummary(ct.getExpressionSummary());
    }
    boolean isSimpleTrigger = (!CronTrigger.class.isAssignableFrom(trigger.getClass()) && SimpleTrigger.class.isAssignableFrom(trigger.getClass()));
    triggerInfo.setSimpleTrigger(isSimpleTrigger);
    boolean isScheduled = CronTrigger.class.isAssignableFrom(triggerInfo.getTriggerClass());
    triggerInfo.setScheduled(isScheduled);
    triggerInfo.setCronExpression(cronExpression);
    triggerInfo.setNextFireTime(trigger.getNextFireTime());
    triggerInfo.setStartTime(trigger.getStartTime());
    triggerInfo.setEndTime(trigger.getEndTime());
    // triggerInfo.setFinalFireTime(trigger.getFinalFireTime());
    triggerInfo.setPreviousFireTime(trigger.getPreviousFireTime());
    return triggerInfo;
}
Also used : DefaultTriggerInfo(com.thinkbiganalytics.scheduler.model.DefaultTriggerInfo) CronTrigger(org.quartz.CronTrigger) DefaultTriggerInfo(com.thinkbiganalytics.scheduler.model.DefaultTriggerInfo)

Example 54 with CronTrigger

use of org.quartz.CronTrigger in project pentaho-platform by pentaho.

the class QuartzSchedulerIT method getJobTest.

@Test
public void getJobTest() throws Exception {
    final CronTrigger cronTrigger = mock(CronTrigger.class);
    when(cronTrigger.getCronExpression()).thenReturn(CRON_EXPRESSION);
    when(quartzScheduler.getTriggersOfJob(eq(JOB_ID), eq(USER_NAME))).thenReturn(new Trigger[] { cronTrigger });
    setJobDataMap(USER_NAME);
    final Job job = scheduler.getJob(JOB_ID);
    assertEquals(JOB_ID, job.getJobId());
    assertEquals(jobDetails, job.getJobParams());
    assertEquals(USER_NAME, job.getUserName());
    assertEquals(JOB_NAME, job.getJobName());
    assertEquals(Job.JobState.NORMAL, job.getState());
}
Also used : CronTrigger(org.quartz.CronTrigger) Job(org.pentaho.platform.api.scheduler2.Job) Test(org.junit.Test)

Example 55 with CronTrigger

use of org.quartz.CronTrigger in project pentaho-platform by pentaho.

the class QuartzSchedulerIT method triggerNowTest.

@Test
public void triggerNowTest() throws Exception {
    final SimpleTrigger simpleTrigger = mock(SimpleTrigger.class);
    final CronTrigger cronTrigger = mock(CronTrigger.class);
    when(quartzScheduler.getTriggersOfJob(eq(JOB_ID), eq(USER_NAME))).thenReturn(new Trigger[] { simpleTrigger, cronTrigger });
    scheduler.triggerNow(JOB_ID);
    verify(simpleTrigger, times(1)).setPreviousFireTime(any(Date.class));
    verify(cronTrigger, times(1)).setPreviousFireTime(any(Date.class));
    verify(quartzScheduler, times(1)).rescheduleJob(eq(JOB_ID), eq(USER_NAME), eq(simpleTrigger));
    verify(quartzScheduler, times(1)).rescheduleJob(eq(JOB_ID), eq(USER_NAME), eq(cronTrigger));
    verify(quartzScheduler, times(1)).triggerJob(eq(JOB_ID), eq(USER_NAME));
}
Also used : CronTrigger(org.quartz.CronTrigger) SimpleTrigger(org.quartz.SimpleTrigger) Date(java.util.Date) Test(org.junit.Test)

Aggregations

CronTrigger (org.quartz.CronTrigger)103 JobDetail (org.quartz.JobDetail)42 Trigger (org.quartz.Trigger)36 SimpleTrigger (org.quartz.SimpleTrigger)29 SchedulerException (org.quartz.SchedulerException)28 Test (org.junit.Test)24 Scheduler (org.quartz.Scheduler)19 TriggerKey (org.quartz.TriggerKey)19 Date (java.util.Date)14 JobKey (org.quartz.JobKey)12 JobDataMap (org.quartz.JobDataMap)11 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)8 CronScheduleBuilder (org.quartz.CronScheduleBuilder)8 ParseException (java.text.ParseException)7 HashMap (java.util.HashMap)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 JobExecutionException (org.quartz.JobExecutionException)6 ComplexJobTrigger (org.pentaho.platform.api.scheduler2.ComplexJobTrigger)5 SimpleJobTrigger (org.pentaho.platform.api.scheduler2.SimpleJobTrigger)5