Skip to content

Commit a0fda4b

Browse files
committed
Set Spring Cloud Task Date Strategy to match Batch
resolves #868 Signed-off-by: Glenn Renfro <[email protected]> Updated the timestamps for H2,oracle, db2, hsqldb to be 9 digits for improved accuracy
1 parent cf80568 commit a0fda4b

File tree

30 files changed

+247
-251
lines changed

30 files changed

+247
-251
lines changed

spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/handler/TaskJobLauncherApplicationRunner.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.Collections;
22-
import java.util.Date;
2322
import java.util.HashMap;
2423
import java.util.List;
2524
import java.util.Map;
@@ -156,8 +155,6 @@ protected void execute(Job job, JobParameters jobParameters) throws JobExecution
156155
private void monitorJobExecutions() {
157156
RepeatTemplate template = new RepeatTemplate();
158157

159-
Date startDate = new Date();
160-
161158
template.iterate(context -> {
162159

163160
List<JobExecution> failedJobExecutions = new ArrayList<>();

spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/partition/DeployerPartitionHandlerTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.cloud.task.batch.partition;
1818

19+
import java.time.LocalDateTime;
1920
import java.util.ArrayList;
2021
import java.util.Collection;
2122
import java.util.Collections;
@@ -91,7 +92,7 @@ public class DeployerPartitionHandlerTests {
9192
public void setUp() {
9293
MockitoAnnotations.openMocks(this);
9394
this.environment = new MockEnvironment();
94-
TaskExecution taskExecution = new TaskExecution(2, 0, "name", new Date(), new Date(), "",
95+
TaskExecution taskExecution = new TaskExecution(2, 0, "name", LocalDateTime.now(), LocalDateTime.now(), "",
9596
Collections.emptyList(), null, null, null);
9697
Mockito.lenient().when(taskRepository.createTaskExecution()).thenReturn(taskExecution);
9798
}

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/TaskLifecycleListener.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import java.io.PrintWriter;
2020
import java.io.StringWriter;
2121
import java.lang.reflect.InvocationTargetException;
22+
import java.time.LocalDateTime;
2223
import java.util.ArrayList;
2324
import java.util.Arrays;
2425
import java.util.Collection;
2526
import java.util.Collections;
26-
import java.util.Date;
2727
import java.util.List;
2828

2929
import io.micrometer.observation.ObservationConvention;
@@ -191,7 +191,7 @@ private String stackTraceToString(Throwable exception) {
191191

192192
private void doTaskEnd() {
193193
if ((this.listenerFailed || this.started) && !this.finished) {
194-
this.taskExecution.setEndTime(new Date());
194+
this.taskExecution.setEndTime(LocalDateTime.now());
195195

196196
if (this.applicationFailedException != null) {
197197
this.taskExecution.setErrorMessage(stackTraceToString(this.applicationFailedException));
@@ -277,15 +277,16 @@ private void doTaskStart() {
277277
Assert.isNull(taskExecution.getEndTime(),
278278
String.format("Invalid TaskExecution, ID %s task is already complete",
279279
this.taskProperties.getExecutionid()));
280-
Date startDate = (taskExecution.getStartTime() == null) ? new Date() : taskExecution.getStartTime();
280+
LocalDateTime startDate = (taskExecution.getStartTime() == null) ? LocalDateTime.now()
281+
: taskExecution.getStartTime();
281282
this.taskExecution = this.taskRepository.startTaskExecution(this.taskProperties.getExecutionid(),
282283
this.taskNameResolver.getTaskName(), startDate, args,
283284
this.taskProperties.getExternalExecutionId(), this.taskProperties.getParentExecutionId());
284285
}
285286
else {
286287
TaskExecution taskExecution = new TaskExecution();
287288
taskExecution.setTaskName(this.taskNameResolver.getTaskName());
288-
taskExecution.setStartTime(new Date());
289+
taskExecution.setStartTime(LocalDateTime.now());
289290
taskExecution.setArguments(args);
290291
taskExecution.setExternalExecutionId(this.taskProperties.getExternalExecutionId());
291292
taskExecution.setParentExecutionId(this.taskProperties.getParentExecutionId());
@@ -384,8 +385,8 @@ private TaskExecution invokeOnTaskError(TaskExecution taskExecution, Throwable t
384385
}
385386

386387
private TaskExecution getTaskExecutionCopy(TaskExecution taskExecution) {
387-
Date startTime = new Date(taskExecution.getStartTime().getTime());
388-
Date endTime = (taskExecution.getEndTime() == null) ? null : new Date(taskExecution.getEndTime().getTime());
388+
LocalDateTime startTime = taskExecution.getStartTime();
389+
LocalDateTime endTime = taskExecution.getEndTime();
389390

390391
return new TaskExecution(taskExecution.getExecutionId(), taskExecution.getExitCode(),
391392
taskExecution.getTaskName(), startTime, endTime, taskExecution.getExitMessage(),

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/TaskExecution.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.cloud.task.repository;
1818

19+
import java.time.LocalDateTime;
1920
import java.util.ArrayList;
20-
import java.util.Date;
2121
import java.util.List;
2222

2323
import org.springframework.util.Assert;
@@ -55,12 +55,12 @@ public class TaskExecution {
5555
/**
5656
* Time of when the task was started.
5757
*/
58-
private Date startTime;
58+
private LocalDateTime startTime;
5959

6060
/**
6161
* Timestamp of when the task was completed/terminated.
6262
*/
63-
private Date endTime;
63+
private LocalDateTime endTime;
6464

6565
/**
6666
* Message returned from the task or stacktrace.
@@ -90,25 +90,26 @@ public TaskExecution() {
9090
this.arguments = new ArrayList<>();
9191
}
9292

93-
public TaskExecution(long executionId, Integer exitCode, String taskName, Date startTime, Date endTime,
94-
String exitMessage, List<String> arguments, String errorMessage, String externalExecutionId,
95-
Long parentExecutionId) {
93+
public TaskExecution(long executionId, Integer exitCode, String taskName, LocalDateTime startTime,
94+
LocalDateTime endTime, String exitMessage, List<String> arguments, String errorMessage,
95+
String externalExecutionId, Long parentExecutionId) {
9696

9797
Assert.notNull(arguments, "arguments must not be null");
9898
this.executionId = executionId;
9999
this.exitCode = exitCode;
100100
this.taskName = taskName;
101101
this.exitMessage = exitMessage;
102102
this.arguments = new ArrayList<>(arguments);
103-
this.startTime = (startTime != null) ? (Date) startTime.clone() : null;
104-
this.endTime = (endTime != null) ? (Date) endTime.clone() : null;
103+
this.startTime = startTime;
104+
this.endTime = endTime;
105105
this.errorMessage = errorMessage;
106106
this.externalExecutionId = externalExecutionId;
107107
this.parentExecutionId = parentExecutionId;
108108
}
109109

110-
public TaskExecution(long executionId, Integer exitCode, String taskName, Date startTime, Date endTime,
111-
String exitMessage, List<String> arguments, String errorMessage, String externalExecutionId) {
110+
public TaskExecution(long executionId, Integer exitCode, String taskName, LocalDateTime startTime,
111+
LocalDateTime endTime, String exitMessage, List<String> arguments, String errorMessage,
112+
String externalExecutionId) {
112113

113114
this(executionId, exitCode, taskName, startTime, endTime, exitMessage, arguments, errorMessage,
114115
externalExecutionId, null);
@@ -134,20 +135,20 @@ public void setTaskName(String taskName) {
134135
this.taskName = taskName;
135136
}
136137

137-
public Date getStartTime() {
138-
return (this.startTime != null) ? (Date) this.startTime.clone() : null;
138+
public LocalDateTime getStartTime() {
139+
return this.startTime;
139140
}
140141

141-
public void setStartTime(Date startTime) {
142-
this.startTime = (startTime != null) ? (Date) startTime.clone() : null;
142+
public void setStartTime(LocalDateTime startTime) {
143+
this.startTime = startTime;
143144
}
144145

145-
public Date getEndTime() {
146-
return (this.endTime != null) ? (Date) this.endTime.clone() : null;
146+
public LocalDateTime getEndTime() {
147+
return this.endTime;
147148
}
148149

149-
public void setEndTime(Date endTime) {
150-
this.endTime = (endTime != null) ? (Date) endTime.clone() : null;
150+
public void setEndTime(LocalDateTime endTime) {
151+
this.endTime = endTime;
151152
}
152153

153154
public String getExitMessage() {

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/TaskRepository.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.cloud.task.repository;
1818

19-
import java.util.Date;
19+
import java.time.LocalDateTime;
2020
import java.util.List;
2121

2222
import org.springframework.transaction.annotation.Transactional;
@@ -40,7 +40,7 @@ public interface TaskRepository {
4040
* @return the updated {@link TaskExecution}
4141
*/
4242
@Transactional("${spring.cloud.task.transaction-manager:springCloudTaskTransactionManager}")
43-
TaskExecution completeTaskExecution(long executionId, Integer exitCode, Date endTime, String exitMessage);
43+
TaskExecution completeTaskExecution(long executionId, Integer exitCode, LocalDateTime endTime, String exitMessage);
4444

4545
/**
4646
* Notifies the repository that a taskExecution has completed.
@@ -53,7 +53,7 @@ public interface TaskRepository {
5353
* @since 1.1.0
5454
*/
5555
@Transactional("${spring.cloud.task.transaction-manager:springCloudTaskTransactionManager}")
56-
TaskExecution completeTaskExecution(long executionId, Integer exitCode, Date endTime, String exitMessage,
56+
TaskExecution completeTaskExecution(long executionId, Integer exitCode, LocalDateTime endTime, String exitMessage,
5757
String errorMessage);
5858

5959
/**
@@ -99,7 +99,7 @@ TaskExecution completeTaskExecution(long executionId, Integer exitCode, Date end
9999
* @return TaskExecution created based on the parameters.
100100
*/
101101
@Transactional("${spring.cloud.task.transaction-manager:springCloudTaskTransactionManager}")
102-
TaskExecution startTaskExecution(long executionid, String taskName, Date startTime, List<String> arguments,
102+
TaskExecution startTaskExecution(long executionid, String taskName, LocalDateTime startTime, List<String> arguments,
103103
String externalExecutionId);
104104

105105
/**
@@ -122,7 +122,7 @@ TaskExecution startTaskExecution(long executionid, String taskName, Date startTi
122122
* a TaskExecution.
123123
*/
124124
@Transactional("${spring.cloud.task.transaction-manager:springCloudTaskTransactionManager}")
125-
TaskExecution startTaskExecution(long executionid, String taskName, Date startTime, List<String> arguments,
125+
TaskExecution startTaskExecution(long executionid, String taskName, LocalDateTime startTime, List<String> arguments,
126126
String externalExecutionId, Long parentExecutionId);
127127

128128
}

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/JdbcTaskExecutionDao.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818

1919
import java.sql.ResultSet;
2020
import java.sql.SQLException;
21+
import java.sql.Timestamp;
2122
import java.sql.Types;
23+
import java.time.LocalDateTime;
2224
import java.util.ArrayList;
2325
import java.util.Collections;
24-
import java.util.Date;
2526
import java.util.HashSet;
2627
import java.util.LinkedHashMap;
2728
import java.util.List;
@@ -199,13 +200,13 @@ public JdbcTaskExecutionDao(DataSource dataSource) {
199200
}
200201

201202
@Override
202-
public TaskExecution createTaskExecution(String taskName, Date startTime, List<String> arguments,
203+
public TaskExecution createTaskExecution(String taskName, LocalDateTime startTime, List<String> arguments,
203204
String externalExecutionId) {
204205
return createTaskExecution(taskName, startTime, arguments, externalExecutionId, null);
205206
}
206207

207208
@Override
208-
public TaskExecution createTaskExecution(String taskName, Date startTime, List<String> arguments,
209+
public TaskExecution createTaskExecution(String taskName, LocalDateTime startTime, List<String> arguments,
209210
String externalExecutionId, Long parentExecutionId) {
210211
long nextExecutionId = getNextExecutionId();
211212

@@ -214,8 +215,9 @@ public TaskExecution createTaskExecution(String taskName, Date startTime, List<S
214215

215216
final MapSqlParameterSource queryParameters = new MapSqlParameterSource()
216217
.addValue("taskExecutionId", nextExecutionId, Types.BIGINT).addValue("exitCode", null, Types.INTEGER)
217-
.addValue("startTime", startTime, Types.TIMESTAMP).addValue("taskName", taskName, Types.VARCHAR)
218-
.addValue("lastUpdated", new Date(), Types.TIMESTAMP)
218+
.addValue("startTime", startTime == null ? null : Timestamp.valueOf(startTime), Types.TIMESTAMP)
219+
.addValue("taskName", taskName, Types.VARCHAR)
220+
.addValue("lastUpdated", Timestamp.valueOf(LocalDateTime.now()), Types.TIMESTAMP)
219221
.addValue("externalExecutionId", externalExecutionId, Types.VARCHAR)
220222
.addValue("parentExecutionId", parentExecutionId, Types.BIGINT);
221223

@@ -225,20 +227,21 @@ public TaskExecution createTaskExecution(String taskName, Date startTime, List<S
225227
}
226228

227229
@Override
228-
public TaskExecution startTaskExecution(long executionId, String taskName, Date startTime, List<String> arguments,
229-
String externalExecutionId) {
230+
public TaskExecution startTaskExecution(long executionId, String taskName, LocalDateTime startTime,
231+
List<String> arguments, String externalExecutionId) {
230232
return startTaskExecution(executionId, taskName, startTime, arguments, externalExecutionId, null);
231233
}
232234

233235
@Override
234-
public TaskExecution startTaskExecution(long executionId, String taskName, Date startTime, List<String> arguments,
235-
String externalExecutionId, Long parentExecutionId) {
236+
public TaskExecution startTaskExecution(long executionId, String taskName, LocalDateTime startTime,
237+
List<String> arguments, String externalExecutionId, Long parentExecutionId) {
236238
TaskExecution taskExecution = new TaskExecution(executionId, null, taskName, startTime, null, null, arguments,
237239
null, externalExecutionId, parentExecutionId);
238240

239241
final MapSqlParameterSource queryParameters = new MapSqlParameterSource()
240-
.addValue("startTime", startTime, Types.TIMESTAMP).addValue("exitCode", null, Types.INTEGER)
241-
.addValue("taskName", taskName, Types.VARCHAR).addValue("lastUpdated", new Date(), Types.TIMESTAMP)
242+
.addValue("startTime", startTime == null ? null : Timestamp.valueOf(startTime), Types.TIMESTAMP)
243+
.addValue("exitCode", null, Types.INTEGER).addValue("taskName", taskName, Types.VARCHAR)
244+
.addValue("lastUpdated", Timestamp.valueOf(LocalDateTime.now()), Types.TIMESTAMP)
242245
.addValue("parentExecutionId", parentExecutionId, Types.BIGINT)
243246
.addValue("taskExecutionId", executionId, Types.BIGINT);
244247

@@ -258,7 +261,7 @@ public TaskExecution startTaskExecution(long executionId, String taskName, Date
258261
}
259262

260263
@Override
261-
public void completeTaskExecution(long taskExecutionId, Integer exitCode, Date endTime, String exitMessage,
264+
public void completeTaskExecution(long taskExecutionId, Integer exitCode, LocalDateTime endTime, String exitMessage,
262265
String errorMessage) {
263266
final MapSqlParameterSource queryParameters = new MapSqlParameterSource().addValue("taskExecutionId",
264267
taskExecutionId, Types.BIGINT);
@@ -271,17 +274,18 @@ public void completeTaskExecution(long taskExecutionId, Integer exitCode, Date e
271274
}
272275

273276
final MapSqlParameterSource parameters = new MapSqlParameterSource()
274-
.addValue("endTime", endTime, Types.TIMESTAMP).addValue("exitCode", exitCode, Types.INTEGER)
275-
.addValue("exitMessage", exitMessage, Types.VARCHAR)
277+
.addValue("endTime", endTime == null ? null : Timestamp.valueOf(endTime), Types.TIMESTAMP)
278+
.addValue("exitCode", exitCode, Types.INTEGER).addValue("exitMessage", exitMessage, Types.VARCHAR)
276279
.addValue("errorMessage", errorMessage, Types.VARCHAR)
277-
.addValue("lastUpdated", new Date(), Types.TIMESTAMP)
280+
.addValue("lastUpdated", Timestamp.valueOf(LocalDateTime.now()), Types.TIMESTAMP)
278281
.addValue("taskExecutionId", taskExecutionId, Types.BIGINT);
279282

280283
this.jdbcTemplate.update(getQuery(UPDATE_TASK_EXECUTION), parameters);
281284
}
282285

283286
@Override
284-
public void completeTaskExecution(long taskExecutionId, Integer exitCode, Date endTime, String exitMessage) {
287+
public void completeTaskExecution(long taskExecutionId, Integer exitCode, LocalDateTime endTime,
288+
String exitMessage) {
285289
completeTaskExecution(taskExecutionId, exitCode, endTime, exitMessage, null);
286290
}
287291

@@ -582,9 +586,9 @@ public TaskExecution mapRow(ResultSet rs, int rowNum) throws SQLException {
582586
parentExecutionId = null;
583587
}
584588
return new TaskExecution(id, getNullableExitCode(rs), rs.getString("TASK_NAME"),
585-
rs.getTimestamp("START_TIME"), rs.getTimestamp("END_TIME"), rs.getString("EXIT_MESSAGE"),
586-
getTaskArguments(id), rs.getString("ERROR_MESSAGE"), rs.getString("EXTERNAL_EXECUTION_ID"),
587-
parentExecutionId);
589+
rs.getObject("START_TIME", LocalDateTime.class), rs.getObject("END_TIME", LocalDateTime.class),
590+
rs.getString("EXIT_MESSAGE"), getTaskArguments(id), rs.getString("ERROR_MESSAGE"),
591+
rs.getString("EXTERNAL_EXECUTION_ID"), parentExecutionId);
588592
}
589593

590594
private Integer getNullableExitCode(ResultSet rs) throws SQLException {

0 commit comments

Comments
 (0)