use of org.quartz.TriggerKey in project camel by apache.
the class QuartzEndpointConfigureTest method testConfigureGroupAndName.
@Test
public void testConfigureGroupAndName() throws Exception {
QuartzEndpoint endpoint = resolveMandatoryEndpoint("quartz2://myGroup/myName?trigger.repeatCount=3&trigger.repeatInterval=1000");
Scheduler scheduler = endpoint.getComponent().getScheduler();
TriggerKey triggerKey = endpoint.getTriggerKey();
Trigger trigger = scheduler.getTrigger(triggerKey);
JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey(triggerKey.getName(), triggerKey.getGroup()));
assertEquals("getName()", "myName", triggerKey.getName());
assertEquals("getGroup()", "myGroup", triggerKey.getGroup());
assertEquals("getJobName", "myName", jobDetail.getKey().getName());
assertEquals("getJobGroup", "myGroup", jobDetail.getKey().getGroup());
SimpleTrigger simpleTrigger = assertIsInstanceOf(SimpleTrigger.class, trigger);
assertEquals("getRepeatCount()", 3, simpleTrigger.getRepeatCount());
}
use of org.quartz.TriggerKey in project camel by apache.
the class QuartzEndpointConfigureTest method testConfigureCronExpression.
@Test
public void testConfigureCronExpression() throws Exception {
QuartzEndpoint endpoint = resolveMandatoryEndpoint("quartz2://myGroup/myTimerName?cron=0+0/5+12-18+?+*+MON-FRI");
Scheduler scheduler = endpoint.getComponent().getScheduler();
TriggerKey triggerKey = endpoint.getTriggerKey();
Trigger trigger = scheduler.getTrigger(triggerKey);
JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey(triggerKey.getName(), triggerKey.getGroup()));
assertEquals("getName()", "myTimerName", triggerKey.getName());
assertEquals("getGroup()", "myGroup", triggerKey.getGroup());
assertEquals("getJobName", "myTimerName", jobDetail.getKey().getName());
assertEquals("getJobGroup", "myGroup", jobDetail.getKey().getGroup());
assertIsInstanceOf(CronTrigger.class, trigger);
CronTrigger cronTrigger = (CronTrigger) trigger;
assertEquals("cron expression", "0 0/5 12-18 ? * MON-FRI", cronTrigger.getCronExpression());
}
use of org.quartz.TriggerKey in project cdap by caskdata.
the class TimeScheduler method scheduleJob.
private void scheduleJob(ProgramId program, SchedulableProgramType programType, String scheduleName, String cronEntry, JobDetail job, Map<String, String> properties) throws SchedulerException {
try {
TriggerKey triggerKey = getGroupedTriggerKey(program, programType, scheduleName);
LOG.debug("Scheduling job {} with cron {}", scheduleName, cronEntry);
TriggerBuilder trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey.getName(), PAUSED_NEW_TRIGGERS_GROUP).forJob(job).withSchedule(CronScheduleBuilder.cronSchedule(Schedulers.getQuartzCronExpression(cronEntry)).withMisfireHandlingInstructionDoNothing());
addProperties(trigger, properties);
scheduler.scheduleJob(trigger.build());
} catch (org.quartz.SchedulerException e) {
throw new SchedulerException(e);
}
}
use of org.quartz.TriggerKey in project cdap by caskdata.
the class TimeScheduler method resumeSchedule.
@Override
public synchronized void resumeSchedule(ProgramId program, SchedulableProgramType programType, String scheduleName) throws NotFoundException, SchedulerException {
checkInitialized();
try {
TriggerKey triggerKey = getGroupedTriggerKey(program, programType, scheduleName);
if (triggerKey.getGroup().equals(PAUSED_NEW_TRIGGERS_GROUP)) {
Trigger neverScheduledTrigger = scheduler.getTrigger(triggerKey);
TriggerBuilder<? extends Trigger> triggerBuilder = neverScheduledTrigger.getTriggerBuilder();
// move this key from TimeScheduler#PAUSED_NEW_TRIGGERS_GROUP to the Key#DEFAULT_GROUP group
// (when no group name is provided default is used)
Trigger resumedTrigger = triggerBuilder.withIdentity(triggerKey.getName()).build();
scheduler.rescheduleJob(neverScheduledTrigger.getKey(), resumedTrigger);
triggerKey = resumedTrigger.getKey();
}
scheduler.resumeTrigger(triggerKey);
} catch (org.quartz.SchedulerException e) {
throw new SchedulerException(e);
}
}
use of org.quartz.TriggerKey in project midpoint by Evolveum.
the class TaskSynchronizer method synchronizeTask.
/**
* Task should be refreshed when entering this method.
*
* @return true if task info in Quartz was updated
*/
public boolean synchronizeTask(TaskQuartzImpl task, OperationResult parentResult) {
if (!task.isPersistent()) {
// transient tasks are not scheduled via Quartz!
return false;
}
boolean changed = false;
String message = "";
OperationResult result = parentResult.createSubresult(TaskSynchronizer.class.getName() + ".synchronizeTask");
result.addArbitraryObjectAsParam("task", task);
try {
LOGGER.trace("Synchronizing task {}; isRecreateQuartzTrigger = {}", task, task.isRecreateQuartzTrigger());
Scheduler scheduler = taskManager.getExecutionManager().getQuartzScheduler();
String oid = task.getOid();
JobKey jobKey = TaskQuartzImplUtil.createJobKeyForTask(task);
TriggerKey standardTriggerKey = TaskQuartzImplUtil.createTriggerKeyForTask(task);
boolean waitingOrClosedOrSuspended = task.getExecutionStatus() == TaskExecutionStatus.WAITING || task.getExecutionStatus() == TaskExecutionStatus.CLOSED || task.getExecutionStatus() == TaskExecutionStatus.SUSPENDED;
if (!scheduler.checkExists(jobKey) && !waitingOrClosedOrSuspended) {
String m1 = "Quartz job does not exist for a task, adding it. Task = " + task;
message += "[" + m1 + "] ";
LOGGER.trace(" - " + m1);
scheduler.addJob(TaskQuartzImplUtil.createJobDetailForTask(task), false);
changed = true;
}
// WAITING and CLOSED tasks should have no triggers; SUSPENDED tasks should have no extra triggers
List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
boolean standardTriggerExists = triggers.stream().anyMatch(t -> t.getKey().equals(standardTriggerKey));
if (waitingOrClosedOrSuspended) {
for (Trigger trigger : triggers) {
if (task.getExecutionStatus() != TaskExecutionStatus.SUSPENDED || !trigger.getKey().equals(standardTriggerKey)) {
String m1 = "Removing Quartz trigger " + trigger.getKey() + " for WAITING/CLOSED/SUSPENDED task " + task;
message += "[" + m1 + "] ";
LOGGER.trace(" - " + m1);
scheduler.unscheduleJob(trigger.getKey());
changed = true;
} else {
// For SUSPENDED tasks, we keep the standard trigger untouched. We want to preserve original
// scheduled time. (This might or might not be what the user wants ... but it has been so for so
// many years, so let's not change it now.)
//
// It's harmless to keep the standard trigger, because:
// 1) If a trigger is mistakenly alive, JobExecutor will take care of it.
// 2) If a trigger has wrong parameters, this will be corrected on task resume.
}
}
} else if (task.getExecutionStatus() == TaskExecutionStatus.RUNNABLE) {
Trigger triggerToBe;
try {
triggerToBe = TaskQuartzImplUtil.createTriggerForTask(task);
} catch (ParseException e) {
String message2 = "Cannot create a trigger for a task " + this + " because of a cron expression parsing exception";
LoggingUtils.logUnexpectedException(LOGGER, message2, e);
result.recordFatalError(message2, e);
// TODO: implement error handling correctly
throw new SystemException("Cannot a trigger for a task because of a cron expression parsing exception", e);
}
if (triggerToBe == null) {
if (standardTriggerExists) {
// TODO what about non-standard triggers?
// These may be legal here (e.g. for a manually-run recurring task waiting to get a chance to run)
String m1 = "Removing standard Quartz trigger for RUNNABLE task that should not have it; task = " + task;
message += "[" + m1 + "] ";
LOGGER.trace(" - " + m1);
scheduler.unscheduleJob(TriggerKey.triggerKey(oid));
changed = true;
}
} else {
// if the trigger should exist and it does not...
if (!standardTriggerExists) {
String m1 = "Creating standard trigger for a RUNNABLE task " + task;
LOGGER.trace(" - " + m1);
message += "[" + m1 + "] ";
scheduler.scheduleJob(triggerToBe);
changed = true;
} else {
// we have to compare trigger parameters with the task's ones
Trigger triggerAsIs = scheduler.getTrigger(standardTriggerKey);
if (task.isRecreateQuartzTrigger() || TaskQuartzImplUtil.triggerDataMapsDiffer(triggerAsIs, triggerToBe)) {
String m1 = "Existing trigger has incompatible parameters or was explicitly requested to be recreated; recreating it. Task = " + task;
LOGGER.trace(" - " + m1);
message += "[" + m1 + "] ";
scheduler.rescheduleJob(standardTriggerKey, triggerToBe);
changed = true;
} else {
String m1 = "Existing trigger is OK, leaving it as is; task = " + task;
LOGGER.trace(" - " + m1);
message += "[" + m1 + "] ";
Trigger.TriggerState state = scheduler.getTriggerState(standardTriggerKey);
if (state == Trigger.TriggerState.PAUSED) {
String m2 = "However, the trigger is paused, resuming it; task = " + task;
LOGGER.trace(" - " + m2);
message += "[" + m2 + "] ";
scheduler.resumeTrigger(standardTriggerKey);
changed = true;
}
}
}
}
}
} catch (Exception e) {
// todo make this more specific (originally here was SchedulerException but e.g. for negative repeat intervals here we get unchecked IllegalArgumentException...)
String message2 = "Cannot synchronize repository/Quartz Job Store information for task " + task;
LoggingUtils.logUnexpectedException(LOGGER, message2, e);
result.recordFatalError(message2, e);
}
if (result.isUnknown()) {
result.computeStatus();
result.recordStatus(result.getStatus(), message);
}
return changed;
}
Aggregations