RESOLVED - issue BATCH-1559: Ability to promote FlowStep execution context to its constituent steps

This commit is contained in:
dsyer
2010-04-23 09:31:04 +00:00
parent 34af058110
commit fc7b86517f
3 changed files with 55 additions and 4 deletions

View File

@@ -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);
}

View File

@@ -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());
}

View File

@@ -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<StateTransition> transitions = new ArrayList<StateTransition>();
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
*