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