diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleStepHandler.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleStepHandler.java index 1132425a5..33a2e52f7 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleStepHandler.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleStepHandler.java @@ -44,6 +44,8 @@ public class SimpleStepHandler implements StepHandler, InitializingBean { private JobRepository jobRepository; + private ExecutionContext executionContext; + /** * Convenient default constructor for configuration usage. */ @@ -55,8 +57,16 @@ public class SimpleStepHandler implements StepHandler, InitializingBean { * @param jobRepository */ public SimpleStepHandler(JobRepository jobRepository) { - super(); + this(jobRepository, new ExecutionContext()); + } + + /** + * @param jobRepository + * @param executionContext + */ + public SimpleStepHandler(JobRepository jobRepository, ExecutionContext executionContext) { this.jobRepository = jobRepository; + this.executionContext = executionContext; } /** @@ -75,6 +85,16 @@ public class SimpleStepHandler implements StepHandler, InitializingBean { this.jobRepository = jobRepository; } + /** + * A context containing values to be added to the step execution before it + * is handled. + * + * @param executionContext the execution context to set + */ + public void setExecutionContext(ExecutionContext executionContext) { + this.executionContext = executionContext; + } + public StepExecution handleStep(Step step, JobExecution execution) throws JobInterruptedException, JobRestartException, StartLimitExceededException { if (execution.isStopping()) { @@ -105,12 +125,12 @@ public class SimpleStepHandler implements StepHandler, InitializingBean { currentStepExecution.setExecutionContext(lastStepExecution.getExecutionContext()); } else { - currentStepExecution.setExecutionContext(new ExecutionContext()); + currentStepExecution.setExecutionContext(new ExecutionContext(executionContext)); } jobRepository.add(currentStepExecution); - logger.info("Executing step: [" + step + "]"); + logger.info("Executing step: [" + step.getName() + "]"); try { step.execute(currentStepExecution); } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/FlowStep.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/FlowStep.java index 9a9a64293..9e578aa66 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/FlowStep.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/FlowStep.java @@ -67,7 +67,7 @@ public class FlowStep extends AbstractStep { @Override protected void doExecute(StepExecution stepExecution) throws Exception { try { - StepHandler stepHandler = new SimpleStepHandler(getJobRepository()); + StepHandler stepHandler = new SimpleStepHandler(getJobRepository(), stepExecution.getExecutionContext()); FlowExecutor executor = new JobFlowExecutor(getJobRepository(), stepHandler, stepExecution.getJobExecution()); executor.updateJobExecutionStatus(flow.start(executor).getStatus()); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowStepTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowStepTests.java index 084301171..64302b6f4 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowStepTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowStepTests.java @@ -97,6 +97,37 @@ public class FlowStepTests { } + /** + * Test method for {@link org.springframework.batch.core.job.flow.FlowStep#doExecute(org.springframework.batch.core.StepExecution)}. + */ + @Test + public void testExecuteWithParentContext() throws Exception { + + FlowStep step = new FlowStep(); + step.setJobRepository(jobRepository); + + SimpleFlow flow = new SimpleFlow("job"); + List transitions = new ArrayList(); + transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1")), "end0")); + transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end0"))); + flow.setStateTransitions(transitions); + + step.setFlow(flow); + step.afterPropertiesSet(); + + StepExecution stepExecution = jobExecution.createStepExecution("step"); + stepExecution.getExecutionContext().put("foo", "bar"); + jobRepository.add(stepExecution); + step.execute(stepExecution); + + stepExecution = getStepExecution(jobExecution, "step"); + assertEquals(ExitStatus.COMPLETED, stepExecution.getExitStatus()); + stepExecution = getStepExecution(jobExecution, "step1"); + assertEquals(ExitStatus.COMPLETED, stepExecution.getExitStatus()); + assertEquals("bar", stepExecution.getExecutionContext().get("foo")); + + } + /** * @author Dave Syer *