RESOLVED - issue BATCH-1559: Ability to promote FlowStep execution context to its constituent steps
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user