Example #1
0
  @SuppressWarnings("unchecked")
  public void jobToBeExecuted(JobExecutionContext context) {
    ALNTLogger.info(this.getClass().getName(), "jobToBeExecuted()", "Begin");
    try {
      ISchedulerConfigService schedulerConfigService =
          (ISchedulerConfigService) ServiceLocator.findService("schedulerConfigService");

      JobDetail jobDetails = context.getJobDetail();
      Long id = (Long) jobDetails.getJobDataMap().get("JOB_ID");
      String jobPgmName = (String) jobDetails.getJobDataMap().get("JOB_NAME");

      String triggerGroup = context.getTrigger().getGroup();
      boolean recoveringJob = false;
      JobStatus jobStatus = null;
      if (!StringUtils.isEmpty(triggerGroup) && triggerGroup.equals("RECOVERING_JOBS")) {
        recoveringJob = true;
        jobDetails.getJobDataMap().put(SchedulerUtil.RECOVERING_JOB, Boolean.TRUE);

        List<JobStatus> jobStatusList = null;
        try {
          jobStatusList =
              schedulerConfigService.loadJobStatus(
                  jobDetails.getJobDataMap().getLongValue("JOB_ID"));
          if (null != jobStatusList && jobStatusList.size() > 0) {
            jobStatus = jobStatusList.get(jobStatusList.size() - 1);
            jobDetails
                .getJobDataMap()
                .put(SchedulerUtil.RECOVERING_JOB_STATUS_ID, jobStatus.getId());
            jobStatus.setJobStatus(CommonConstants.JOB_STATUS_RUNNING);
          }
        } catch (Exception ex) {
          ALNTLogger.error(this.getClass().getName(), "jobToBeExecuted", ex);
        }
      }

      if (jobStatus == null) {
        jobStatus = new JobStatus();
        jobStatus.setJobId(id);
        Date nextFireTime = context.getNextFireTime();
        jobStatus.setNextFireTime(nextFireTime);
        jobStatus.setJobName(jobPgmName);
        jobStatus.setCreationDate(new Date());
        jobStatus.setJobStatus(CommonConstants.JOB_STATUS_RUNNING);
      }

      Job job = context.getJobInstance();
      if (job instanceof IJobsWithResult) {
        IJobsWithResult jobWithResult = (IJobsWithResult) job;
        JobResultWriter writer = new JobResultWriter();
        writer.setCreateDate(jobStatus.getCreationDate());
        writer.setJobId(id);
        writer.init();
        jobWithResult.setResultWriter(writer);
      }
      if (jobDetails.getJobDataMap().containsKey(SchedulerUtil.RETRYING_JOB)
          && jobDetails.getJobDataMap().containsKey(SchedulerUtil.FAILED_JOB_STATUS_ID)
          && jobDetails.getJobDataMap().containsKey(SchedulerUtil.RETRYING_JOB_STATUS_ID)) {
        if (jobDetails.getJobDataMap().getBoolean(SchedulerUtil.RETRYING_JOB)) {
          jobStatus.setOriginalFailedId(
              jobDetails.getJobDataMap().getLong(SchedulerUtil.FAILED_JOB_STATUS_ID));
          jobStatus.setRetryForId(
              jobDetails.getJobDataMap().getLong(SchedulerUtil.RETRYING_JOB_STATUS_ID));
        }
      }

      jobStatus = schedulerConfigService.saveJobStatus(jobStatus);

      jobDetails.getJobDataMap().put("JobStatus_ID", jobStatus.getId());
      if (recoveringJob) {

      } else if (jobStatus.getRetryForId() != null) {
        JobStatus retryingForJobStatus =
            schedulerConfigService.loadJobStatusByStatusId(jobStatus.getRetryForId());
        if (retryingForJobStatus != null) {
          // get the job status object that simply says retrying and append it with new job status
          // id
          retryingForJobStatus.setJobStatus(
              CommonConstants.JOB_STATUS_RETRIED + " - " + jobStatus.getId());
          schedulerConfigService.saveJobStatus(retryingForJobStatus);
        }
      }

      ALNTLogger.info(this.getClass().getName(), "jobToBeExecuted()", "End");
    } catch (ALNTSystemException e) {
      ALNTLogger.error(this.getClass().getName(), "jobToBeExecuted()", "Error ::" + e.getMessage());
    } catch (ALNTApplicationException e) {
      ALNTLogger.error(this.getClass().getName(), "jobToBeExecuted", "Error ::" + e.getMessage());
    }
  }
