[BAEL-3488] Timer Class (#8769)

* [BAEL-3488] Moving code to core-java where the original article code was located

* [BAEL-3488] Moved original code under timer package
This commit is contained in:
François Dupire
2020-02-23 17:53:12 +01:00
committed by GitHub
parent 6cf0732ffd
commit 9a838423fb
5 changed files with 1 additions and 1 deletions

View File

@@ -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);
}
}

View File

@@ -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()));
}
}

View File

@@ -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);
}
}

View File

@@ -1,4 +1,4 @@
package com.baeldung;
package com.baeldung.timer;
import org.junit.Test;
import org.slf4j.Logger;

View File

@@ -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);
}
}
}