From: Kai Moritz Date: Sun, 8 Jan 2023 09:57:25 +0000 (+0100) Subject: refactor: The `ChatRoom` determines the timestamp of a `Message` X-Git-Tag: wip~67 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=9de7f5dd0a6cd0205a9540fac141614af57ae8f4;p=demos%2Fkafka%2Fchat refactor: The `ChatRoom` determines the timestamp of a `Message` --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java index 1b568294..05fc2cb7 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java @@ -39,9 +39,11 @@ public class ChatBackendConfiguration } @Bean - InMemoryChatHomeService chatHomeService(ChatBackendProperties properties) + InMemoryChatHomeService chatHomeService( + Clock clock, + ChatBackendProperties properties) { - return new InMemoryChatHomeService(properties.getChatroomBufferSize()); + return new InMemoryChatHomeService(clock, properties.getChatroomBufferSize()); } @Bean diff --git a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java index 68c53d55..cc6958fe 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java +++ b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java @@ -9,8 +9,6 @@ import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import java.time.Clock; -import java.time.LocalDateTime; import java.util.Optional; import java.util.UUID; import java.util.stream.Stream; @@ -21,7 +19,6 @@ import java.util.stream.Stream; public class ChatBackendController { private final ChatHome chatHome; - private final Clock clock; private final StorageStrategy storageStrategy; @@ -78,7 +75,6 @@ public class ChatBackendController chatroom .addMessage( messageId, - LocalDateTime.now(clock), username, text) .switchIfEmpty(chatroom.getMessage(username, messageId)) diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java index 0fdea33e..63b5b363 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java @@ -6,6 +6,7 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.core.publisher.Sinks; +import java.time.Clock; import java.time.LocalDateTime; import java.util.*; @@ -17,6 +18,7 @@ public class ChatRoom private final UUID id; @Getter private final String name; + private final Clock clock; private final ChatRoomService service; private final int bufferSize; private Sinks.Many sink; @@ -24,11 +26,13 @@ public class ChatRoom public ChatRoom( UUID id, String name, + Clock clock, ChatRoomService service, int bufferSize) { this.id = id; this.name = name; + this.clock = clock; this.service = service; this.bufferSize = bufferSize; this.sink = createSink(); @@ -37,12 +41,11 @@ public class ChatRoom synchronized public Mono addMessage( Long id, - LocalDateTime timestamp, String user, String text) { return service - .persistMessage(Message.MessageKey.of(user, id), timestamp, text) + .persistMessage(Message.MessageKey.of(user, id), LocalDateTime.now(clock), text) .doOnNext(message -> { Sinks.EmitResult result = sink.tryEmitNext(message); diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatHomeService.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatHomeService.java index e57b99b7..df0ecba5 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatHomeService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatHomeService.java @@ -4,6 +4,7 @@ import de.juplo.kafka.chat.backend.domain.ChatRoom; import de.juplo.kafka.chat.backend.domain.ChatHomeService; import lombok.RequiredArgsConstructor; +import java.time.Clock; import java.util.LinkedHashMap; import java.util.UUID; @@ -11,6 +12,7 @@ import java.util.UUID; @RequiredArgsConstructor public class InMemoryChatHomeService implements ChatHomeService { + private final Clock clock; private final int bufferSize; @@ -19,7 +21,7 @@ public class InMemoryChatHomeService implements ChatHomeService()); - return new ChatRoom(id, name, service, bufferSize); + return new ChatRoom(id, name, clock, service, bufferSize); } public ChatRoom restoreChatroom( @@ -27,6 +29,6 @@ public class InMemoryChatHomeService implements ChatHomeService