use of org.quartz.Trigger 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;
}
use of org.quartz.Trigger in project opennms by OpenNMS.
the class ImportSchedulerIT method createJobAndVerifyImportJobFactoryIsRegistered.
@Test
public void createJobAndVerifyImportJobFactoryIsRegistered() throws SchedulerException, InterruptedException {
RequisitionDef def = m_dao.getDefs().get(0);
JobDetail detail = new JobDetailImpl("test", ImportScheduler.JOB_GROUP, ImportJob.class, false, false);
detail.getJobDataMap().put(ImportJob.URL, def.getImportUrlResource().orElse(null));
detail.getJobDataMap().put(ImportJob.RESCAN_EXISTING, def.getRescanExisting());
class MyBoolWrapper {
volatile Boolean m_called = false;
public Boolean getCalled() {
return m_called;
}
public void setCalled(Boolean called) {
m_called = called;
}
}
final MyBoolWrapper callTracker = new MyBoolWrapper();
m_importScheduler.getScheduler().getListenerManager().addTriggerListener(new TriggerListener() {
@Override
public String getName() {
return "TestTriggerListener";
}
@Override
public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode) {
LOG.info("triggerComplete called on trigger listener");
callTracker.setCalled(true);
}
@Override
public void triggerFired(Trigger trigger, JobExecutionContext context) {
LOG.info("triggerFired called on trigger listener");
Job jobInstance = context.getJobInstance();
if (jobInstance instanceof ImportJob) {
Assert.assertNotNull(((ImportJob) jobInstance).getProvisioner());
Assert.assertTrue(context.getJobDetail().getJobDataMap().containsKey(ImportJob.URL));
Assert.assertEquals("dns://localhost/localhost", context.getJobDetail().getJobDataMap().get(ImportJob.URL));
Assert.assertTrue(context.getJobDetail().getJobDataMap().containsKey(ImportJob.RESCAN_EXISTING));
Assert.assertEquals("dbonly", context.getJobDetail().getJobDataMap().get(ImportJob.RESCAN_EXISTING));
}
callTracker.setCalled(true);
}
@Override
public void triggerMisfired(Trigger trigger) {
LOG.info("triggerMisFired called on trigger listener");
callTracker.setCalled(true);
}
@Override
public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
LOG.info("vetoJobExecution called on trigger listener");
callTracker.setCalled(true);
return false;
}
});
Calendar testCal = Calendar.getInstance();
testCal.add(Calendar.SECOND, 5);
SimpleTriggerImpl trigger = new SimpleTriggerImpl("test", ImportScheduler.JOB_GROUP, testCal.getTime());
m_importScheduler.getScheduler().scheduleJob(detail, trigger);
m_importScheduler.start();
int callCheck = 0;
while (!callTracker.getCalled() && callCheck++ < 2) {
Thread.sleep(5000);
}
// TODO: need to fix the interrupted exception that occurs in the provisioner
}
use of org.quartz.Trigger in project opennms by OpenNMS.
the class DefaultSchedulerService method getTriggerDescriptions.
/**
* <p>getTriggerDescriptions</p>
*
* @return a {@link java.util.List} object.
*/
@Override
public List<TriggerDescription> getTriggerDescriptions() {
List<TriggerDescription> triggerDescriptions = new ArrayList<>();
try {
Set<TriggerKey> triggerKeys = m_scheduler.getTriggerKeys(GroupMatcher.<TriggerKey>groupEquals(m_triggerGroup));
for (TriggerKey triggerKey : triggerKeys) {
TriggerDescription description = new TriggerDescription();
Trigger trigger = m_scheduler.getTrigger(triggerKey);
description.setNextFireTime(trigger.getNextFireTime());
description.setTriggerName(triggerKey.getName());
description.setReportId((String) trigger.getJobDataMap().get("reportId"));
description.setDeliveryOptions((DeliveryOptions) trigger.getJobDataMap().get("deliveryOptions"));
description.setReportParameters(((ReportParameters) trigger.getJobDataMap().get("criteria")).getReportParms());
triggerDescriptions.add(description);
}
} catch (SchedulerException e) {
LOG.error("exception lretrieving trigger descriptions", e);
}
return triggerDescriptions;
}
use of org.quartz.Trigger in project Dempsy by Dempsy.
the class RelativeOutputSchedule method start.
/**
* Container will invoke this method.
*/
@Override
public void start(final Infrastructure infra) {
try {
final OutputQuartzHelper outputQuartzHelper = new OutputQuartzHelper();
final JobDetail jobDetail = outputQuartzHelper.getJobDetail(outputInvoker);
final Trigger trigger = outputQuartzHelper.getSimpleTrigger(timeUnit, (int) interval);
scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
} catch (final SchedulerException se) {
LOGGER.error("Error occurred while starting the relative scheduler : " + se.getMessage(), se);
}
}
use of org.quartz.Trigger in project Dempsy by Dempsy.
the class CronOutputSchedule method start.
/*
* (non-Javadoc)
*
* @see com.nokia.dempsy.output.OutputExecuter#start()
*/
@Override
public void start(final Infrastructure infra) {
try {
final OutputQuartzHelper outputQuartzHelper = new OutputQuartzHelper();
final JobDetail jobDetail = outputQuartzHelper.getJobDetail(outputInvoker);
final Trigger trigger = outputQuartzHelper.getCronTrigger(cronExpression);
scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
} catch (final SchedulerException se) {
LOGGER.error("Error occurred while starting the cron scheduler : " + se.getMessage(), se);
}
}
Aggregations