Example #2
0
  public void jobWasExecuted(JobExecutionContext context, JobExecutionException arg1) {
    SchedulerMailUtil mailUtil = new SchedulerMailUtil();
    ALNTLogger.info(this.getClass().getName(), "jobWasExecuted()", "Begin");
    try {
      ISchedulerConfigService schedulerConfigService =
          (ISchedulerConfigService) ServiceLocator.findService("schedulerConfigService");
      Job job = context.getJobInstance();
      if (job instanceof IJobsWithResult) {
        IJobsWithResult jobWithResult = (IJobsWithResult) job;
        JobResultWriter writer = jobWithResult.getResultWriter();
        if (writer != null) {
          writer.getLogWriter().flush();
          writer.getLogWriter().close();
          writer.getResultWriter().flush();
          writer.getResultWriter().close();
        }
      }
      Object jobId = context.getJobDetail().getJobDataMap().get("JOB_ID");
      Object id = context.getJobDetail().getJobDataMap().get("JobStatus_ID");
      String jobPgmName = (String) context.getJobDetail().getJobDataMap().get("JOB_NAME");
      ScheduledJobs scheduledJobs =
          schedulerConfigService.loadScheduledJob(new Long(jobId.toString()));
      ALNTLogger.info(getName(), "Got NotificationDetail object from ScheduledJobs");

      // Getting notification object to send the notification email to user
      scheduledNotificationDetail = scheduledJobs.getScheduledNotificationDetail();

      JobStatus jobStatus =
          schedulerConfigService.loadJobStatus(new Long(jobId.toString()), new Long(id.toString()));
      if (CommonConstants.SCHEDULE_IMMEDIATE.equalsIgnoreCase(scheduledJobs.getInterval())) {
        scheduledJobs.setActiveStatus(false);
      } else if (CommonConstants.SCHEDULE_ONCE.equalsIgnoreCase(scheduledJobs.getInterval())) {
        scheduledJobs.setActiveStatus(false);
      } else if (scheduledJobs.getInterval().startsWith(CommonConstants.SCHEDULE_PERIODICALLY)) {
        if (null == context.getNextFireTime()) {
          scheduledJobs.setActiveStatus(false);
        }
      }

      if (CommonConstants.JOB_STATUS_CANCELING.equalsIgnoreCase(jobStatus.getJobStatus())) {
        scheduledJobs.setActiveStatus(false);
        jobStatus.setJobStatus(CommonConstants.JOB_STATUS_CANCELED);

      } else {
        jobStatus.setJobStatus(CommonConstants.JOB_STATUS_COMPLETED);
        if (arg1 != null) {
          jobStatus.setJobStatus(CommonConstants.JOB_STATUS_FAILED);
        } else if (context
            .getJobDetail()
            .getJobDataMap()
            .containsKey(CommonConstants.JOB_STATUS_SKIPPED)) {
          jobStatus.setJobStatus(CommonConstants.JOB_STATUS_SKIPPED);
          context.getJobDetail().getJobDataMap().remove(CommonConstants.JOB_STATUS_SKIPPED);
        }
        if (context.getJobDetail() != null
            && context.getJobDetail().getJobDataMap() != null
            && context
                .getJobDetail()
                .getJobDataMap()
                .containsKey(CommonConstants.NO_VALID_FEED_FILE_EXISTS)) {
          jobStatus.setJobStatus(
              CommonConstants.JOB_STATUS_FAILED + "- " + CommonConstants.NO_VALID_FEED_FILE_EXISTS);
          context.getJobDetail().getJobDataMap().remove(CommonConstants.NO_VALID_FEED_FILE_EXISTS);
        }
        // completionTime=context.getFireTime();

        JobStage jobStage =
            schedulerConfigService.loadJobStage(
                jobStatus.getOriginalFailedId() == null
                    ? jobStatus.getId()
                    : jobStatus.getOriginalFailedId());
        if (jobStage != null) {
          jobStatus.setLastKnowStage(jobStage.getJobStage());
        }
      }

      // Sending notification email to user with status of current job with defined parameters or
      // with standard parameters
      // If the notification has not been configured for current running job then
      // scheduledNotificationDetail object will be null
      // so we are checking for that the notification has been configured for this or not
      if (scheduledNotificationDetail != null) {
        if (job instanceof IJobWithExtendedMailSubstVar) {
          Map<String, String> mapParams =
              ((IJobWithExtendedMailSubstVar) job).getMailSubstVarValues();
          mapParams.putAll(this.getStandardJobParams(scheduledJobs));
          mailUtil.sendMail(scheduledNotificationDetail, jobStatus.getJobStatus(), mapParams);
        } else {
          mailUtil.sendMail(
              scheduledNotificationDetail,
              jobStatus.getJobStatus(),
              this.getStandardJobParams(scheduledJobs));
        }
      }

      jobStatus.setCompleteTime(new Date());
      jobStatus.setJobName(jobPgmName);

      schedulerConfigService.saveScheduledJobWithoutScheduleJob(scheduledJobs);
      schedulerConfigService.saveJobStatus(jobStatus);

      ALNTLogger.info(this.getClass().getName(), "jobWasExecuted()", "End");
    } catch (ALNTSystemException e) {
      ALNTLogger.error(this.getClass().getName(), "jobWasExecuted()", "Error ::" + e.getMessage());
    } catch (ALNTApplicationException e) {
      ALNTLogger.error(this.getClass().getName(), "jobWasExecuted()", ":Error ::" + e.getMessage());
    }
  }