Search in sources :

Example 61 with SchedulerException

use of org.quartz.SchedulerException in project ddf by codice.

the class WorkspaceQueryServiceImpl method setCronString.

/**
     * @param cronString cron string (must be non-null)
     */
@SuppressWarnings("unused")
public void setCronString(String cronString) {
    notNull(cronString, "cronString must be non-null");
    notNull(scheduler, "scheduler must be non-null");
    notNull(jobDetail, "jobDetail must be non-null");
    try {
        scheduler.deleteJob(jobDetail.getKey());
        LOGGER.debug("Scheduling job {}", jobDetail);
        CronTrigger trigger = newTrigger().withIdentity(TRIGGER_NAME).startNow().withSchedule(cronSchedule(cronString)).build();
        scheduler.scheduleJob(jobDetail, trigger);
        LOGGER.debug("Setting cron string : {}", cronString);
    } catch (SchedulerException e) {
        LOGGER.warn("Unable to update scheduler with cron string: cron=[{}]", cronString, e);
    }
}
Also used : CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException)

Example 62 with SchedulerException

use of org.quartz.SchedulerException in project ddf by codice.

the class ScheduledCommandTaskTest method testNewTaskScheduleException.

@Test
public void testNewTaskScheduleException() throws Exception {
    when(scheduler.scheduleJob(any(JobDetail.class), any(Trigger.class))).thenThrow(new SchedulerException());
    scheduledCommandTask.newTask();
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException) Test(org.junit.Test)

Example 63 with SchedulerException

use of org.quartz.SchedulerException in project ddf by codice.

the class ScheduledCommandTask method newTask.

@Override
public void newTask() {
    LOGGER.trace("Creating new Task.");
    long identifier = System.currentTimeMillis();
    this.jobKey = new JobKey("job" + identifier, jobClass.getSimpleName());
    this.triggerKey = new TriggerKey("trigger" + identifier, jobClass.getSimpleName());
    JobDetail jobDetail = createJob();
    Trigger trigger = createTrigger();
    if (trigger == null) {
        return;
    }
    try {
        scheduler.scheduleJob(jobDetail, trigger);
    } catch (SchedulerException e) {
        LOGGER.info("Error with scheduling of task.", e);
    }
}
Also used : TriggerKey(org.quartz.TriggerKey) JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException)

Example 64 with SchedulerException

use of org.quartz.SchedulerException in project midpoint by Evolveum.

the class TaskManagerQuartzImpl method onTaskDelete.

@Override
public void onTaskDelete(String oid, OperationResult parentResult) {
    OperationResult result = parentResult.createSubresult(DOT_INTERFACE + "onTaskDelete");
    result.addParam("oid", oid);
    LOGGER.trace("onTaskDelete called for oid = " + oid);
    JobKey jobKey = TaskQuartzImplUtil.createJobKeyForTaskOid(oid);
    try {
        if (executionManager.getQuartzScheduler().checkExists(jobKey)) {
            // removes triggers as well
            executionManager.getQuartzScheduler().deleteJob(jobKey);
        }
    } catch (SchedulerException e) {
        String message = "Quartz shadow job cannot be removed; oid = " + oid;
        LoggingUtils.logUnexpectedException(LOGGER, message, e);
        result.recordFatalError(message);
    }
    result.recordSuccessIfUnknown();
}
Also used : JobKey(org.quartz.JobKey) SchedulerException(org.quartz.SchedulerException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString)

Example 65 with SchedulerException

use of org.quartz.SchedulerException 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;
}
Also used : TriggerKey(org.quartz.TriggerKey) JobKey(org.quartz.JobKey) Trigger(org.quartz.Trigger) SystemException(com.evolveum.midpoint.util.exception.SystemException) Scheduler(org.quartz.Scheduler) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ParseException(java.text.ParseException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SchedulerException(org.quartz.SchedulerException) ParseException(java.text.ParseException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) SystemException(com.evolveum.midpoint.util.exception.SystemException)

Aggregations

SchedulerException (org.quartz.SchedulerException)133 JobDetail (org.quartz.JobDetail)59 Trigger (org.quartz.Trigger)42 Scheduler (org.quartz.Scheduler)37 JobKey (org.quartz.JobKey)33 SimpleTrigger (org.quartz.SimpleTrigger)19 JobDataMap (org.quartz.JobDataMap)18 CronTrigger (org.quartz.CronTrigger)17 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)15 ApplicationContext (org.springframework.context.ApplicationContext)15 ArrayList (java.util.ArrayList)12 SchedulerContext (org.quartz.SchedulerContext)12 IOException (java.io.IOException)11 TriggerKey (org.quartz.TriggerKey)10 Date (java.util.Date)9 JobExecutionException (org.quartz.JobExecutionException)9 StdSchedulerFactory (org.quartz.impl.StdSchedulerFactory)6 ParseException (java.text.ParseException)5 Command (org.openhab.core.types.Command)5 JobSystemException (com.dangdang.ddframe.job.exception.JobSystemException)4