diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunner.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunner.java index a343346eb3e6..416fc683fe25 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunner.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunner.java @@ -39,6 +39,8 @@ import org.springframework.batch.core.converter.JobParametersConverter; import org.springframework.batch.core.explore.JobExplorer; import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.launch.support.ExitCodeMapper; +import org.springframework.batch.core.launch.support.SimpleJvmExitCodeMapper; import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; import org.springframework.batch.core.repository.JobRepository; @@ -54,6 +56,8 @@ import org.springframework.util.Assert; import org.springframework.util.StringUtils; +import static org.springframework.batch.core.launch.support.ExitCodeMapper.JVM_EXITCODE_COMPLETED; + /** * {@link ApplicationRunner} to {@link JobLauncher launch} Spring Batch jobs. If a single * job is found in the context, it will be executed by default. If multiple jobs are @@ -64,7 +68,8 @@ * @author Mahmoud Ben Hassine * @author Stephane Nicoll * @author Akshay Dubey - * @since 2.3.0 + * @author Minjun Kang + * @since 3.3.1 */ public class JobLauncherApplicationRunner implements ApplicationRunner, InitializingBean, Ordered, ApplicationEventPublisherAware { @@ -92,6 +97,8 @@ public class JobLauncherApplicationRunner private int order = DEFAULT_ORDER; + private ExitCodeMapper exitCodeMapper = new SimpleJvmExitCodeMapper(); + private ApplicationEventPublisher publisher; /** @@ -173,6 +180,7 @@ protected void launchJobFromProperties(Properties properties) throws JobExecutio JobParameters jobParameters = this.converter.getJobParameters(properties); executeLocalJobs(jobParameters); executeRegisteredJobs(jobParameters); + validateJobTerminateStatus(); } private boolean isLocalJob(String jobName) { @@ -204,6 +212,15 @@ private void executeRegisteredJobs(JobParameters jobParameters) throws JobExecut } } + private void validateJobTerminateStatus() throws JobExecutionException { + String exitCode = jobExplorer.getLastJobExecution(jobExplorer.getLastJobInstance(this.jobName)) + .getExitStatus().getExitCode(); + + if (exitCodeMapper.intValue(exitCode) != JVM_EXITCODE_COMPLETED) { + throw new JobExecutionException("Job Terminated with BatchStatus : " + exitCode); + } + } + protected void execute(Job job, JobParameters jobParameters) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException { JobParameters parameters = getNextJobParameters(job, jobParameters);