dupirefr/dupire.francois+pro@gmail.com [BAEL-3488] Timer Class (#8468)
* [BAEL-3488] Added examples and tests * [BAEL-3488] Moved code to core-java-date-operations-2 module * [BAEL-3488] Added assertj library to maven dependencies
This commit is contained in:
parent
31889765c3
commit
9136e9a6fb
@ -31,11 +31,18 @@
|
|||||||
<artifactId>hirondelle-date4j</artifactId>
|
<artifactId>hirondelle-date4j</artifactId>
|
||||||
<version>${hirondelle-date4j.version}</version>
|
<version>${hirondelle-date4j.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<joda-time.version>2.10</joda-time.version>
|
<joda-time.version>2.10</joda-time.version>
|
||||||
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
|
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
|
||||||
|
<assertj.version>3.14.0</assertj.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
public class DatabaseMigrationTask extends TimerTask {
|
||||||
|
private List<String> oldDatabase;
|
||||||
|
private List<String> newDatabase;
|
||||||
|
|
||||||
|
public DatabaseMigrationTask(List<String> oldDatabase, List<String> newDatabase) {
|
||||||
|
this.oldDatabase = oldDatabase;
|
||||||
|
this.newDatabase = newDatabase;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
newDatabase.addAll(oldDatabase);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
public class NewsletterTask extends TimerTask {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
System.out.println("Email sent at: "
|
||||||
|
+ LocalDateTime.ofInstant(Instant.ofEpochMilli(scheduledExecutionTime()), ZoneId.systemDefault()));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
class DatabaseMigrationTaskUnitTest {
|
||||||
|
@Test
|
||||||
|
void givenDatabaseMigrationTask_whenTimerScheduledForNowPlusTwoSeconds_thenDataMigratedAfterTwoSeconds() throws Exception {
|
||||||
|
List<String> oldDatabase = Arrays.asList("Harrison Ford", "Carrie Fisher", "Mark Hamill");
|
||||||
|
List<String> newDatabase = new ArrayList<>();
|
||||||
|
|
||||||
|
LocalDateTime twoSecondsLater = LocalDateTime.now().plusSeconds(2);
|
||||||
|
Date twoSecondsLaterAsDate = Date.from(twoSecondsLater.atZone(ZoneId.systemDefault()).toInstant());
|
||||||
|
|
||||||
|
new Timer().schedule(new DatabaseMigrationTask(oldDatabase, newDatabase), twoSecondsLaterAsDate);
|
||||||
|
|
||||||
|
while (LocalDateTime.now().isBefore(twoSecondsLater)) {
|
||||||
|
assertThat(newDatabase).isEmpty();
|
||||||
|
Thread.sleep(500);
|
||||||
|
}
|
||||||
|
assertThat(newDatabase).containsExactlyElementsOf(oldDatabase);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenDatabaseMigrationTask_whenTimerScheduledInTwoSeconds_thenDataMigratedAfterTwoSeconds() throws Exception {
|
||||||
|
List<String> oldDatabase = Arrays.asList("Harrison Ford", "Carrie Fisher", "Mark Hamill");
|
||||||
|
List<String> newDatabase = new ArrayList<>();
|
||||||
|
|
||||||
|
new Timer().schedule(new DatabaseMigrationTask(oldDatabase, newDatabase), 2000);
|
||||||
|
|
||||||
|
LocalDateTime twoSecondsLater = LocalDateTime.now().plusSeconds(2);
|
||||||
|
|
||||||
|
while (LocalDateTime.now().isBefore(twoSecondsLater)) {
|
||||||
|
assertThat(newDatabase).isEmpty();
|
||||||
|
Thread.sleep(500);
|
||||||
|
}
|
||||||
|
assertThat(newDatabase).containsExactlyElementsOf(oldDatabase);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Timer;
|
||||||
|
|
||||||
|
class NewsletterTaskUnitTest {
|
||||||
|
private final Timer timer = new Timer();
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void afterEach() {
|
||||||
|
timer.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNewsletterTask_whenTimerScheduledEachSecondFixedDelay_thenNewsletterSentEachSecond() throws Exception {
|
||||||
|
timer.schedule(new NewsletterTask(), 0, 1000);
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNewsletterTask_whenTimerScheduledEachSecondFixedRate_thenNewsletterSentEachSecond() throws Exception {
|
||||||
|
timer.scheduleAtFixedRate(new NewsletterTask(), 0, 1000);
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user