Ejemplo n.º 1
0
  public void executeJob(final ScheduledJobDetail scheduledJobDetail, String triggerType) {
    try {
      final JobDataMap jobDataMap = new JobDataMap();
      if (triggerType == null) {
        triggerType = SchedulerServiceConstants.TRIGGER_TYPE_APPLICATION;
      }
      jobDataMap.put(SchedulerServiceConstants.TRIGGER_TYPE_REFERENCE, triggerType);
      jobDataMap.put(
          SchedulerServiceConstants.TENANT_IDENTIFIER,
          ThreadLocalContextUtil.getTenant().getTenantIdentifier());
      final String key = scheduledJobDetail.getJobKey();
      final JobKey jobKey = constructJobKey(key);
      final String schedulerName = getSchedulerName(scheduledJobDetail);
      final Scheduler scheduler = this.schedulers.get(schedulerName);
      if (scheduler == null || !scheduler.checkExists(jobKey)) {
        final JobDetail jobDetail = createJobDetail(scheduledJobDetail);
        final String tempSchedulerName = "temp" + scheduledJobDetail.getId();
        final Scheduler tempScheduler =
            createScheduler(tempSchedulerName, 1, schedulerJobListener, schedulerStopListener);
        tempScheduler.addJob(jobDetail, true);
        jobDataMap.put(SchedulerServiceConstants.SCHEDULER_NAME, tempSchedulerName);
        this.schedulers.put(tempSchedulerName, tempScheduler);
        tempScheduler.triggerJob(jobDetail.getKey(), jobDataMap);
      } else {
        scheduler.triggerJob(jobKey, jobDataMap);
      }

    } catch (final Exception e) {
      final String msg = "Job execution failed for job with id:" + scheduledJobDetail.getId();
      logger.error(msg, e);
      throw new PlatformInternalServerException(
          "error.msg.sheduler.job.execution.failed", msg, scheduledJobDetail.getId());
    }
  }
Ejemplo n.º 2
0
 public void rescheduleJob(final ScheduledJobDetail scheduledJobDetail) {
   try {
     final String jobIdentity = scheduledJobDetail.getJobKey();
     final JobKey jobKey = constructJobKey(jobIdentity);
     final String schedulername = getSchedulerName(scheduledJobDetail);
     final Scheduler scheduler = this.schedulers.get(schedulername);
     if (scheduler != null) {
       scheduler.deleteJob(jobKey);
     }
     scheduleJob(scheduledJobDetail);
     this.schedularWritePlatformService.saveOrUpdate(scheduledJobDetail);
   } catch (final Throwable throwable) {
     final String stackTrace = getStackTraceAsString(throwable);
     scheduledJobDetail.updateErrorLog(stackTrace);
     this.schedularWritePlatformService.saveOrUpdate(scheduledJobDetail);
   }
 }
Ejemplo n.º 3
0
 @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);
         }
       }
     }
   }
 }