private static void executeJob(String jobId) { long execID = 0; // ジョブの開始時はXML読み込みなど同時実行されるとおかしくなる処理があるので、ジョブID単位で同期化しておく。 JobOperator jobOperator = BatchRuntime.getJobOperator(); synchronized (jobId) { Properties props = new Properties(); props.setProperty( "date", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); execID = jobOperator.start(jobId, props); System.out.println("job stated id:" + execID); } JobExecution jobExec = null; // ジョブが終了するまでポーリング while (true) { jobExec = jobOperator.getJobExecution(execID); if (jobExec.getEndTime() != null) { break; } try { Thread.sleep(1000L); } catch (InterruptedException ex) { } } System.out.println("JOB END:Status is " + jobExec.getExitStatus()); }
protected long getOriginalJobExecutionId(final String jobName) { final List<JobInstance> jobInstances = jobOperator.getJobInstances(jobName, 0, 1); final JobInstance jobInstance = jobInstances.get(0); final List<JobExecution> jobExecutions = jobOperator.getJobExecutions(jobInstance); final JobExecution originalJobExecution = jobExecutions.get(jobExecutions.size() - 1); return originalJobExecution.getExecutionId(); }
/** * 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; }