| 1 | /* |
| 2 | * Copyright 2006-2009 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 org.apache.commons.logging.Log; |
| 20 | import org.apache.commons.logging.LogFactory; |
| 21 | import org.springframework.batch.core.BatchStatus; |
| 22 | import org.springframework.batch.core.JobExecution; |
| 23 | import org.springframework.batch.core.JobInstance; |
| 24 | import org.springframework.batch.core.JobInterruptedException; |
| 25 | import org.springframework.batch.core.StartLimitExceededException; |
| 26 | import org.springframework.batch.core.Step; |
| 27 | import org.springframework.batch.core.StepExecution; |
| 28 | import org.springframework.batch.core.repository.JobRepository; |
| 29 | import org.springframework.batch.core.repository.JobRestartException; |
| 30 | import org.springframework.batch.item.ExecutionContext; |
| 31 | import org.springframework.beans.factory.InitializingBean; |
| 32 | import org.springframework.util.Assert; |
| 33 | |
| 34 | /** |
| 35 | * Implementation of {@link StepHandler} that manages repository and restart |
| 36 | * concerns. |
| 37 | * |
| 38 | * @author Dave Syer |
| 39 | * |
| 40 | */ |
| 41 | public class SimpleStepHandler implements StepHandler, InitializingBean { |
| 42 | |
| 43 | private static final Log logger = LogFactory.getLog(SimpleStepHandler.class); |
| 44 | |
| 45 | private JobRepository jobRepository; |
| 46 | |
| 47 | private ExecutionContext executionContext; |
| 48 | |
| 49 | /** |
| 50 | * Convenient default constructor for configuration usage. |
| 51 | */ |
| 52 | public SimpleStepHandler() { |
| 53 | this(null); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param jobRepository |
| 58 | */ |
| 59 | public SimpleStepHandler(JobRepository jobRepository) { |
| 60 | this(jobRepository, new ExecutionContext()); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param jobRepository |
| 65 | * @param executionContext |
| 66 | */ |
| 67 | public SimpleStepHandler(JobRepository jobRepository, ExecutionContext executionContext) { |
| 68 | this.jobRepository = jobRepository; |
| 69 | this.executionContext = executionContext; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Check mandatory properties (jobRepository). |
| 74 | * |
| 75 | * @see InitializingBean#afterPropertiesSet() |
| 76 | */ |
| 77 | public void afterPropertiesSet() throws Exception { |
| 78 | Assert.state(jobRepository != null, "A JobRepository must be provided"); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param jobRepository the jobRepository to set |
| 83 | */ |
| 84 | public void setJobRepository(JobRepository jobRepository) { |
| 85 | this.jobRepository = jobRepository; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * A context containing values to be added to the step execution before it |
| 90 | * is handled. |
| 91 | * |
| 92 | * @param executionContext the execution context to set |
| 93 | */ |
| 94 | public void setExecutionContext(ExecutionContext executionContext) { |
| 95 | this.executionContext = executionContext; |
| 96 | } |
| 97 | |
| 98 | public StepExecution handleStep(Step step, JobExecution execution) throws JobInterruptedException, |
| 99 | JobRestartException, StartLimitExceededException { |
| 100 | if (execution.isStopping()) { |
| 101 | throw new JobInterruptedException("JobExecution interrupted."); |
| 102 | } |
| 103 | |
| 104 | JobInstance jobInstance = execution.getJobInstance(); |
| 105 | |
| 106 | StepExecution lastStepExecution = jobRepository.getLastStepExecution(jobInstance, step.getName()); |
| 107 | if (stepExecutionPartOfExistingJobExecution(execution, lastStepExecution)) { |
| 108 | // If the last execution of this step was in the same job, it's |
| 109 | // probably intentional so we want to run it again... |
| 110 | logger.info(String.format("Duplicate step [%s] detected in execution of job=[%s]. " |
| 111 | + "If either step fails, both will be executed again on restart.", step.getName(), jobInstance |
| 112 | .getJobName())); |
| 113 | lastStepExecution = null; |
| 114 | } |
| 115 | StepExecution currentStepExecution = lastStepExecution; |
| 116 | |
| 117 | if (shouldStart(lastStepExecution, jobInstance, step)) { |
| 118 | |
| 119 | currentStepExecution = execution.createStepExecution(step.getName()); |
| 120 | |
| 121 | boolean isRestart = (lastStepExecution != null && !lastStepExecution.getStatus().equals( |
| 122 | BatchStatus.COMPLETED)); |
| 123 | |
| 124 | if (isRestart) { |
| 125 | currentStepExecution.setExecutionContext(lastStepExecution.getExecutionContext()); |
| 126 | } |
| 127 | else { |
| 128 | currentStepExecution.setExecutionContext(new ExecutionContext(executionContext)); |
| 129 | } |
| 130 | |
| 131 | jobRepository.add(currentStepExecution); |
| 132 | |
| 133 | logger.info("Executing step: [" + step.getName() + "]"); |
| 134 | try { |
| 135 | step.execute(currentStepExecution); |
| 136 | } |
| 137 | catch (JobInterruptedException e) { |
| 138 | // Ensure that the job gets the message that it is stopping |
| 139 | // and can pass it on to other steps that are executing |
| 140 | // concurrently. |
| 141 | execution.setStatus(BatchStatus.STOPPING); |
| 142 | throw e; |
| 143 | } |
| 144 | |
| 145 | jobRepository.updateExecutionContext(execution); |
| 146 | |
| 147 | if (currentStepExecution.getStatus() == BatchStatus.STOPPING |
| 148 | || currentStepExecution.getStatus() == BatchStatus.STOPPED) { |
| 149 | // Ensure that the job gets the message that it is stopping |
| 150 | execution.setStatus(BatchStatus.STOPPING); |
| 151 | throw new JobInterruptedException("Job interrupted by step execution"); |
| 152 | } |
| 153 | |
| 154 | } |
| 155 | else { |
| 156 | // currentStepExecution.setExitStatus(ExitStatus.NOOP); |
| 157 | } |
| 158 | |
| 159 | return currentStepExecution; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Detect whether a step execution belongs to this job execution. |
| 164 | * @param jobExecution the current job execution |
| 165 | * @param stepExecution an existing step execution |
| 166 | * @return |
| 167 | */ |
| 168 | private boolean stepExecutionPartOfExistingJobExecution(JobExecution jobExecution, StepExecution stepExecution) { |
| 169 | return stepExecution != null && stepExecution.getJobExecutionId() != null |
| 170 | && stepExecution.getJobExecutionId().equals(jobExecution.getId()); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Given a step and configuration, return true if the step should start, |
| 175 | * false if it should not, and throw an exception if the job should finish. |
| 176 | * @param lastStepExecution the last step execution |
| 177 | * @param jobInstance |
| 178 | * @param step |
| 179 | * |
| 180 | * @throws StartLimitExceededException if the start limit has been exceeded |
| 181 | * for this step |
| 182 | * @throws JobRestartException if the job is in an inconsistent state from |
| 183 | * an earlier failure |
| 184 | */ |
| 185 | private boolean shouldStart(StepExecution lastStepExecution, JobInstance jobInstance, Step step) |
| 186 | throws JobRestartException, StartLimitExceededException { |
| 187 | |
| 188 | BatchStatus stepStatus; |
| 189 | if (lastStepExecution == null) { |
| 190 | stepStatus = BatchStatus.STARTING; |
| 191 | } |
| 192 | else { |
| 193 | stepStatus = lastStepExecution.getStatus(); |
| 194 | } |
| 195 | |
| 196 | if (stepStatus == BatchStatus.UNKNOWN) { |
| 197 | throw new JobRestartException("Cannot restart step from UNKNOWN status. " |
| 198 | + "The last execution ended with a failure that could not be rolled back, " |
| 199 | + "so it may be dangerous to proceed. Manual intervention is probably necessary."); |
| 200 | } |
| 201 | |
| 202 | if ((stepStatus == BatchStatus.COMPLETED && step.isAllowStartIfComplete() == false) |
| 203 | || stepStatus == BatchStatus.ABANDONED) { |
| 204 | // step is complete, false should be returned, indicating that the |
| 205 | // step should not be started |
| 206 | logger.info("Step already complete or not restartable, so no action to execute: " + lastStepExecution); |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | if (jobRepository.getStepExecutionCount(jobInstance, step.getName()) < step.getStartLimit()) { |
| 211 | // step start count is less than start max, return true |
| 212 | return true; |
| 213 | } |
| 214 | else { |
| 215 | // start max has been exceeded, throw an exception. |
| 216 | throw new StartLimitExceededException("Maximum start limit exceeded for step: " + step.getName() |
| 217 | + "StartMax: " + step.getStartLimit()); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | } |