NG
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / persistence / kafka / ChatRoomChannel.java
index 9ea23b1..8bbc82e 100644 (file)
@@ -14,18 +14,16 @@ import reactor.core.publisher.Mono;
 
 import java.time.*;
 import java.util.List;
-import java.util.Optional;
 import java.util.UUID;
-import java.util.concurrent.Callable;
 
 
 @RequiredArgsConstructor
 @Slf4j
-public class ChatRoomChannel implements Callable<Optional<Exception>>
+public class ChatRoomChannel implements Runnable
 {
   private final String topic;
-  private final Producer<Integer, ChatRoomTo> producer;
-  private final Consumer<Integer, ChatRoomTo> consumer;
+  private final Producer<Integer, CreateChatRoomRequestTo> producer;
+  private final Consumer<Integer, CreateChatRoomRequestTo> consumer;
   private final ShardingStrategy shardingStrategy;
   private final ChatMessageChannel chatMessageChannel;
   private final Clock clock;
@@ -34,43 +32,8 @@ public class ChatRoomChannel implements Callable<Optional<Exception>>
   private boolean running;
 
 
-  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);
-        }
-      }));
-    });
-  }
-
   @Override
-  public Optional<Exception> call()
+  public void run()
   {
     consumer.assign(List.of(new TopicPartition(topic, 0)));
 
@@ -80,10 +43,10 @@ public class ChatRoomChannel implements Callable<Optional<Exception>>
     {
       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)
         {
           createChatRoom(record.value().toChatRoomInfo());
         }
@@ -93,15 +56,9 @@ public class ChatRoomChannel implements Callable<Optional<Exception>>
         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();
   }