From b378f33db6cb91318f79e7f786d1e96bf15bd934 Mon Sep 17 00:00:00 2001 From: Marcus Fihlon Date: Wed, 27 Mar 2024 16:06:18 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20dynamically=20update=20conference?= =?UTF-8?q?=20sessions=20closes=20#14?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcus Fihlon --- .../apus/service/ConferenceService.java | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/main/java/swiss/fihlon/apus/service/ConferenceService.java b/src/main/java/swiss/fihlon/apus/service/ConferenceService.java index af3446e..5eba1e1 100644 --- a/src/main/java/swiss/fihlon/apus/service/ConferenceService.java +++ b/src/main/java/swiss/fihlon/apus/service/ConferenceService.java @@ -17,6 +17,7 @@ */ package swiss.fihlon.apus.service; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import swiss.fihlon.apus.conference.Session; @@ -33,25 +34,43 @@ @Service public final class ConferenceService { - private final List sessions; + private List sessions; public ConferenceService() { - sessions = generateSampleData(); + updateSessions(); + } + + @Scheduled(fixedRate = 300_000) // every five minutes + private void scheduler() { + updateSessions(); + } + + private void updateSessions() { + final var newSessions = generateSampleData().stream() + .sorted() + .toList(); + synchronized (this) { + sessions = newSessions; + } + } + + public List getAllSessions() { + synchronized (this) { + return List.copyOf(sessions); + } } public List getRunningSessions() { final LocalDateTime now = LocalDateTime.now(); - return sessions.stream() + return getAllSessions().stream() .filter(session -> session.startDate().isBefore(now) && session.endDate().isAfter(now)) - .sorted() .toList(); } public List getFutureSessions() { final LocalDateTime now = LocalDateTime.now(); - return sessions.stream() + return getAllSessions().stream() .filter(session -> session.startDate().isAfter(now)) - .sorted() .toList(); } @@ -70,6 +89,7 @@ public List getNextSessions() { .toList(); } + @SuppressWarnings("java:S125") private List generateSampleData() { final int sampleDataSize = 100; final int sampleSessionParallel = 15;