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