| 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.Collection; |
| 20 | import java.util.Date; |
| 21 | import java.util.HashSet; |
| 22 | import java.util.Iterator; |
| 23 | |
| 24 | import org.springframework.batch.item.ExecutionContext; |
| 25 | import org.springframework.batch.repeat.ExitStatus; |
| 26 | |
| 27 | /** |
| 28 | * Batch domain object representing the execution of a job. |
| 29 | * |
| 30 | * @author Lucas Ward |
| 31 | * |
| 32 | */ |
| 33 | public class JobExecution extends Entity { |
| 34 | |
| 35 | private final JobInstance jobInstance; |
| 36 | |
| 37 | private volatile transient Collection stepExecutions = new HashSet(); |
| 38 | |
| 39 | private volatile BatchStatus status = BatchStatus.STARTING; |
| 40 | |
| 41 | private volatile Date startTime = null; |
| 42 | |
| 43 | private volatile Date createTime = new Date(System.currentTimeMillis()); |
| 44 | |
| 45 | private volatile Date endTime = null; |
| 46 | |
| 47 | private volatile ExitStatus exitStatus = ExitStatus.UNKNOWN; |
| 48 | |
| 49 | private volatile ExecutionContext executionContext = new ExecutionContext(); |
| 50 | |
| 51 | /** |
| 52 | * Because a JobExecution isn't valid unless the job is set, this |
| 53 | * constructor is the only valid one from a modelling point of view. |
| 54 | * |
| 55 | * @param job the job of which this execution is a part |
| 56 | */ |
| 57 | public JobExecution(JobInstance job, Long id) { |
| 58 | super(id); |
| 59 | this.jobInstance = job; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Constructor for transient (unsaved) instances. |
| 64 | * |
| 65 | * @param job the enclosing {@link JobInstance} |
| 66 | */ |
| 67 | public JobExecution(JobInstance job) { |
| 68 | this(job, null); |
| 69 | } |
| 70 | |
| 71 | public Date getEndTime() { |
| 72 | return endTime; |
| 73 | } |
| 74 | |
| 75 | public void setEndTime(Date endTime) { |
| 76 | this.endTime = endTime; |
| 77 | } |
| 78 | |
| 79 | public Date getStartTime() { |
| 80 | return startTime; |
| 81 | } |
| 82 | |
| 83 | public void setStartTime(Date startTime) { |
| 84 | this.startTime = startTime; |
| 85 | } |
| 86 | |
| 87 | public BatchStatus getStatus() { |
| 88 | return status; |
| 89 | } |
| 90 | |
| 91 | public void setStatus(BatchStatus status) { |
| 92 | this.status = status; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Convenience getter for for the id of the enclosing job. Useful for DAO |
| 97 | * implementations. |
| 98 | * |
| 99 | * @return the id of the enclosing job |
| 100 | */ |
| 101 | public Long getJobId() { |
| 102 | if (jobInstance != null) { |
| 103 | return jobInstance.getId(); |
| 104 | } |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @param exitStatus |
| 110 | */ |
| 111 | public void setExitStatus(ExitStatus exitStatus) { |
| 112 | this.exitStatus = exitStatus; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @return the exitCode |
| 117 | */ |
| 118 | public ExitStatus getExitStatus() { |
| 119 | return exitStatus; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @return the Job that is executing. |
| 124 | */ |
| 125 | public JobInstance getJobInstance() { |
| 126 | return jobInstance; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Accessor for the step executions. |
| 131 | * |
| 132 | * @return the step executions that were registered |
| 133 | */ |
| 134 | public Collection getStepExecutions() { |
| 135 | return stepExecutions; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Register a step execution with the current job execution. |
| 140 | */ |
| 141 | public StepExecution createStepExecution(Step step) { |
| 142 | StepExecution stepExecution = new StepExecution(step.getName(), this, null); |
| 143 | this.stepExecutions.add(stepExecution); |
| 144 | return stepExecution; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * (non-Javadoc) |
| 149 | * @see org.springframework.batch.core.domain.Entity#toString() |
| 150 | */ |
| 151 | public String toString() { |
| 152 | return super.toString() + ", startTime=" + startTime + ", endTime=" + endTime + ", job=[" + jobInstance + "]"; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Test if this {@link JobExecution} indicates that it is running. It should |
| 157 | * be noted that this does not necessarily mean that it has been persisted |
| 158 | * as such yet. |
| 159 | * @return true if the end time is null |
| 160 | */ |
| 161 | public boolean isRunning() { |
| 162 | return endTime == null; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Test if this {@link JobExecution} indicates that it has been signalled to |
| 167 | * stop. |
| 168 | * @return true if the status is {@link BatchStatus#STOPPING} |
| 169 | */ |
| 170 | public boolean isStopping() { |
| 171 | return status == BatchStatus.STOPPING; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Signal the {@link JobExecution} to stop. Iterates through the associated |
| 176 | * {@link StepExecution}s, calling {@link StepExecution#setTerminateOnly()}. |
| 177 | * |
| 178 | */ |
| 179 | public void stop() { |
| 180 | for (Iterator it = stepExecutions.iterator(); it.hasNext();) { |
| 181 | StepExecution stepExecution = (StepExecution) it.next(); |
| 182 | stepExecution.setTerminateOnly(); |
| 183 | } |
| 184 | status = BatchStatus.STOPPING; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Sets the {@link ExecutionContext} for this execution |
| 189 | * |
| 190 | * @param executionContext the context |
| 191 | */ |
| 192 | public void setExecutionContext(ExecutionContext executionContext) { |
| 193 | this.executionContext = executionContext; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Returns the {@link ExecutionContext} for this execution |
| 198 | * |
| 199 | * @return the context |
| 200 | */ |
| 201 | public ExecutionContext getExecutionContext() { |
| 202 | return executionContext; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @return the time when this execution was created. |
| 207 | */ |
| 208 | public Date getCreateTime() { |
| 209 | return createTime; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @param createTime creation time of this execution. |
| 214 | */ |
| 215 | public void setCreateTime(Date createTime) { |
| 216 | this.createTime = createTime; |
| 217 | } |
| 218 | } |