forked from spring-projects/spring-batch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StepExecution.java
581 lines (503 loc) · 14.6 KB
/
StepExecution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Batch domain object representation the execution of a step. Unlike
* {@link JobExecution}, there are additional properties related the processing
* of items such as commit count, etc.
*
* @author Lucas Ward
* @author Dave Syer
* @author Mahmoud Ben Hassine
* @author Taeik Lim
*
*/
@SuppressWarnings("serial")
public class StepExecution extends Entity {
private final JobExecution jobExecution;
private final String stepName;
private volatile BatchStatus status = BatchStatus.STARTING;
private volatile long readCount = 0;
private volatile long writeCount = 0;
private volatile long commitCount = 0;
private volatile long rollbackCount = 0;
private volatile long readSkipCount = 0;
private volatile long processSkipCount = 0;
private volatile long writeSkipCount = 0;
private volatile Date startTime = null;
private volatile Date createTime = new Date(System.currentTimeMillis());
private volatile Date endTime = null;
private volatile Date lastUpdated = null;
private volatile ExecutionContext executionContext = new ExecutionContext();
private volatile ExitStatus exitStatus = ExitStatus.EXECUTING;
private volatile boolean terminateOnly;
private volatile long filterCount;
private transient volatile List<Throwable> failureExceptions = new CopyOnWriteArrayList<>();
/**
* Constructor with mandatory properties.
*
* @param stepName the step to which this execution belongs
* @param jobExecution the current job execution
* @param id the id of this execution
*/
public StepExecution(String stepName, JobExecution jobExecution, Long id) {
this(stepName, jobExecution);
Assert.notNull(jobExecution, "JobExecution must be provided to re-hydrate an existing StepExecution");
Assert.notNull(id, "The entity Id must be provided to re-hydrate an existing StepExecution");
setId(id);
jobExecution.addStepExecution(this);
}
/**
* Constructor that substitutes in null for the execution id
*
* @param stepName the step to which this execution belongs
* @param jobExecution the current job execution
*/
public StepExecution(String stepName, JobExecution jobExecution) {
super();
Assert.hasLength(stepName, "A stepName is required");
this.stepName = stepName;
this.jobExecution = jobExecution;
}
/**
* Constructor that requires only a stepName. Intended only to be
* used via serialization libraries to address the circular
* reference between {@link JobExecution} and StepExecution.
*
* @param stepName the name of the executed step
*/
@SuppressWarnings("unused")
private StepExecution(String stepName) {
super();
Assert.hasLength(stepName, "A stepName is required");
this.stepName = stepName;
this.jobExecution = null;
}
/**
* Returns the {@link ExecutionContext} for this execution
*
* @return the attributes
*/
public ExecutionContext getExecutionContext() {
return executionContext;
}
/**
* Sets the {@link ExecutionContext} for this execution
*
* @param executionContext the attributes
*/
public void setExecutionContext(ExecutionContext executionContext) {
this.executionContext = executionContext;
}
/**
* Returns the current number of commits for this execution
*
* @return the current number of commits
*/
public long getCommitCount() {
return commitCount;
}
/**
* Sets the current number of commits for this execution
*
* @param commitCount the current number of commits
*/
public void setCommitCount(long commitCount) {
this.commitCount = commitCount;
}
/**
* Returns the time that this execution ended or {@code null} if the step is running.
*
* @return the time that this execution ended or {@code null} if the step is running
*/
@Nullable
public Date getEndTime() {
return endTime;
}
/**
* Sets the time that this execution ended
*
* @param endTime the time that this execution ended
*/
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
/**
* Returns the current number of items read for this execution
*
* @return the current number of items read for this execution
*/
public long getReadCount() {
return readCount;
}
/**
* Sets the current number of read items for this execution
*
* @param readCount the current number of read items for this execution
*/
public void setReadCount(long readCount) {
this.readCount = readCount;
}
/**
* Returns the current number of items written for this execution
*
* @return the current number of items written for this execution
*/
public long getWriteCount() {
return writeCount;
}
/**
* Sets the current number of written items for this execution
*
* @param writeCount the current number of written items for this execution
*/
public void setWriteCount(long writeCount) {
this.writeCount = writeCount;
}
/**
* Returns the current number of rollbacks for this execution
*
* @return the current number of rollbacks for this execution
*/
public long getRollbackCount() {
return rollbackCount;
}
/**
* Returns the current number of items filtered out of this execution
*
* @return the current number of items filtered out of this execution
*/
public long getFilterCount() {
return filterCount;
}
/**
* Public setter for the number of items filtered out of this execution.
*
* @param filterCount the number of items filtered out of this execution to
* set
*/
public void setFilterCount(long filterCount) {
this.filterCount = filterCount;
}
/**
* Setter for number of rollbacks for this execution
*
* @param rollbackCount long the number of rollbacks.
*/
public void setRollbackCount(long rollbackCount) {
this.rollbackCount = rollbackCount;
}
/**
* Gets the time this execution was created
*
* @return the time when this execution was created.
*/
public Date getCreateTime() {
return createTime;
}
/**
* Sets the time this execution was created
*
* @param createTime creation time of this execution.
*/
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
/**
* Gets the time this execution started
*
* @return the time this execution started
*/
public Date getStartTime() {
return startTime;
}
/**
* Sets the time this execution started
*
* @param startTime the time this execution started
*/
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
/**
* Returns the current status of this step
*
* @return the current status of this step
*/
public BatchStatus getStatus() {
return status;
}
/**
* Sets the current status of this step
*
* @param status the current status of this step
*/
public void setStatus(BatchStatus status) {
this.status = status;
}
/**
* Upgrade the status field if the provided value is greater than the
* existing one. Clients using this method to set the status can be sure
* that they don't overwrite a failed status with an successful one.
*
* @param status the new status value
*/
public void upgradeStatus(BatchStatus status) {
this.status = this.status.upgradeTo(status);
}
/**
* @return the name of the step
*/
public String getStepName() {
return stepName;
}
/**
* Accessor for the job execution id.
*
* @return the jobExecutionId
*/
public Long getJobExecutionId() {
if (jobExecution != null) {
return jobExecution.getId();
}
return null;
}
/**
* @param exitStatus {@link ExitStatus} instance used to establish the exit status.
*/
public void setExitStatus(ExitStatus exitStatus) {
this.exitStatus = exitStatus;
}
/**
* @return the exitCode
*/
public ExitStatus getExitStatus() {
return exitStatus;
}
/**
* Accessor for the execution context information of the enclosing job.
*
* @return the {@link JobExecution} that was used to start this step
* execution.
*/
public JobExecution getJobExecution() {
return jobExecution;
}
/**
* Factory method for {@link StepContribution}.
*
* @return a new {@link StepContribution}
*/
public StepContribution createStepContribution() {
return new StepContribution(this);
}
/**
* On successful execution just before a chunk commit, this method should be
* called. Synchronizes access to the {@link StepExecution} so that changes
* are atomic.
*
* @param contribution {@link StepContribution} instance used to update the StepExecution state.
*/
public synchronized void apply(StepContribution contribution) {
readSkipCount += contribution.getReadSkipCount();
writeSkipCount += contribution.getWriteSkipCount();
processSkipCount += contribution.getProcessSkipCount();
filterCount += contribution.getFilterCount();
readCount += contribution.getReadCount();
writeCount += contribution.getWriteCount();
exitStatus = exitStatus.and(contribution.getExitStatus());
}
/**
* On unsuccessful execution after a chunk has rolled back.
*/
public synchronized void incrementRollbackCount() {
rollbackCount++;
}
/**
* @return flag to indicate that an execution should halt
*/
public boolean isTerminateOnly() {
return this.terminateOnly;
}
/**
* Set a flag that will signal to an execution environment that this
* execution (and its surrounding job) wishes to exit.
*/
public void setTerminateOnly() {
this.terminateOnly = true;
}
/**
* @return the total number of items skipped.
*/
public long getSkipCount() {
return readSkipCount + processSkipCount + writeSkipCount;
}
/**
* Increment the number of commits
*/
public void incrementCommitCount() {
commitCount++;
}
/**
* Convenience method to get the current job parameters.
*
* @return the {@link JobParameters} from the enclosing job, or empty if
* that is null
*/
public JobParameters getJobParameters() {
if (jobExecution == null) {
return new JobParameters();
}
return jobExecution.getJobParameters();
}
/**
* @return the number of records skipped on read
*/
public long getReadSkipCount() {
return readSkipCount;
}
/**
* @return the number of records skipped on write
*/
public long getWriteSkipCount() {
return writeSkipCount;
}
/**
* Set the number of records skipped on read
*
* @param readSkipCount long containing read skip count to be used for the step execution.
*/
public void setReadSkipCount(long readSkipCount) {
this.readSkipCount = readSkipCount;
}
/**
* Set the number of records skipped on write
*
* @param writeSkipCount long containing write skip count to be used for the step execution.
*/
public void setWriteSkipCount(long writeSkipCount) {
this.writeSkipCount = writeSkipCount;
}
/**
* @return the number of records skipped during processing
*/
public long getProcessSkipCount() {
return processSkipCount;
}
/**
* Set the number of records skipped during processing.
*
* @param processSkipCount long containing process skip count to be used for the step execution.
*/
public void setProcessSkipCount(long processSkipCount) {
this.processSkipCount = processSkipCount;
}
/**
* @return the Date representing the last time this execution was persisted.
*/
public Date getLastUpdated() {
return lastUpdated;
}
/**
* Set the time when the StepExecution was last updated before persisting
*
* @param lastUpdated {@link Date} instance used to establish the last
* updated date for the Step Execution.
*/
public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated;
}
/**
* @return The {@link List} of {@link Throwable} objects.
*/
public List<Throwable> getFailureExceptions() {
return failureExceptions;
}
/**
* Add a {@link Throwable} to failure exceptions.
*
* @param throwable The {@link Throwable} to add to failure exceptions.
*/
public void addFailureException(Throwable throwable) {
this.failureExceptions.add(throwable);
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.batch.container.common.domain.Entity#equals(java.
* lang.Object)
*/
@Override
public boolean equals(Object obj) {
Object jobExecutionId = getJobExecutionId();
if (jobExecutionId == null || !(obj instanceof StepExecution) || getId() == null) {
return super.equals(obj);
}
StepExecution other = (StepExecution) obj;
return stepName.equals(other.getStepName()) && (jobExecutionId.equals(other.getJobExecutionId()))
&& getId().equals(other.getId());
}
/**
* Deserialize and ensure transient fields are re-instantiated when read
* back.
*
* @param stream instance of {@link ObjectInputStream}.
*
* @throws IOException thrown if error occurs during read.
* @throws ClassNotFoundException thrown if class is not found.
*/
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
failureExceptions = new ArrayList<>();
}
/*
* (non-Javadoc)
*
* @see org.springframework.batch.container.common.domain.Entity#hashCode()
*/
@Override
public int hashCode() {
Object jobExecutionId = getJobExecutionId();
Long id = getId();
return super.hashCode() + 31 * (stepName != null ? stepName.hashCode() : 0) + 91
* (jobExecutionId != null ? jobExecutionId.hashCode() : 0) + 59 * (id != null ? id.hashCode() : 0);
}
@Override
public String toString() {
return String.format(getSummary() + ", exitDescription=%s", exitStatus.getExitDescription());
}
/**
* @return The {@link String} containing a summary of the step execution.
*/
public String getSummary() {
return super.toString()
+ String.format(
", name=%s, status=%s, exitStatus=%s, readCount=%d, filterCount=%d, writeCount=%d readSkipCount=%d, writeSkipCount=%d"
+ ", processSkipCount=%d, commitCount=%d, rollbackCount=%d", stepName, status,
exitStatus.getExitCode(), readCount, filterCount, writeCount, readSkipCount, writeSkipCount,
processSkipCount, commitCount, rollbackCount);
}
}