NEU
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / persistence / kafka / ChatRoomChannel.java
index 0c553e6..97ee988 100644 (file)
@@ -1,90 +1,43 @@
 package de.juplo.kafka.chat.backend.persistence.kafka;
 
 import de.juplo.kafka.chat.backend.domain.*;
-import de.juplo.kafka.chat.backend.persistence.KafkaLikeShardingStrategy;
+import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.clients.consumer.Consumer;
-import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
 import org.apache.kafka.clients.consumer.ConsumerRecord;
 import org.apache.kafka.clients.consumer.ConsumerRecords;
 import org.apache.kafka.clients.producer.Producer;
 import org.apache.kafka.clients.producer.ProducerRecord;
 import org.apache.kafka.common.TopicPartition;
-import org.apache.kafka.common.errors.RecordDeserializationException;
 import org.apache.kafka.common.errors.WakeupException;
-import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
 import java.time.*;
-import java.util.Collection;
 import java.util.List;
-import java.util.Map;
 import java.util.UUID;
-import java.util.stream.IntStream;
 
 
+@RequiredArgsConstructor
 @Slf4j
 public class ChatRoomChannel implements Runnable
 {
   private final String topic;
-  private final Consumer<Integer, ChatRoomTo> consumer;
   private final Producer<Integer, ChatRoomTo> producer;
+  private final Consumer<Integer, ChatRoomTo> consumer;
   private final ShardingStrategy shardingStrategy;
   private final ChatMessageChannel chatMessageChannel;
+  private final Clock clock;
+  private final int bufferSize;
 
   private boolean running;
 
 
-  public ChatRoomChannel(
-    String topic,
-    Consumer<Integer, ChatRoomTo> consumer,
-    Producer<Integer, ChatRoomTo> producer,
-    int numShards,
-    ChatMessageChannel chatMessageChannel)
-  {
-    log.debug(
-        "Creating ChatRoomChannel for topic {} with sharding for {} partitions",
-        topic,
-        numShards);
-    this.topic = topic;
-    this.consumer = consumer;
-    this.producer = producer;
-    this.shardingStrategy = new KafkaLikeShardingStrategy(numShards);
-    this.chatMessageChannel = chatMessageChannel;
-  }
-
-
-  @Override
-  public void run()
-  {
-    consumer.assign(List.of(new TopicPartition(topic, 0)));
-
-    running = true;
-
-    while (running)
-    {
-      try
-      {
-        ConsumerRecords<String, MessageTo> records = consumer.poll(Duration.ofMinutes(5));
-        log.info("Fetched {} messages", records.count());
-
-      }
-      catch (WakeupException e)
-      {
-      }
-      catch (RecordDeserializationException e)
-      {
-      }
-    }
-  }
-
-
   Mono<ChatRoomInfo> sendCreateChatRoomRequest(
       UUID chatRoomId,
       String name)
   {
     int shard = this.shardingStrategy.selectShard(chatRoomId);
-    ChatRoomTo chatRoomTo = ChatRoomTo.of(chatRoomId, name, shard);
+    ChatRoomTo chatRoomTo = ChatRoomTo.of(chatRoomId.toString(), name, shard);
     return Mono.create(sink ->
     {
       ProducerRecord<Integer, ChatRoomTo> record =
@@ -97,22 +50,61 @@ public class ChatRoomChannel implements Runnable
       {
         if (metadata != null)
         {
-          log.info("Successfully send chreate-request {}", chatRoomTo);
+          log.info("Successfully send chreate-request for chat room: {}", chatRoomTo);
           sink.success(chatRoomTo.toChatRoomInfo());
         }
         else
         {
           // On send-failure
           log.error(
-              "Could not create-request for chat-room={}, key={}, timestamp={}, text={}: {}",
+              "Could not send create-request for chat room (id={}, name={}): {}",
               chatRoomId,
-              key,
-              timestamp,
-              text,
+              name,
               exception);
           sink.error(exception);
         }
       }));
     });
   }
+
+  @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;
+      }
+    }
+
+    log.info("Exiting normally");
+  }
+
+
+  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);
+  }
 }