NEU vs. NG ??
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / persistence / kafka / ChatMessageChannel.java
index 7b19bb6..138d9a7 100644 (file)
@@ -18,12 +18,11 @@ import reactor.core.publisher.Mono;
 
 import java.time.*;
 import java.util.*;
-import java.util.concurrent.Callable;
 import java.util.stream.IntStream;
 
 
 @Slf4j
-public class ChatMessageChannel implements Callable<Optional<Exception>>, ConsumerRebalanceListener
+public class ChatMessageChannel implements Runnable, ConsumerRebalanceListener
 {
   private final String topic;
   private final Producer<String, MessageTo> producer;
@@ -61,6 +60,9 @@ public class ChatMessageChannel implements Callable<Optional<Exception>>, Consum
     this.currentOffset = new long[numShards];
     this.nextOffset = new long[numShards];
     this.chatrooms = new Map[numShards];
+    IntStream
+        .range(0, numShards)
+        .forEach(shard -> this.chatrooms[shard] = new HashMap<>());
     this.shardingStrategy = new KafkaLikeShardingStrategy(numShards);
   }
 
@@ -153,9 +155,9 @@ public class ChatMessageChannel implements Callable<Optional<Exception>>, Consum
   }
 
   @Override
-  public Optional<Exception> call()
+  public void run()
   {
-    consumer.subscribe(List.of(topic));
+    consumer.subscribe(List.of(topic), this);
 
     running = true;
 
@@ -191,15 +193,9 @@ public class ChatMessageChannel implements Callable<Optional<Exception>>, Consum
         log.info("Received WakeupException, exiting!");
         running = false;
       }
-      catch (Exception e)
-      {
-        log.error("Exiting abnormally!");
-        return Optional.of(e);
-      }
     }
 
     log.info("Exiting normally");
-    return Optional.empty();
   }
 
   void loadMessages(ConsumerRecords<String, MessageTo> records)
@@ -220,7 +216,7 @@ public class ChatMessageChannel implements Callable<Optional<Exception>>, Consum
       ChatRoom chatRoom = chatrooms[record.partition()].get(chatRoomId);
       if (chatRoom == null)
       {
-        // Alles pausieren und erst von putChatRoom wieder resumen lassen!
+        // TODO: Alles pausieren und erst von putChatRoom wieder resumen lassen!
       }
       KafkaChatRoomService kafkaChatRoomService =
           (KafkaChatRoomService) chatRoom.getChatRoomService();
@@ -234,11 +230,7 @@ public class ChatMessageChannel implements Callable<Optional<Exception>>, Consum
     return IntStream
         .range(0, numShards)
         .filter(shard -> isShardOwned[shard])
-        .mapToObj(shard -> nextOffset[shard] >= currentOffset[shard])
-        .collect(
-            () -> Boolean.TRUE, // TODO: Boolean is immutable
-            (acc, v) -> Boolean.valueOf(acc && v), // TODO: Boolean is immutable
-            (a, b) -> Boolean.valueOf(a && b)); // TODO: Boolean is immutable
+        .allMatch(shard -> nextOffset[shard] >= currentOffset[shard]);
   }
 
   void pauseAllOwnedPartions()