1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.batch.core.step.job;
17
18 import org.springframework.batch.core.Job;
19 import org.springframework.batch.core.JobExecution;
20 import org.springframework.batch.core.JobParameters;
21 import org.springframework.batch.core.Step;
22 import org.springframework.batch.core.StepExecution;
23 import org.springframework.batch.core.UnexpectedJobExecutionException;
24 import org.springframework.batch.core.launch.JobLauncher;
25 import org.springframework.batch.core.step.AbstractStep;
26 import org.springframework.batch.item.ExecutionContext;
27 import org.springframework.util.Assert;
28
29
30
31
32
33
34
35
36
37
38
39
40 public class JobStep extends AbstractStep {
41
42
43
44
45
46 private static final String JOB_PARAMETERS_KEY = JobStep.class.getName() + ".JOB_PARAMETERS";
47
48 private Job job;
49
50 private JobLauncher jobLauncher;
51
52 private JobParametersExtractor jobParametersExtractor = new DefaultJobParametersExtractor();
53
54 @Override
55 public void afterPropertiesSet() throws Exception {
56 super.afterPropertiesSet();
57 Assert.state(jobLauncher != null, "A JobLauncher must be provided");
58 Assert.state(job != null, "A Job must be provided");
59 }
60
61
62
63
64
65
66 public void setJob(Job job) {
67 this.job = job;
68 }
69
70
71
72
73
74
75
76 public void setJobLauncher(JobLauncher jobLauncher) {
77 this.jobLauncher = jobLauncher;
78 }
79
80
81
82
83
84
85
86
87
88 public void setJobParametersExtractor(JobParametersExtractor jobParametersExtractor) {
89 this.jobParametersExtractor = jobParametersExtractor;
90 }
91
92
93
94
95
96
97
98
99
100
101 @Override
102 protected void doExecute(StepExecution stepExecution) throws Exception {
103
104 ExecutionContext executionContext = stepExecution.getExecutionContext();
105
106 JobParameters jobParameters;
107 if (executionContext.containsKey(JOB_PARAMETERS_KEY)) {
108 jobParameters = (JobParameters) executionContext.get(JOB_PARAMETERS_KEY);
109 }
110 else {
111 jobParameters = jobParametersExtractor.getJobParameters(job, stepExecution);
112 executionContext.put(JOB_PARAMETERS_KEY, jobParameters);
113 }
114
115 JobExecution jobExecution = jobLauncher.run(job, jobParameters);
116 if (jobExecution.getStatus().isUnsuccessful()) {
117
118 throw new UnexpectedJobExecutionException("Step failure: the delegate Job failed in JobStep.");
119 }
120
121 }
122
123 }