From ca847e27601ed67bc00f670e971d243188ef2f6a Mon Sep 17 00:00:00 2001 From: Michael Minella Date: Fri, 14 Oct 2016 12:07:48 -0500 Subject: [PATCH] Addressed stopped jobs from a job step Prior to this commit, when using the `JobStep`, if the child job was stopped (left in the `STOPPED` state), the step executing that job was marked as `COMPLETE`, preventing it from being restarted. This commit now marks a step that was executing the stopped job also as stopped, allowing for it to be restarted. Resolves BATCH-2429 --- .../batch/core/step/job/JobStep.java | 4 +++ .../batch/core/step/job/JobStepTests.java | 36 +++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/job/JobStep.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/job/JobStep.java index 93ecb313d..4a2214c44 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/job/JobStep.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/job/JobStep.java @@ -15,6 +15,7 @@ */ package org.springframework.batch.core.step.job; +import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; @@ -119,6 +120,9 @@ public class JobStep extends AbstractStep { // AbstractStep will take care of the step execution status throw new UnexpectedJobExecutionException("Step failure: the delegate Job failed in JobStep."); } + else if(jobExecution.getStatus().equals(BatchStatus.STOPPED)) { + stepExecution.setStatus(BatchStatus.STOPPED); + } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/job/JobStepTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/job/JobStepTests.java index 45a119fd5..c44e4e14e 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/job/JobStepTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/job/JobStepTests.java @@ -15,8 +15,11 @@ */ package org.springframework.batch.core.step.job; +import java.util.Date; + import org.junit.Before; import org.junit.Test; + import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; @@ -28,8 +31,6 @@ import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean; import org.springframework.batch.item.ExecutionContext; -import java.util.Date; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -171,4 +172,35 @@ public class JobStepTests { } + @Test + public void testStoppedChild() throws Exception { + + DefaultJobParametersExtractor jobParametersExtractor = new DefaultJobParametersExtractor(); + jobParametersExtractor.setKeys(new String[] {"foo"}); + ExecutionContext executionContext = stepExecution.getExecutionContext(); + executionContext.put("foo", "bar"); + step.setJobParametersExtractor(jobParametersExtractor); + + step.setJob(new JobSupport("child") { + @Override + public void execute(JobExecution execution) { + assertEquals(1, execution.getJobParameters().getParameters().size()); + execution.setStatus(BatchStatus.STOPPED); + execution.setEndTime(new Date()); + jobRepository.update(execution); + } + @Override + public boolean isRestartable() { + return true; + } + }); + + step.afterPropertiesSet(); + step.execute(stepExecution); + JobExecution jobExecution = stepExecution.getJobExecution(); + jobExecution.setEndTime(new Date()); + jobRepository.update(jobExecution); + + assertEquals(BatchStatus.STOPPED, stepExecution.getStatus()); + } }