private JobParameters createJobParametersWithIncrementerIfAvailable(String parameters, Job job) throws JobParametersNotFoundException { JobParameters jobParameters = jobParametersConverter.getJobParameters(PropertiesConverter.stringToProperties(parameters)); // use JobParametersIncrementer to create JobParameters if incrementer is set and only if the job is no restart if (job.getJobParametersIncrementer() != null){ JobExecution lastJobExecution = jobRepository.getLastJobExecution(job.getName(), jobParameters); boolean restart = false; // check if job failed before if (lastJobExecution != null) { BatchStatus status = lastJobExecution.getStatus(); if (status.isUnsuccessful() && status != BatchStatus.ABANDONED) { restart = true; } } // if it's not a restart, create new JobParameters with the incrementer if (!restart) { JobParameters nextParameters = getNextJobParameters(job); Map<String, JobParameter> map = new HashMap<String, JobParameter>(nextParameters.getParameters()); map.putAll(jobParameters.getParameters()); jobParameters = new JobParameters(map); } } return jobParameters; }
/** * Borrowed from CommandLineJobRunner. * @param job the job that we need to find the next parameters for * @return the next job parameters if they can be located * @throws JobParametersNotFoundException if there is a problem */ private JobParameters getNextJobParameters(Job job) throws JobParametersNotFoundException { String jobIdentifier = job.getName(); JobParameters jobParameters; List<JobInstance> lastInstances = jobExplorer.getJobInstances(jobIdentifier, 0, 1); JobParametersIncrementer incrementer = job.getJobParametersIncrementer(); if (lastInstances.isEmpty()) { jobParameters = incrementer.getNext(new JobParameters()); if (jobParameters == null) { throw new JobParametersNotFoundException("No bootstrap parameters found from incrementer for job=" + jobIdentifier); } } else { List<JobExecution> lastExecutions = jobExplorer.getJobExecutions(lastInstances.get(0)); jobParameters = incrementer.getNext(lastExecutions.get(0).getJobParameters()); } return jobParameters; }
/** * 다음 실행 될 Batch Job의 Job Parameter를 생성한다. * * @param job * @return JobParameters * @throws JobParametersNotFoundException */ private JobParameters getNextJobParameters(Job job) throws JobParametersNotFoundException { String jobIdentifier = job.getName(); JobParameters jobParameters; List<JobInstance> lastInstances = jobExplorer.getJobInstances(jobIdentifier, 0, 1); JobParametersIncrementer incrementer = job.getJobParametersIncrementer(); if (incrementer == null) { throw new JobParametersNotFoundException("No job parameters incrementer found for job=" + jobIdentifier); } if (lastInstances.isEmpty()) { jobParameters = incrementer.getNext(new JobParameters()); if (jobParameters == null) { throw new JobParametersNotFoundException("No bootstrap parameters found from incrementer for job=" + jobIdentifier); } } else { jobParameters = incrementer.getNext(lastInstances.get(0).getJobParameters()); } return jobParameters; }
protected void execute(Job job, JobParameters jobParameters) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException, JobParametersNotFoundException { JobParameters nextParameters = getNextJobParameters(job, jobParameters); if (nextParameters != null) { JobExecution execution = this.jobLauncher.run(job, nextParameters); if (this.publisher != null) { this.publisher.publishEvent(new JobExecutionEvent(execution)); } } }
/** * Merges the job's default parameters with the given input parameters. Input parameters override default parameters. * * @param inputParameter * the input parameters, may be <code>null</code>. */ private JobParameters createJobParameters(final String jobName, final JobParameters inputParameter) throws NoSuchJobException, JobParametersNotFoundException { final JobParameters params = getNextJobParameters(jobName); return Optional.ofNullable(inputParameter).map(customParams -> { final JobParametersBuilder builder = new JobParametersBuilder(params); customParams.getParameters().forEach((key, value) -> builder.addParameter(key, value)); return builder.toJobParameters(); }).orElse(params); }
private JobParameters getNextJobParameters(final String jobName) throws NoSuchJobException, JobParametersNotFoundException { final Job job = jobRegistry.getJob(jobName); final JobParametersIncrementer incrementer = Optional.ofNullable(job.getJobParametersIncrementer()).orElseThrow( () -> new JobParametersNotFoundException("No job parameters incrementer found for job " + jobName)); return getNextJobParameters(jobName, incrementer); }
private JobParameters getNextJobParameters(final String jobName, final JobParametersIncrementer incrementer) throws JobParametersNotFoundException { final JobParameters previousParameters = jobExplorer.getJobInstances(jobName, 0, 1).stream().findFirst() .flatMap(instance -> jobExplorer.getJobExecutions(instance).stream().findFirst()).map(JobExecution::getJobParameters) .orElseGet(JobParameters::new); return Optional.ofNullable(incrementer.getNext(previousParameters)).orElseThrow( () -> new JobParametersNotFoundException("No bootstrap parameters found for job " + jobName)); }
@Test(expected = JobParametersNotFoundException.class) public void incrementerReturnsNullThrows() throws Exception { when(jobParametersIncrementer.getNext(Matchers.any(JobParameters.class))).thenReturn(null); batchOperator.start(JOB_NAME, null); }
@Test(expected = JobParametersNotFoundException.class) public void startJobWithoutIncrementerThrows() throws Exception { when(job.getJobParametersIncrementer()).thenReturn(null); batchOperator.start(JOB_NAME, null); }
@ResponseStatus(HttpStatus.NOT_FOUND) @ExceptionHandler(JobParametersNotFoundException.class) public String handleNoBootstrapParametersCreatedByIncrementer(Exception ex) { LOG.warn("JobParametersIncrementer didn't provide bootstrap parameters.",ex); return ex.getMessage(); }