NEU
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / domain / ChatRoom.java
index 1c21fb9..b946309 100644 (file)
@@ -1,12 +1,10 @@
 package de.juplo.kafka.chat.backend.domain;
 
-import lombok.EqualsAndHashCode;
-import lombok.Getter;
-import lombok.ToString;
 import lombok.extern.slf4j.Slf4j;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 import reactor.core.publisher.Sinks;
+import reactor.core.publisher.SynchronousSink;
 
 import java.time.Clock;
 import java.time.LocalDateTime;
@@ -16,15 +14,9 @@ import java.util.regex.Pattern;
 
 
 @Slf4j
-@EqualsAndHashCode(of = { "id" })
-@ToString(of = { "id", "name" })
-public class ChatRoom
+public class ChatRoom extends ChatRoomInfo
 {
   public final static Pattern VALID_USER = Pattern.compile("^[a-z0-9-]{2,}$");
-  @Getter
-  private final UUID id;
-  @Getter
-  private final String name;
   private final Clock clock;
   private final ChatRoomService service;
   private final int bufferSize;
@@ -34,12 +26,13 @@ public class ChatRoom
   public ChatRoom(
       UUID id,
       String name,
+      int shard,
       Clock clock,
       ChatRoomService service,
       int bufferSize)
   {
-    this.id = id;
-    this.name = name;
+    super(id, name, shard);
+    log.info("Created ChatRoom {} with buffer-size {}", id, bufferSize);
     this.clock = clock;
     this.service = service;
     this.bufferSize = bufferSize;
@@ -62,12 +55,20 @@ public class ChatRoom
     Message.MessageKey key = Message.MessageKey.of(user, id);
     return service
         .getMessage(key)
-        .flatMap(existing -> text.equals(existing.getMessageText())
-            ? Mono.just(existing)
-            : Mono.error(() -> new MessageMutationException(existing, text)))
+        .handle((Message existing, SynchronousSink<Message> sink) ->
+        {
+          if (existing.getMessageText().equals(text))
+          {
+            sink.next(existing);
+          }
+          else
+          {
+            sink.error(new MessageMutationException(existing, text));
+          }
+        })
         .switchIfEmpty(
             Mono
-                .fromSupplier(() ->service.persistMessage(key, LocalDateTime.now(clock), text))
+                .defer(() -> service.persistMessage(key, LocalDateTime.now(clock), text))
                 .doOnNext(m ->
                 {
                   Sinks.EmitResult result = sink.tryEmitNext(m);
@@ -79,6 +80,11 @@ public class ChatRoom
   }
 
 
+  public ChatRoomService getChatRoomService()
+  {
+    return service;
+  }
+
   public Mono<Message> getMessage(String username, Long messageId)
   {
     Message.MessageKey key = Message.MessageKey.of(username, messageId);