NG
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / persistence / kafka / ChatRoomChannel.java
index b08a50d..8bbc82e 100644 (file)
@@ -9,7 +9,6 @@ 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.Mono;
 
@@ -23,10 +22,12 @@ import java.util.UUID;
 public class ChatRoomChannel implements Runnable
 {
   private final String topic;
-  private final Consumer<Integer, ChatRoomTo> consumer;
-  private final Producer<Integer, ChatRoomTo> producer;
+  private final Producer<Integer, CreateChatRoomRequestTo> producer;
+  private final Consumer<Integer, CreateChatRoomRequestTo> consumer;
   private final ShardingStrategy shardingStrategy;
   private final ChatMessageChannel chatMessageChannel;
+  private final Clock clock;
+  private final int bufferSize;
 
   private boolean running;
 
@@ -42,66 +43,33 @@ public class ChatRoomChannel implements Runnable
     {
       try
       {
-        ConsumerRecords<Integer, ChatRoomTo> records = consumer.poll(Duration.ofMinutes(5));
+        ConsumerRecords<Integer, CreateChatRoomRequestTo> records = consumer.poll(Duration.ofMinutes(5));
         log.info("Fetched {} messages", records.count());
 
-        for (ConsumerRecord<Integer, ChatRoomTo> record : records)
+        for (ConsumerRecord<Integer, CreateChatRoomRequestTo> record : records)
         {
-          UUID id = record.value().getId();
-          String name = record.value().getName();
-          chatRoomFactory.createChatRoom(id, name);
+          createChatRoom(record.value().toChatRoomInfo());
         }
       }
       catch (WakeupException e)
       {
-      }
-      catch (RecordDeserializationException e)
-      {
+        log.info("Received WakeupException, exiting!");
+        running = false;
       }
     }
+
+    log.info("Exiting normally");
   }
 
-  void createChatRoom()
+
+  void createChatRoom(ChatRoomInfo chatRoomInfo)
   {
-    log.info("Creating ChatRoom with buffer-size {}", bufferSize);
+    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);
-    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)
-  {
-    int shard = this.shardingStrategy.selectShard(chatRoomId);
-    ChatRoomTo chatRoomTo = ChatRoomTo.of(chatRoomId, name, shard);
-    return Mono.create(sink ->
-    {
-      ProducerRecord<Integer, ChatRoomTo> record =
-          new ProducerRecord<>(
-              topic,
-              shard,
-              chatRoomTo);
-
-      producer.send(record, ((metadata, exception) ->
-      {
-        if (metadata != null)
-        {
-          log.info("Successfully send chreate-request for chat room: {}", chatRoomTo);
-          sink.success(chatRoomTo.toChatRoomInfo());
-        }
-        else
-        {
-          // On send-failure
-          log.error(
-              "Could not send create-request for chat room (id={}, name={}): {}",
-              chatRoomId,
-              name,
-              exception);
-          sink.error(exception);
-        }
-      }));
-    });
-  }
 }