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);
}
}
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);
}
}
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;
}
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());
}
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));
}
Aggregations