示例#1
0
  /**
   * Waits for job to finish. JSR 352 does not provide a mechanism to launch jobs in the synchronous
   * mode. As a result, the polling needs to occur
   *
   * @param jobOperator
   * @param executionId
   * @param pollingWait
   * @param maxTries
   * @return
   */
  public static JobExecution waitForJobToEnd(
      JobOperator jobOperator, final long executionId, final long pollingWait, final int maxTries) {

    int countTries = 0;
    JobExecution jobExecution = null;
    do {
      try {
        countTries++;
        Thread.sleep(pollingWait);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }
      jobExecution = jobOperator.getJobExecution(executionId);
    } while (INCOMPLETE_STATUSES.contains(jobExecution.getBatchStatus())
        || (countTries < maxTries));

    return jobExecution;
  }
  @Test
  public void test() throws Exception {
    JobExecution execution = runJob("threadLocalClassloaderBeanPostProcessorTestsJob", null, 10000);

    assertEquals(BatchStatus.COMPLETED, execution.getBatchStatus());
  }
  @Override
  public boolean select(
      final JobExecution jobExecution, final Collection<Long> allJobExecutionIds) {
    if (excludeRunningJobExecutions) {
      final BatchStatus batchStatus = jobExecution.getBatchStatus();
      if (batchStatus != BatchStatus.COMPLETED
          && batchStatus != BatchStatus.FAILED
          && batchStatus != BatchStatus.STOPPED
          && batchStatus != BatchStatus.ABANDONED) {
        return false;
      }
    }

    final long id = jobExecution.getExecutionId();
    if (jobExecutionIds != null && !jobExecutionIds.isEmpty()) {
      return jobExecutionIds.contains(id);
    }

    if (numberOfRecentJobExecutionsToExclude != null) {
      int numOfLargerIds = 0;
      for (final Long jeid : allJobExecutionIds) {
        if (jeid > id) {
          numOfLargerIds++;
          if (numOfLargerIds >= numberOfRecentJobExecutionsToExclude) {
            return true;
          }
        }
      }
      if (numOfLargerIds < numberOfRecentJobExecutionsToExclude) {
        return false;
      }
    }

    if (jobExecutionIdFrom != null) {
      if (jobExecutionIdTo != null) {
        return id >= jobExecutionIdFrom && id <= jobExecutionIdTo;
      } else {
        return id >= jobExecutionIdFrom;
      }
    } else if (jobExecutionIdTo != null) {
      return id <= jobExecutionIdTo;
    }

    final Date endTime = jobExecution.getEndTime();
    // End time may be null if there are unfinished batch jobs
    if (endTime != null) {
      if (withinPastMinutes != null) {
        final long diffMillis = System.currentTimeMillis() - endTime.getTime();
        final long diffMinutes = TimeUnit.MILLISECONDS.toMinutes(diffMillis);
        return diffMinutes <= withinPastMinutes;
      }

      if (jobExecutionEndTimeFrom != null) {
        if (jobExecutionEndTimeTo != null) {
          return (endTime.after(jobExecutionEndTimeFrom) || endTime.equals(jobExecutionEndTimeFrom))
              && (endTime.before(jobExecutionEndTimeTo) || endTime.equals(jobExecutionEndTimeTo));
        } else {
          return endTime.after(jobExecutionEndTimeFrom) || endTime.equals(jobExecutionEndTimeFrom);
        }
      } else if (jobExecutionEndTimeTo != null) {
        return endTime.before(jobExecutionEndTimeTo) || endTime.equals(jobExecutionEndTimeTo);
      }
    }

    if (batchStatuses != null) {
      return batchStatuses.contains(jobExecution.getBatchStatus().name());
    }

    if (exitStatuses != null) {
      return exitStatuses.contains(jobExecution.getExitStatus());
    }

    if (jobExecutionsByJobNames != null) {
      return jobExecutionsByJobNames.contains(jobExecution.getJobName());
    }

    return false;
  }