From caf4c4c6ddaebf56ff0768c20c244c0dbd70b360 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 27 Oct 2017 14:53:25 +0200 Subject: [PATCH] Make use of Batch's new API for accessing job parameters Closes gh-10135 --- .../batch/JobLauncherCommandLineRunner.java | 71 +------------------ 1 file changed, 3 insertions(+), 68 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java index a0ce641562..c9fff0725a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java @@ -19,22 +19,16 @@ package org.springframework.boot.autoconfigure.batch; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobExecutionException; -import org.springframework.batch.core.JobInstance; -import org.springframework.batch.core.JobParameter; import org.springframework.batch.core.JobParameters; -import org.springframework.batch.core.JobParametersIncrementer; +import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.JobParametersInvalidException; import org.springframework.batch.core.configuration.JobRegistry; import org.springframework.batch.core.converter.DefaultJobParametersConverter; @@ -124,66 +118,6 @@ public class JobLauncherCommandLineRunner executeRegisteredJobs(jobParameters); } - private JobParameters getNextJobParameters(Job job, - JobParameters additionalParameters) { - String name = job.getName(); - JobParameters parameters = new JobParameters(); - List lastInstances = this.jobExplorer.getJobInstances(name, 0, 1); - JobParametersIncrementer incrementer = job.getJobParametersIncrementer(); - Map additionals = additionalParameters.getParameters(); - if (lastInstances.isEmpty()) { - // Start from a completely clean sheet - if (incrementer != null) { - parameters = incrementer.getNext(new JobParameters()); - } - } - else { - List previousExecutions = this.jobExplorer - .getJobExecutions(lastInstances.get(0)); - JobExecution previousExecution = previousExecutions.get(0); - if (previousExecution == null) { - // Normally this will not happen - an instance exists with no executions - if (incrementer != null) { - parameters = incrementer.getNext(new JobParameters()); - } - } - else if (isStoppedOrFailed(previousExecution) && job.isRestartable()) { - // Retry a failed or stopped execution - parameters = previousExecution.getJobParameters(); - // Non-identifying additional parameters can be removed to a retry - removeNonIdentifying(additionals); - } - else if (incrementer != null) { - // New instance so increment the parameters if we can - parameters = incrementer.getNext(previousExecution.getJobParameters()); - } - } - return merge(parameters, additionals); - } - - private boolean isStoppedOrFailed(JobExecution execution) { - BatchStatus status = execution.getStatus(); - return (status == BatchStatus.STOPPED || status == BatchStatus.FAILED); - } - - private void removeNonIdentifying(Map parameters) { - HashMap copy = new HashMap<>(parameters); - for (Map.Entry parameter : copy.entrySet()) { - if (!parameter.getValue().isIdentifying()) { - parameters.remove(parameter.getKey()); - } - } - } - - private JobParameters merge(JobParameters parameters, - Map additionals) { - Map merged = new HashMap<>(); - merged.putAll(parameters.getParameters()); - merged.putAll(additionals); - parameters = new JobParameters(merged); - return parameters; - } - private void executeRegisteredJobs(JobParameters jobParameters) throws JobExecutionException { if (this.jobRegistry != null && StringUtils.hasText(this.jobNames)) { @@ -208,7 +142,8 @@ public class JobLauncherCommandLineRunner throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException, JobParametersNotFoundException { - JobParameters nextParameters = getNextJobParameters(job, jobParameters); + JobParameters nextParameters = new JobParametersBuilder(jobParameters, this.jobExplorer) + .getNextJobParameters(job).toJobParameters(); if (nextParameters != null) { JobExecution execution = this.jobLauncher.run(job, nextParameters); if (this.publisher != null) {