refactor: Added success- and failure-callbacks to `StorageStrategy`
authorKai Moritz <kai@juplo.de>
Sat, 3 Feb 2024 21:44:02 +0000 (22:44 +0100)
committerKai Moritz <kai@juplo.de>
Sat, 3 Feb 2024 21:44:02 +0000 (22:44 +0100)
src/main/java/de/juplo/kafka/chat/backend/implementation/StorageStrategy.java
src/main/java/de/juplo/kafka/chat/backend/storage/files/FilesStorageStrategy.java
src/main/java/de/juplo/kafka/chat/backend/storage/mongodb/MongoDbStorageStrategy.java
src/main/java/de/juplo/kafka/chat/backend/storage/nostorage/NoStorageStorageConfiguration.java

index 24fd15f..ba8bc23 100644 (file)
@@ -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(
@@ -23,8 +29,24 @@ public interface StorageStrategy
                         .flatMapMany(chatRoomData -> chatRoomData.getMessages()))));
   }
 
+  default void writeChatRoomData(UUID chatRoomId, Flux<Message> messageFlux)
+  {
+    writeChatRoomData(
+        chatRoomId,
+        messageFlux,
+        (id) -> log.info("Successfully stored chat-room {}", id),
+        (id, throwable) -> log.error("Could not store chat-room {}: {}", id, throwable));
+  }
+
   void writeChatRoomInfo(Flux<ChatRoomInfo> chatRoomInfoFlux);
   Flux<ChatRoomInfo> readChatRoomInfo();
-  void writeChatRoomData(UUID chatRoomId, Flux<Message> messageFlux);
+  void writeChatRoomData(
+      UUID chatRoomId,
+      Flux<Message> messageFlux,
+      SuccessCallback callback,
+      FailureCallback failureCallback);
   Flux<Message> readChatRoomData(UUID chatRoomId);
+
+  interface SuccessCallback extends Consumer<UUID> {}
+  interface FailureCallback extends BiConsumer<UUID, Throwable> {}
 }
index cb7dd31..1de0b44 100644 (file)
@@ -121,7 +121,9 @@ public class FilesStorageStrategy implements StorageStrategy
   @Override
   public void writeChatRoomData(
       UUID chatRoomId,
-      Flux<Message> messageFlux)
+      Flux<Message> messageFlux,
+      SuccessCallback successCallback,
+      FailureCallback failureCallback)
   {
     Path path = chatroomPath(chatRoomId);
     log.info("Writing messages for {} to {}", chatRoomId, path);
@@ -172,10 +174,12 @@ public class FilesStorageStrategy implements StorageStrategy
               throw new RuntimeException(e);
             }
           });
+
+      successCallback.accept(chatRoomId);
     }
     catch (IOException e)
     {
-      throw new RuntimeException(e);
+      failureCallback.accept(chatRoomId, e);
     }
   }
 
index b1bead9..972122e 100644 (file)
@@ -41,10 +41,16 @@ public class MongoDbStorageStrategy implements StorageStrategy
   }
 
   @Override
-  public void writeChatRoomData(UUID chatRoomId, Flux<Message> messageFlux)
+  public void writeChatRoomData(
+      UUID chatRoomId,
+      Flux<Message> messageFlux,
+      SuccessCallback successCallback,
+      FailureCallback failureCallback)
   {
     messageFlux
         .map(message -> MessageTo.from(chatRoomId, message))
+        .doOnComplete(() -> successCallback.accept(chatRoomId))
+        .doOnError(throwable -> failureCallback.accept(chatRoomId, throwable))
         .subscribe(messageTo -> messageRepository.save(messageTo));
   }
 
index 1b20aa3..376679a 100644 (file)
@@ -41,7 +41,15 @@ public class NoStorageStorageConfiguration
       }
 
       @Override
-      public void writeChatRoomData(UUID chatRoomId, Flux<Message> messageFlux) {}
+      public void writeChatRoomData(
+          UUID chatRoomId,
+          Flux<Message> messageFlux,
+          SuccessCallback successCallback,
+          FailureCallback failureCallback
+      )
+      {
+        successCallback.accept(chatRoomId);
+      }
 
       @Override
       public Flux<Message> readChatRoomData(UUID chatRoomId)