NEU
authorKai Moritz <kai@juplo.de>
Wed, 19 Apr 2023 15:30:55 +0000 (17:30 +0200)
committerKai Moritz <kai@juplo.de>
Wed, 19 Apr 2023 15:30:55 +0000 (17:30 +0200)
src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/ChatMessageChannel.java
src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/ChatRoomChannel.java

index 230f822..78665de 100644 (file)
@@ -251,35 +251,19 @@ public class ChatMessageChannel implements Runnable, ConsumerRebalanceListener
   {
     Integer partition = chatRoom.getShard();
     UUID chatRoomId = chatRoom.getId();
-    ChatRoom existingChatRoom = chatrooms[partition].get(chatRoomId);
-    if (existingChatRoom == null)
+    if (chatrooms[partition].containsKey(chatRoomId))
+    {
+      log.warn("Ignoring existing chat-room: " + chatRoom);
+    }
+    else
     {
       log.info(
-          "Creating new chat-room in partition {}: {}",
+          "Adding new chat-room to partition {}: {}",
           partition,
           chatRoom);
+
       chatrooms[partition].put(chatRoomId, chatRoom);
     }
-    else
-    {
-      if (chatRoom.getShard() != existingChatRoom.getShard())
-      {
-        throw new IllegalArgumentException(
-            "Could not change the shard of existing chat-room " +
-            chatRoomId + " from " +
-            existingChatRoom.getShard() + " to " +
-            chatRoom.getShard());
-      }
-      else
-      {
-        log.info(
-            "Updating chat-room in partition {}: {} -> {}",
-            partition,
-            existingChatRoom,
-            chatRoom);
-        existingChatRoom.s
-      }
-    }
   }
 
   Mono<ChatRoom> getChatRoom(int shard, UUID id)
index b08a50d..ac44f99 100644 (file)
@@ -27,49 +27,12 @@ public class ChatRoomChannel implements Runnable
   private final Producer<Integer, ChatRoomTo> producer;
   private final ShardingStrategy shardingStrategy;
   private final ChatMessageChannel chatMessageChannel;
+  private final Clock clock;
+  private final int bufferSize;
 
   private boolean running;
 
 
-  @Override
-  public void run()
-  {
-    consumer.assign(List.of(new TopicPartition(topic, 0)));
-
-    running = true;
-
-    while (running)
-    {
-      try
-      {
-        ConsumerRecords<Integer, ChatRoomTo> records = consumer.poll(Duration.ofMinutes(5));
-        log.info("Fetched {} messages", records.count());
-
-        for (ConsumerRecord<Integer, ChatRoomTo> record : records)
-        {
-          UUID id = record.value().getId();
-          String name = record.value().getName();
-          chatRoomFactory.createChatRoom(id, name);
-        }
-      }
-      catch (WakeupException e)
-      {
-      }
-      catch (RecordDeserializationException e)
-      {
-      }
-    }
-  }
-
-  void createChatRoom()
-  {
-    log.info("Creating ChatRoom with buffer-size {}", bufferSize);
-    KafkaChatRoomService service = new KafkaChatRoomService(chatMessageChannel, id);
-    int shard = shardingStrategy.selectShard(id);
-    ChatRoom chatRoom = new ChatRoom(id, name, shard, clock, service, bufferSize);
-    chatMessageChannel.putChatRoom(chatRoom);
-  }
-
   Mono<ChatRoomInfo> sendCreateChatRoomRequest(
       UUID chatRoomId,
       String name)
@@ -104,4 +67,42 @@ public class ChatRoomChannel implements Runnable
       }));
     });
   }
+
+  @Override
+  public void run()
+  {
+    consumer.assign(List.of(new TopicPartition(topic, 0)));
+
+    running = true;
+
+    while (running)
+    {
+      try
+      {
+        ConsumerRecords<Integer, ChatRoomTo> records = consumer.poll(Duration.ofMinutes(5));
+        log.info("Fetched {} messages", records.count());
+
+        for (ConsumerRecord<Integer, ChatRoomTo> record : records)
+        {
+          createChatRoom(record.value().toChatRoomInfo());
+        }
+      }
+      catch (WakeupException e)
+      {
+        log.info("Received WakeupException, exiting!");
+        running = false;
+      }
+    }
+  }
+
+  void createChatRoom(ChatRoomInfo chatRoomInfo)
+  {
+    UUID id = chatRoomInfo.getId();
+    String name = chatRoomInfo.getName();
+    int shard = chatRoomInfo.getShard();
+    log.info("Creating ChatRoom {} with buffer-size {}", id, bufferSize);
+    KafkaChatRoomService service = new KafkaChatRoomService(chatMessageChannel, id);
+    ChatRoom chatRoom = new ChatRoom(id, name, shard, clock, service, bufferSize);
+    chatMessageChannel.putChatRoom(chatRoom);
+  }
 }