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
14
15
16
17
18
19
20
21
22
23 public class FlowStep extends AbstractStep {
24
25 private Flow flow;
26
27
28
29
30 public FlowStep() {
31 super(null);
32 }
33
34
35
36
37
38 public FlowStep(Flow flow) {
39 super(flow.getName());
40 }
41
42
43
44
45
46
47 public void setFlow(Flow flow) {
48 this.flow = flow;
49 }
50
51
52
53
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
66
67
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 }