| 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; |
| 18 | |
| 19 | import java.util.Date; |
| 20 | |
| 21 | import org.springframework.batch.item.ExecutionContext; |
| 22 | import org.springframework.batch.repeat.ExitStatus; |
| 23 | import org.springframework.util.Assert; |
| 24 | |
| 25 | /** |
| 26 | * Batch domain object representation the execution of a step. Unlike |
| 27 | * JobExecution, there are four additional properties: itemCount, commitCount, |
| 28 | * rollbackCount and execution context. These values represent how many items |
| 29 | * the step has processed, how many times it has been committed and rolled back, |
| 30 | * and any other information the developer wishes to store, respectively. |
| 31 | * |
| 32 | * @author Lucas Ward |
| 33 | * @author Dave Syer |
| 34 | * |
| 35 | */ |
| 36 | public class StepExecution extends Entity { |
| 37 | |
| 38 | private final JobExecution jobExecution; |
| 39 | |
| 40 | private final String stepName; |
| 41 | |
| 42 | private volatile BatchStatus status = BatchStatus.STARTING; |
| 43 | |
| 44 | private volatile int itemCount = 0; |
| 45 | |
| 46 | private volatile int commitCount = 0; |
| 47 | |
| 48 | private volatile int rollbackCount = 0; |
| 49 | |
| 50 | private volatile int readSkipCount = 0; |
| 51 | |
| 52 | private volatile int writeSkipCount = 0; |
| 53 | |
| 54 | private volatile Date startTime = new Date(System.currentTimeMillis()); |
| 55 | |
| 56 | private volatile Date endTime = null; |
| 57 | |
| 58 | private volatile ExecutionContext executionContext = new ExecutionContext(); |
| 59 | |
| 60 | private volatile ExitStatus exitStatus = ExitStatus.CONTINUABLE; |
| 61 | |
| 62 | private volatile boolean terminateOnly; |
| 63 | |
| 64 | /** |
| 65 | * Constructor with mandatory properties. |
| 66 | * |
| 67 | * @param stepName the step to which this execution belongs |
| 68 | * @param jobExecution the current job execution |
| 69 | * @param id the id of this execution |
| 70 | */ |
| 71 | public StepExecution(String stepName, JobExecution jobExecution, Long id) { |
| 72 | super(id); |
| 73 | Assert.hasLength(stepName); |
| 74 | this.stepName = stepName; |
| 75 | this.jobExecution = jobExecution; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Constructor that substitutes in null for the execution id |
| 80 | * |
| 81 | * @param stepName the step to which this execution belongs |
| 82 | * @param jobExecution the current job execution |
| 83 | */ |
| 84 | public StepExecution(String stepName, JobExecution jobExecution) { |
| 85 | this(stepName, jobExecution, null); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the {@link ExecutionContext} for this execution |
| 90 | * |
| 91 | * @return the attributes |
| 92 | */ |
| 93 | public ExecutionContext getExecutionContext() { |
| 94 | return executionContext; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Sets the {@link ExecutionContext} for this execution |
| 99 | * |
| 100 | * @param executionContext the attributes |
| 101 | */ |
| 102 | public void setExecutionContext(ExecutionContext executionContext) { |
| 103 | this.executionContext = executionContext; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns the current number of commits for this execution |
| 108 | * |
| 109 | * @return the current number of commits |
| 110 | */ |
| 111 | public Integer getCommitCount() { |
| 112 | return new Integer(commitCount); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Sets the current number of commits for this execution |
| 117 | * |
| 118 | * @param commitCount the current number of commits |
| 119 | */ |
| 120 | public void setCommitCount(int commitCount) { |
| 121 | this.commitCount = commitCount; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns the time that this execution ended |
| 126 | * |
| 127 | * @return the time that this execution ended |
| 128 | */ |
| 129 | public Date getEndTime() { |
| 130 | return endTime; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Sets the time that this execution ended |
| 135 | * |
| 136 | * @param endTime the time that this execution ended |
| 137 | */ |
| 138 | public void setEndTime(Date endTime) { |
| 139 | this.endTime = endTime; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns the current number of items processed for this execution |
| 144 | * |
| 145 | * @return the current number of items processed for this execution |
| 146 | */ |
| 147 | public Integer getItemCount() { |
| 148 | return new Integer(itemCount); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Sets the current number of processed items for this execution |
| 153 | * |
| 154 | * @param itemCount the current number of processed items for this execution |
| 155 | */ |
| 156 | public void setItemCount(int itemCount) { |
| 157 | this.itemCount = itemCount; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns the current number of rollbacks for this execution |
| 162 | * |
| 163 | * @return the current number of rollbacks for this execution |
| 164 | */ |
| 165 | public Integer getRollbackCount() { |
| 166 | return new Integer(rollbackCount); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Setter for number of rollbacks for this execution |
| 171 | */ |
| 172 | public void setRollbackCount(int rollbackCount) { |
| 173 | this.rollbackCount = rollbackCount; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Gets the time this execution started |
| 178 | * |
| 179 | * @return the time this execution started |
| 180 | */ |
| 181 | public Date getStartTime() { |
| 182 | return startTime; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Sets the time this execution started |
| 187 | * |
| 188 | * @param startTime the time this execution started |
| 189 | */ |
| 190 | public void setStartTime(Date startTime) { |
| 191 | this.startTime = startTime; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Returns the current status of this step |
| 196 | * |
| 197 | * @return the current status of this step |
| 198 | */ |
| 199 | public BatchStatus getStatus() { |
| 200 | return status; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Sets the current status of this step |
| 205 | * |
| 206 | * @param status the current status of this step |
| 207 | */ |
| 208 | public void setStatus(BatchStatus status) { |
| 209 | this.status = status; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @return the name of the step |
| 214 | */ |
| 215 | public String getStepName() { |
| 216 | return stepName; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Accessor for the job execution id. |
| 221 | * |
| 222 | * @return the jobExecutionId |
| 223 | */ |
| 224 | public Long getJobExecutionId() { |
| 225 | if (jobExecution != null) { |
| 226 | return jobExecution.getId(); |
| 227 | } |
| 228 | return null; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * (non-Javadoc) |
| 233 | * |
| 234 | * @see org.springframework.batch.container.common.domain.Entity#equals(java.lang.Object) |
| 235 | */ |
| 236 | public boolean equals(Object obj) { |
| 237 | |
| 238 | Object jobExecutionId = getJobExecutionId(); |
| 239 | if (jobExecutionId == null || !(obj instanceof StepExecution) || getId() == null) { |
| 240 | return super.equals(obj); |
| 241 | } |
| 242 | StepExecution other = (StepExecution) obj; |
| 243 | |
| 244 | return stepName.equals(other.getStepName()) && (jobExecutionId.equals(other.getJobExecutionId())); |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * (non-Javadoc) |
| 249 | * |
| 250 | * @see org.springframework.batch.container.common.domain.Entity#hashCode() |
| 251 | */ |
| 252 | public int hashCode() { |
| 253 | Object jobExecutionId = getJobExecutionId(); |
| 254 | return super.hashCode() + 31 * (stepName != null ? stepName.hashCode() : 0) + 91 |
| 255 | * (jobExecutionId != null ? jobExecutionId.hashCode() : 0); |
| 256 | } |
| 257 | |
| 258 | public String toString() { |
| 259 | return super.toString() + ", name=" + stepName + ", itemCount=" + itemCount + ", commitCount=" |
| 260 | + commitCount + ", rollbackCount=" + rollbackCount; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @param exitStatus |
| 265 | */ |
| 266 | public void setExitStatus(ExitStatus exitStatus) { |
| 267 | this.exitStatus = exitStatus; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * @return the exitCode |
| 272 | */ |
| 273 | public ExitStatus getExitStatus() { |
| 274 | return exitStatus; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Accessor for the execution context information of the enclosing job. |
| 279 | * |
| 280 | * @return the {@link JobExecution} that was used to start this step |
| 281 | * execution. |
| 282 | */ |
| 283 | public JobExecution getJobExecution() { |
| 284 | return jobExecution; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Factory method for {@link StepContribution}. |
| 289 | * |
| 290 | * @return a new {@link StepContribution} |
| 291 | */ |
| 292 | public StepContribution createStepContribution() { |
| 293 | return new StepContribution(this); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * On successful execution just before a chunk commit, this method should be |
| 298 | * called. Synchronizes access to the {@link StepExecution} so that changes |
| 299 | * are atomic. |
| 300 | * |
| 301 | * @param contribution |
| 302 | */ |
| 303 | public synchronized void apply(StepContribution contribution) { |
| 304 | itemCount += contribution.getItemCount(); |
| 305 | commitCount += contribution.getCommitCount(); |
| 306 | |
| 307 | contribution.combineSkipCounts(); |
| 308 | readSkipCount += contribution.getReadSkipCount(); |
| 309 | writeSkipCount += contribution.getWriteSkipCount(); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * On unsuccessful execution after a chunk has rolled back. |
| 314 | */ |
| 315 | public synchronized void rollback() { |
| 316 | rollbackCount++; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * @return flag to indicate that an execution should halt |
| 321 | */ |
| 322 | public boolean isTerminateOnly() { |
| 323 | return this.terminateOnly; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Set a flag that will signal to an execution environment that this |
| 328 | * execution (and its surrounding job) wishes to exit. |
| 329 | */ |
| 330 | public void setTerminateOnly() { |
| 331 | this.terminateOnly = true; |
| 332 | } |
| 333 | |
| 334 | public int getSkipCount() { |
| 335 | return readSkipCount + writeSkipCount; |
| 336 | } |
| 337 | |
| 338 | public void incrementReadSkipCountBy(int count) { |
| 339 | readSkipCount += count; |
| 340 | } |
| 341 | |
| 342 | public void incrementWriteSkipCountBy(int count) { |
| 343 | writeSkipCount += count; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Convenience method to get the current job parameters. |
| 348 | * |
| 349 | * @return the {@link JobParameters} from the enclosing job, or empty if |
| 350 | * that is null |
| 351 | */ |
| 352 | public JobParameters getJobParameters() { |
| 353 | if (jobExecution == null || jobExecution.getJobInstance() == null) { |
| 354 | return new JobParameters(); |
| 355 | } |
| 356 | return jobExecution.getJobInstance().getJobParameters(); |
| 357 | } |
| 358 | |
| 359 | public Integer getReadSkipCount() { |
| 360 | return new Integer(readSkipCount); |
| 361 | } |
| 362 | |
| 363 | public Integer getWriteSkipCount() { |
| 364 | return new Integer(writeSkipCount); |
| 365 | } |
| 366 | |
| 367 | public void setReadSkipCount(int readSkipCount) { |
| 368 | this.readSkipCount = readSkipCount; |
| 369 | } |
| 370 | |
| 371 | public void setWriteSkipCount(int writeSkipCount) { |
| 372 | this.writeSkipCount = writeSkipCount; |
| 373 | } |
| 374 | |
| 375 | } |