| 1 | /* |
| 2 | * Copyright 2006-2007 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 | public Collection<String> getStepNames() { |
| 76 | List<String> names = new ArrayList<String>(); |
| 77 | for (Step step : steps) { |
| 78 | names.add(step.getName()); |
| 79 | } |
| 80 | return names; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Convenience method for adding a single step to the job. |
| 85 | * |
| 86 | * @param step a {@link Step} to add |
| 87 | */ |
| 88 | public void addStep(Step step) { |
| 89 | this.steps.add(step); |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * (non-Javadoc) |
| 94 | * |
| 95 | * @see |
| 96 | * org.springframework.batch.core.job.AbstractJob#getStep(java.lang.String) |
| 97 | */ |
| 98 | public Step getStep(String stepName) { |
| 99 | for (Step step : this.steps) { |
| 100 | if (step.getName().equals(stepName)) { |
| 101 | return step; |
| 102 | } |
| 103 | } |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Handler of steps sequentially as provided, checking each one for success |
| 109 | * before moving to the next. Returns the last {@link StepExecution} |
| 110 | * successfully processed if it exists, and null if none were processed. |
| 111 | * |
| 112 | * @param execution the current {@link JobExecution} |
| 113 | * |
| 114 | * @see AbstractJob#handleStep(Step, JobExecution) |
| 115 | */ |
| 116 | protected void doExecute(JobExecution execution) throws JobInterruptedException, JobRestartException, |
| 117 | StartLimitExceededException { |
| 118 | |
| 119 | StepExecution stepExecution = null; |
| 120 | for (Step step : steps) { |
| 121 | stepExecution = handleStep(step, execution); |
| 122 | if (stepExecution.getStatus() != BatchStatus.COMPLETED) { |
| 123 | // |
| 124 | // Terminate the job if a step fails |
| 125 | // |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // |
| 131 | // Update the job status to be the same as the last step |
| 132 | // |
| 133 | if (stepExecution != null) { |
| 134 | logger.debug("Upgrading JobExecution status: " + stepExecution); |
| 135 | execution.upgradeStatus(stepExecution.getStatus()); |
| 136 | execution.setExitStatus(stepExecution.getExitStatus()); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | } |