View Javadoc

1   package org.springframework.batch.core.job.flow;
2   
3   import org.springframework.batch.core.JobExecutionException;
4   import org.springframework.batch.core.Step;
5   import org.springframework.batch.core.StepExecution;
6   import org.springframework.batch.core.job.SimpleStepHandler;
7   import org.springframework.batch.core.job.StepHandler;
8   import org.springframework.batch.core.repository.JobRepository;
9   import org.springframework.batch.core.step.AbstractStep;
10  import org.springframework.util.Assert;
11  
12  /**
13   * A {@link Step} implementation that delegates to a {@link Flow}. Useful for
14   * logical grouping of steps, and especially for partitioning with multiple
15   * steps per execution. If the flow has steps then when the {@link FlowStep}
16   * executes, all steps including the parent {@link FlowStep} will have
17   * executions in the {@link JobRepository} (one for the parent and one each for
18   * the flow steps).
19   * 
20   * @author Dave Syer
21   * 
22   */
23  public class FlowStep extends AbstractStep {
24  
25  	private Flow flow;
26  
27  	/**
28  	 * Default constructor convenient for configuration purposes.
29  	 */
30  	public FlowStep() {
31  		super(null);
32  	}
33  
34  	/**
35  	 * Constructor for a {@link FlowStep} that sets the flow and of the step
36  	 * explicitly.
37  	 */
38  	public FlowStep(Flow flow) {
39  		super(flow.getName());
40  	}
41  
42  	/**
43  	 * Public setter for the flow.
44  	 * 
45  	 * @param flow the flow to set
46  	 */
47  	public void setFlow(Flow flow) {
48  		this.flow = flow;
49  	}
50  
51  	/**
52  	 * Ensure that the flow is set.
53  	 * @see AbstractStep#afterPropertiesSet()
54  	 */
55  	@Override
56  	public void afterPropertiesSet() throws Exception {
57  		Assert.state(flow != null, "A Flow must be provided");
58  		if (getName()==null) {
59  			setName(flow.getName());
60  		}
61  		super.afterPropertiesSet();
62  	}
63  
64  	/**
65  	 * Delegate to the flow provided for the execution of the step.
66  	 * 
67  	 * @see AbstractStep#doExecute(StepExecution)
68  	 */
69  	@Override
70  	protected void doExecute(StepExecution stepExecution) throws Exception {
71  		try {
72  			StepHandler stepHandler = new SimpleStepHandler(getJobRepository(), stepExecution.getExecutionContext());
73  			FlowExecutor executor = new JobFlowExecutor(getJobRepository(), stepHandler, stepExecution.getJobExecution());
74  			executor.updateJobExecutionStatus(flow.start(executor).getStatus());
75  			stepExecution.upgradeStatus(executor.getJobExecution().getStatus());
76  			stepExecution.setExitStatus(executor.getJobExecution().getExitStatus());
77  		}
78  		catch (FlowExecutionException e) {
79  			if (e.getCause() instanceof JobExecutionException) {
80  				throw (JobExecutionException) e.getCause();
81  			}
82  			throw new JobExecutionException("Flow execution ended unexpectedly", e);
83  		}
84  	}
85  
86  }