Skip to content

Commit 61f6926

Browse files
committed
Updated Task to support LocalDateTime from Spring Batch
1 parent 6d52da0 commit 61f6926

File tree

8 files changed

+43
-27
lines changed

8 files changed

+43
-27
lines changed

spring-cloud-task-samples/batch-events/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@
8181
<groupId>org.hsqldb</groupId>
8282
<artifactId>hsqldb</artifactId>
8383
</dependency>
84+
<dependency>
85+
<groupId>com.fasterxml.jackson.datatype</groupId>
86+
<artifactId>jackson-datatype-jsr310</artifactId>
87+
</dependency>
8488
<dependency>
8589
<groupId>org.springframework.cloud</groupId>
8690
<artifactId>spring-cloud-stream</artifactId>

spring-cloud-task-samples/batch-events/src/test/java/io/spring/cloud/BatchEventsApplicationTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121
import java.util.UUID;
2222

23+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
2324
import org.junit.jupiter.api.Tag;
2425
import com.fasterxml.jackson.databind.DeserializationFeature;
2526
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -54,6 +55,7 @@ public class BatchEventsApplicationTests {
5455
@BeforeEach
5556
public void setup() {
5657
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
58+
objectMapper.registerModule(new JavaTimeModule());
5759
}
5860

5961
@AfterEach

spring-cloud-task-stream/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,11 @@
102102
<artifactId>assertj-core</artifactId>
103103
<scope>test</scope>
104104
</dependency>
105+
<dependency>
106+
<groupId>com.fasterxml.jackson.datatype</groupId>
107+
<artifactId>jackson-datatype-jsr310</artifactId>
108+
<scope>test</scope>
109+
<optional>true</optional>
110+
</dependency>
105111
</dependencies>
106112
</project>

spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/batch/listener/support/JobExecutionEvent.java

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

1717
package org.springframework.cloud.task.batch.listener.support;
1818

19+
import java.time.LocalDateTime;
1920
import java.util.ArrayList;
2021
import java.util.Collection;
2122
import java.util.Collections;
22-
import java.util.Date;
2323
import java.util.HashSet;
2424
import java.util.List;
2525
import java.util.Set;
@@ -49,13 +49,13 @@ public class JobExecutionEvent extends Entity {
4949

5050
private BatchStatus status = BatchStatus.STARTING;
5151

52-
private Date startTime = null;
52+
private LocalDateTime startTime = null;
5353

54-
private Date createTime = new Date(System.currentTimeMillis());
54+
private LocalDateTime createTime = LocalDateTime.now();
5555

56-
private Date endTime = null;
56+
private LocalDateTime endTime = null;
5757

58-
private Date lastUpdated = null;
58+
private LocalDateTime lastUpdated = null;
5959

6060
private ExitStatus exitStatus = new ExitStatus(new org.springframework.batch.core.ExitStatus("UNKNOWN"));
6161

@@ -94,19 +94,19 @@ public JobParametersEvent getJobParameters() {
9494
return this.jobParameters;
9595
}
9696

97-
public Date getEndTime() {
97+
public LocalDateTime getEndTime() {
9898
return this.endTime;
9999
}
100100

101-
public void setEndTime(Date endTime) {
101+
public void setEndTime(LocalDateTime endTime) {
102102
this.endTime = endTime;
103103
}
104104

105-
public Date getStartTime() {
105+
public LocalDateTime getStartTime() {
106106
return this.startTime;
107107
}
108108

109-
public void setStartTime(Date startTime) {
109+
public void setStartTime(LocalDateTime startTime) {
110110
this.startTime = startTime;
111111
}
112112

@@ -197,14 +197,14 @@ public void setExecutionContext(ExecutionContext executionContext) {
197197
/**
198198
* @return the time when this execution was created.
199199
*/
200-
public Date getCreateTime() {
200+
public LocalDateTime getCreateTime() {
201201
return this.createTime;
202202
}
203203

204204
/**
205205
* @param createTime creation time of this execution.
206206
*/
207-
public void setCreateTime(Date createTime) {
207+
public void setCreateTime(LocalDateTime createTime) {
208208
this.createTime = createTime;
209209
}
210210

@@ -213,15 +213,15 @@ public void setCreateTime(Date createTime) {
213213
* JobRepository.
214214
* @return Date representing the last time this JobExecution was updated.
215215
*/
216-
public Date getLastUpdated() {
216+
public LocalDateTime getLastUpdated() {
217217
return this.lastUpdated;
218218
}
219219

220220
/**
221221
* Set the last time this {@link JobExecution} was updated.
222222
* @param lastUpdated The date the {@link JobExecution} was updated.
223223
*/
224-
public void setLastUpdated(Date lastUpdated) {
224+
public void setLastUpdated(LocalDateTime lastUpdated) {
225225
this.lastUpdated = lastUpdated;
226226
}
227227

spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/batch/listener/support/StepExecutionEvent.java

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

1717
package org.springframework.cloud.task.batch.listener.support;
1818

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

@@ -55,11 +55,11 @@ public class StepExecutionEvent extends Entity {
5555

5656
private long writeSkipCount = 0;
5757

58-
private Date startTime = new Date(System.currentTimeMillis());
58+
private LocalDateTime startTime = LocalDateTime.now();
5959

60-
private Date endTime = null;
60+
private LocalDateTime endTime = null;
6161

62-
private Date lastUpdated = null;
62+
private LocalDateTime lastUpdated = null;
6363

6464
private ExecutionContext executionContext = new ExecutionContext();
6565

@@ -147,15 +147,15 @@ public void setCommitCount(int commitCount) {
147147
* Returns the time that this execution ended.
148148
* @return the time that this execution ended
149149
*/
150-
public Date getEndTime() {
150+
public LocalDateTime getEndTime() {
151151
return this.endTime;
152152
}
153153

154154
/**
155155
* Sets the time that this execution ended.
156156
* @param endTime the time that this execution ended
157157
*/
158-
public void setEndTime(Date endTime) {
158+
public void setEndTime(LocalDateTime endTime) {
159159
this.endTime = endTime;
160160
}
161161

@@ -227,15 +227,15 @@ public void setFilterCount(int filterCount) {
227227
* Gets the time this execution started.
228228
* @return the time this execution started
229229
*/
230-
public Date getStartTime() {
230+
public LocalDateTime getStartTime() {
231231
return this.startTime;
232232
}
233233

234234
/**
235235
* Sets the time this execution started.
236236
* @param startTime the time this execution started
237237
*/
238-
public void setStartTime(Date startTime) {
238+
public void setStartTime(LocalDateTime startTime) {
239239
this.startTime = startTime;
240240
}
241241

@@ -357,15 +357,15 @@ public void setProcessSkipCount(int processSkipCount) {
357357
/**
358358
* @return the Date representing the last time this execution was persisted.
359359
*/
360-
public Date getLastUpdated() {
360+
public LocalDateTime getLastUpdated() {
361361
return this.lastUpdated;
362362
}
363363

364364
/**
365365
* Set the time when the StepExecution was last updated before persisting.
366-
* @param lastUpdated the {@link Date} the StepExecution was last updated.
366+
* @param lastUpdated the {@link LocalDateTime} the StepExecution was last updated.
367367
*/
368-
public void setLastUpdated(Date lastUpdated) {
368+
public void setLastUpdated(LocalDateTime lastUpdated) {
369369
this.lastUpdated = lastUpdated;
370370
}
371371

spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/EventListenerTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import com.fasterxml.jackson.databind.DeserializationFeature;
2525
import com.fasterxml.jackson.databind.ObjectMapper;
26+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
2627
import org.junit.jupiter.api.AfterEach;
2728
import org.junit.jupiter.api.BeforeEach;
2829
import org.junit.jupiter.api.Test;
@@ -78,6 +79,8 @@ public class EventListenerTests {
7879

7980
@BeforeEach
8081
public void beforeTests() {
82+
objectMapper.registerModule(new JavaTimeModule());
83+
8184
this.applicationContext = new SpringApplicationBuilder()
8285
.sources(TestChannelBinderConfiguration.getCompleteConfiguration(BatchEventsApplication.class))
8386
.web(WebApplicationType.NONE).build().run();

spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/JobExecutionEventTests.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.listener;
1818

19+
import java.time.LocalDateTime;
1920
import java.util.ArrayList;
2021
import java.util.Date;
2122
import java.util.Iterator;
@@ -217,7 +218,7 @@ public void testToString() {
217218

218219
@Test
219220
public void testGetterSetters() {
220-
Date date = new Date();
221+
LocalDateTime date = LocalDateTime.now();
221222
JobExecutionEvent jobExecutionEvent = new JobExecutionEvent();
222223
jobExecutionEvent.setLastUpdated(date);
223224
assertThat(jobExecutionEvent.getLastUpdated()).isEqualTo(date);

spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/StepExecutionEventTests.java

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

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

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

2121
import org.junit.jupiter.api.Test;
2222

@@ -124,7 +124,7 @@ public void testEquals() {
124124
@Test
125125
public void testSettersGetters() {
126126
StepExecutionEvent stepExecutionEvent = new StepExecutionEvent(getBasicStepExecution());
127-
Date date = new Date();
127+
LocalDateTime date = LocalDateTime.now();
128128
stepExecutionEvent.setLastUpdated(date);
129129
assertThat(stepExecutionEvent.getLastUpdated()).isEqualTo(date);
130130

0 commit comments

Comments
 (0)