1 /*
2 * Copyright 2006-2013 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
17 package org.springframework.batch.core.job;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
22
23 import org.springframework.batch.core.BatchStatus;
24 import org.springframework.batch.core.Job;
25 import org.springframework.batch.core.JobExecution;
26 import org.springframework.batch.core.JobInterruptedException;
27 import org.springframework.batch.core.StartLimitExceededException;
28 import org.springframework.batch.core.Step;
29 import org.springframework.batch.core.StepExecution;
30 import org.springframework.batch.core.repository.JobRestartException;
31
32 /**
33 * Simple implementation of {@link Job} interface providing the ability to run a
34 * {@link JobExecution}. Sequentially executes a job by iterating through its
35 * list of steps. Any {@link Step} that fails will fail the job. The job is
36 * considered complete when all steps have been executed.
37 *
38 * @author Lucas Ward
39 * @author Dave Syer
40 */
41 public class SimpleJob extends AbstractJob {
42
43 private List<Step> steps = new ArrayList<Step>();
44
45 /**
46 * Default constructor for job with null name
47 */
48 public SimpleJob() {
49 this(null);
50 }
51
52 /**
53 * @param name
54 */
55 public SimpleJob(String name) {
56 super(name);
57 }
58
59 /**
60 * Public setter for the steps in this job. Overrides any calls to
61 * {@link #addStep(Step)}.
62 *
63 * @param steps the steps to execute
64 */
65 public void setSteps(List<Step> steps) {
66 this.steps.clear();
67 this.steps.addAll(steps);
68 }
69
70 /**
71 * Convenience method for clients to inspect the steps for this job.
72 *
73 * @return the step names for this job
74 */
75 @Override
76 public Collection<String> getStepNames() {
77 List<String> names = new ArrayList<String>();
78 for (Step step : steps) {
79 names.add(step.getName());
80 }
81 return names;
82 }
83
84 /**
85 * Convenience method for adding a single step to the job.
86 *
87 * @param step a {@link Step} to add
88 */
89 public void addStep(Step step) {
90 this.steps.add(step);
91 }
92
93 /*
94 * (non-Javadoc)
95 *
96 * @see
97 * org.springframework.batch.core.job.AbstractJob#getStep(java.lang.String)
98 */
99 @Override
100 public Step getStep(String stepName) {
101 for (Step step : this.steps) {
102 if (step.getName().equals(stepName)) {
103 return step;
104 }
105 }
106 return null;
107 }
108
109 /**
110 * Handler of steps sequentially as provided, checking each one for success
111 * before moving to the next. Returns the last {@link StepExecution}
112 * successfully processed if it exists, and null if none were processed.
113 *
114 * @param execution the current {@link JobExecution}
115 *
116 * @see AbstractJob#handleStep(Step, JobExecution)
117 */
118 @Override
119 protected void doExecute(JobExecution execution) throws JobInterruptedException, JobRestartException,
120 StartLimitExceededException {
121
122 StepExecution stepExecution = null;
123 for (Step step : steps) {
124 stepExecution = handleStep(step, execution);
125 if (stepExecution.getStatus() != BatchStatus.COMPLETED) {
126 //
127 // Terminate the job if a step fails
128 //
129 break;
130 }
131 }
132
133 //
134 // Update the job status to be the same as the last step
135 //
136 if (stepExecution != null) {
137 logger.debug("Upgrading JobExecution status: " + stepExecution);
138 execution.upgradeStatus(stepExecution.getStatus());
139 execution.setExitStatus(stepExecution.getExitStatus());
140 }
141 }
142
143 }