From: Kai Moritz Date: Tue, 20 Feb 2024 06:47:22 +0000 (+0100) Subject: refactor: Simplified `StorageStrategy` X-Git-Tag: rebase--2024-02-22~13 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=0e2b77f45fce3d760b39303c23dda744b9e8c615;p=demos%2Fkafka%2Fchat refactor: Simplified `StorageStrategy` * Reconfigurable success/error-logging was introduced for `NoStorageStorageStrategy`. * But as it turns out, this strategy can simply apply its logging in the overwritten method, that disables the whole storing-logic. * Hence, the interface was greatly simplified again, by removing this ununsed mechanism. --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/StorageStrategy.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/StorageStrategy.java index 6fa9c163..c6c50172 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/StorageStrategy.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/StorageStrategy.java @@ -8,8 +8,6 @@ import org.slf4j.LoggerFactory; import reactor.core.publisher.Flux; import java.util.UUID; -import java.util.function.BiConsumer; -import java.util.function.Consumer; public interface StorageStrategy @@ -17,74 +15,25 @@ public interface StorageStrategy Logger log = LoggerFactory.getLogger(StorageStrategy.class.getCanonicalName()); default Flux write(ChatHomeService chatHomeService) - { - return write( - chatHomeService, - this::logSuccessChatHomeService, - this::logFailureChatHomeService); - } - - default Flux write( - ChatHomeService chatHomeService, - ChatHomeServiceWrittenSuccessCallback successCallback, - ChatHomeServiceWrittenFailureCallback failureCallback) { return writeChatRoomInfo( chatHomeService .getChatRoomInfo() - .doOnComplete(() -> successCallback.accept(chatHomeService)) - .doOnError(throwable -> failureCallback.accept(chatHomeService, throwable)) + .doOnComplete(() -> log.info("Stored {}", chatHomeService)) + .doOnError(throwable -> log.error("Could not store {}: {}", chatHomeService, throwable)) .doOnNext(chatRoomInfo -> writeChatRoomData( chatRoomInfo.getId(), chatHomeService .getChatRoomData(chatRoomInfo.getId()) - .flatMapMany(chatRoomData -> chatRoomData.getMessages()), - this::logSuccessChatRoom, - this::logFailureChatRoom + .flatMapMany(chatRoomData -> chatRoomData.getMessages()) + .doOnComplete(() -> log.info("Stored {}", chatRoomInfo)) + .doOnError(throwable -> log.error("Could not store {}: {}", chatRoomInfo, throwable)) ) .subscribe())); } Flux writeChatRoomInfo(Flux chatRoomInfoFlux); Flux readChatRoomInfo(); - default Flux writeChatRoomData( - UUID chatRoomId, - Flux messageFlux, - ChatRoomWrittenSuccessCallback successCallback, - ChatRoomWrittenFailureCallback failureCallback) - { - return writeChatRoomData( - chatRoomId, - messageFlux - .doOnComplete(() -> successCallback.accept(chatRoomId)) - .doOnError(throwable -> failureCallback.accept(chatRoomId, throwable))); - } Flux writeChatRoomData(UUID chatRoomId, Flux messageFlux); Flux readChatRoomData(UUID chatRoomId); - - interface ChatHomeServiceWrittenSuccessCallback extends Consumer {} - interface ChatHomeServiceWrittenFailureCallback extends BiConsumer {} - - default void logSuccessChatHomeService(ChatHomeService chatHomeService) - { - log.info("Successfully stored {}", chatHomeService); - } - - default void logFailureChatHomeService(ChatHomeService chatHomeService, Throwable throwable) - { - log.error("Could not store {}: {}", chatHomeService, throwable); - } - - interface ChatRoomWrittenSuccessCallback extends Consumer {} - interface ChatRoomWrittenFailureCallback extends BiConsumer {} - - default void logSuccessChatRoom(UUID chatRoomId) - { - log.info("Successfully stored chat-room {}", chatRoomId); - } - - default void logFailureChatRoom(UUID chatRoomId, Throwable throwable) - { - log.error("Could not store chat-room {}: {}", chatRoomId, throwable); - } }