1 | /* |
2 | * Copyright 2006-2008 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
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 | * A {@link Step} that delegates to a {@link Job} to do its work. This is a |
31 | * great tool for managing dependencies between jobs, and also to modularise |
32 | * complex step logic into something that is testable in isolation. The job is |
33 | * executed with parameters that can be extracted from the step execution, hence |
34 | * this step can also be usefully used as the worker in a parallel or |
35 | * partitioned execution. |
36 | * |
37 | * @author Dave Syer |
38 | * |
39 | */ |
40 | public class JobStep extends AbstractStep { |
41 | |
42 | /** |
43 | * The key for the job parameters in the step execution context. Needed for |
44 | * restarts. |
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 | * The {@link Job} to delegate to in this step. |
63 | * |
64 | * @param job a {@link Job} |
65 | */ |
66 | public void setJob(Job job) { |
67 | this.job = job; |
68 | } |
69 | |
70 | /** |
71 | * A {@link JobLauncher} is required to be able to run the enclosed |
72 | * {@link Job}. |
73 | * |
74 | * @param jobLauncher the {@link JobLauncher} to set |
75 | */ |
76 | public void setJobLauncher(JobLauncher jobLauncher) { |
77 | this.jobLauncher = jobLauncher; |
78 | } |
79 | |
80 | /** |
81 | * The {@link JobParametersExtractor} is used to extract |
82 | * {@link JobParametersExtractor} from the {@link StepExecution} to run the |
83 | * {@link Job}. By default an instance will be provided that simply copies |
84 | * the {@link JobParameters} from the parent job. |
85 | * |
86 | * @param jobParametersExtractor the {@link JobParametersExtractor} to set |
87 | */ |
88 | public void setJobParametersExtractor(JobParametersExtractor jobParametersExtractor) { |
89 | this.jobParametersExtractor = jobParametersExtractor; |
90 | } |
91 | |
92 | /** |
93 | * Execute the job provided by delegating to the {@link JobLauncher} to |
94 | * prevent duplicate executions. The job parameters will be generated by the |
95 | * {@link JobParametersExtractor} provided (if any), otherwise empty. On a |
96 | * restart, the job parameters will be the same as the last (failed) |
97 | * execution. |
98 | * |
99 | * @see AbstractStep#doExecute(StepExecution) |
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 | // AbstractStep will take care of the step execution status |
118 | throw new UnexpectedJobExecutionException("Step failure: the delegate Job failed in JobStep."); |
119 | } |
120 | |
121 | } |
122 | |
123 | } |