From d5cd41aac014f3b0ea3e5f6a4c20f0ec204bde03 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sun, 18 Feb 2024 21:46:56 +0100 Subject: [PATCH] WIP:callbacks - chat-home --- .../implementation/StorageStrategy.java | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) 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 9dd76250..11f91f4e 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 @@ -17,27 +17,39 @@ public interface StorageStrategy Logger log = LoggerFactory.getLogger(StorageStrategy.class.getCanonicalName()); default void write(ChatHomeService chatHomeService) + { + write( + chatHomeService, + this::logSuccessChatHomeService, + this::logFailureChatHomeService); + } + + default void write( + ChatHomeService chatHomeService, + ChatHomeServiceWrittenSuccessCallback successCallback, + ChatHomeServiceWrittenFailureCallback failureCallback) { writeChatRoomInfo( chatHomeService .getChatRoomInfo() + .doOnComplete(() -> successCallback.accept(chatHomeService)) + .doOnError(throwable -> failureCallback.accept(chatHomeService, throwable)) .doOnNext(chatRoomInfo -> writeChatRoomData( chatRoomInfo.getId(), chatHomeService .getChatRoomData(chatRoomInfo.getId()) .flatMapMany(chatRoomData -> chatRoomData.getMessages()), - this::logSuccess, - this::logFailure))); + this::logSuccessChatRoom, + this::logFailureChatRoom))); } - void writeChatRoomInfo(Flux chatRoomInfoFlux); Flux readChatRoomInfo(); default void writeChatRoomData( UUID chatRoomId, Flux messageFlux, - SuccessCallback successCallback, - FailureCallback failureCallback) + ChatRoomWrittenSuccessCallback successCallback, + ChatRoomWrittenFailureCallback failureCallback) { writeChatRoomData( chatRoomId, @@ -48,15 +60,28 @@ public interface StorageStrategy void writeChatRoomData(UUID chatRoomId, Flux messageFlux); Flux readChatRoomData(UUID chatRoomId); - interface SuccessCallback extends Consumer {} - interface FailureCallback extends BiConsumer {} + interface ChatHomeServiceWrittenSuccessCallback extends Consumer {} + interface ChatHomeServiceWrittenFailureCallback extends BiConsumer {} + + default void logSuccessChatHomeService(ChatHomeService chatHomeService) + { + log.info("Successfully stored chat-home {}", chatHomeService); + } + + default void logFailureChatHomeService(ChatHomeService chatHomeService, Throwable throwable) + { + log.error("Could not store chat-home {}: {}", chatHomeService, throwable); + } + + interface ChatRoomWrittenSuccessCallback extends Consumer {} + interface ChatRoomWrittenFailureCallback extends BiConsumer {} - default void logSuccess(UUID chatRoomId) + default void logSuccessChatRoom(UUID chatRoomId) { log.info("Successfully stored chat-room {}", chatRoomId); } - default void logFailure(UUID chatRoomId, Throwable throwable) + default void logFailureChatRoom(UUID chatRoomId, Throwable throwable) { log.error("Could not store chat-room {}: {}", chatRoomId, throwable); } -- 2.20.1