private void scheduleJob(final ScheduledJobDetail scheduledJobDetails) { if (!scheduledJobDetails.isActiveSchedular()) { scheduledJobDetails.updateNextRunTime(null); scheduledJobDetails.updateCurrentlyRunningStatus(false); return; } try { final JobDetail jobDetail = createJobDetail(scheduledJobDetails); final Trigger trigger = createTrigger(scheduledJobDetails, jobDetail); final Scheduler scheduler = getScheduler(scheduledJobDetails); scheduler.scheduleJob(jobDetail, trigger); scheduledJobDetails.updateJobKey(getJobKeyAsString(jobDetail.getKey())); scheduledJobDetails.updateNextRunTime(trigger.getNextFireTime()); scheduledJobDetails.updateErrorLog(null); } catch (final Throwable throwable) { scheduledJobDetails.updateNextRunTime(null); final String stackTrace = getStackTraceAsString(throwable); scheduledJobDetails.updateErrorLog(stackTrace); logger.error("Could not schedule job: " + scheduledJobDetails.getJobName(), throwable); } scheduledJobDetails.updateCurrentlyRunningStatus(false); }
@Override public void startScheduler() { final SchedulerDetail schedulerDetail = this.schedularWritePlatformService.retriveSchedulerDetail(); if (schedulerDetail.isSuspended()) { schedulerDetail.updateSuspendedState(false); this.schedularWritePlatformService.updateSchedulerDetail(schedulerDetail); if (schedulerDetail.isExecuteInstructionForMisfiredJobs()) { final List<ScheduledJobDetail> scheduledJobDetails = this.schedularWritePlatformService.retrieveAllJobs(); for (final ScheduledJobDetail jobDetail : scheduledJobDetails) { if (jobDetail.isTriggerMisfired()) { if (jobDetail.isActiveSchedular()) { executeJob(jobDetail, SchedulerServiceConstants.TRIGGER_TYPE_CRON); } final String schedulerName = getSchedulerName(jobDetail); final Scheduler scheduler = this.schedulers.get(schedulerName); if (scheduler != null) { final String key = jobDetail.getJobKey(); final JobKey jobKey = constructJobKey(key); try { final List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey); for (final Trigger trigger : triggers) { if (trigger.getNextFireTime() != null && trigger.getNextFireTime().after(jobDetail.getNextRunTime())) { jobDetail.updateNextRunTime(trigger.getNextFireTime()); } } } catch (final SchedulerException e) { logger.error(e.getMessage(), e); } } jobDetail.updateTriggerMisfired(false); this.schedularWritePlatformService.saveOrUpdate(jobDetail); } } } } }