From: Kai Moritz Date: Sat, 3 Feb 2024 21:44:02 +0000 (+0100) Subject: refactor: Added success- and failure-callbacks to `StorageStrategy` X-Git-Tag: rebase--2024-02-20--09-03~13 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=1a309b11c39da10ec1f8aa714c5aaea0f766410a;p=demos%2Fkafka%2Fchat refactor: Added success- and failure-callbacks to `StorageStrategy` --- 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 24fd15f0..8f91ad02 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 @@ -3,13 +3,19 @@ package de.juplo.kafka.chat.backend.implementation; import de.juplo.kafka.chat.backend.domain.ChatHomeService; import de.juplo.kafka.chat.backend.domain.ChatRoomInfo; import de.juplo.kafka.chat.backend.domain.Message; +import org.slf4j.Logger; +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 { + Logger log = LoggerFactory.getLogger(StorageStrategy.class.getCanonicalName()); + default void write(ChatHomeService chatHomeService) { writeChatRoomInfo( @@ -20,11 +26,38 @@ public interface StorageStrategy chatRoomInfo.getId(), chatHomeService .getChatRoomData(chatRoomInfo.getId()) - .flatMapMany(chatRoomData -> chatRoomData.getMessages())))); + .flatMapMany(chatRoomData -> chatRoomData.getMessages()), + this::logSuccessChatRoom, + this::logFailureChatRoom))); } void writeChatRoomInfo(Flux chatRoomInfoFlux); Flux readChatRoomInfo(); + default void writeChatRoomData( + UUID chatRoomId, + Flux messageFlux, + ChatRoomWrittenSuccessCallback successCallback, + ChatRoomWrittenFailureCallback failureCallback) + { + writeChatRoomData( + chatRoomId, + messageFlux + .doOnComplete(() -> successCallback.accept(chatRoomId)) + .doOnError(throwable -> failureCallback.accept(chatRoomId, throwable))); + } void writeChatRoomData(UUID chatRoomId, Flux messageFlux); Flux readChatRoomData(UUID chatRoomId); + + 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); + } }