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