NEU
authorKai Moritz <kai@juplo.de>
Thu, 20 Apr 2023 07:11:16 +0000 (09:11 +0200)
committerKai Moritz <kai@juplo.de>
Thu, 20 Apr 2023 07:11:16 +0000 (09:11 +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
src/test/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaServicesConfiguration.java

index 7b19bb6..43ea399 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;
@@ -153,7 +152,7 @@ public class ChatMessageChannel implements Callable<Optional<Exception>>, Consum
   }
 
   @Override
-  public Optional<Exception> call()
+  public void run()
   {
     consumer.subscribe(List.of(topic));
 
@@ -191,15 +190,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)
index 9ea23b1..2a34c81 100644 (file)
@@ -14,14 +14,12 @@ 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;
@@ -70,7 +68,7 @@ public class ChatRoomChannel implements Callable<Optional<Exception>>
   }
 
   @Override
-  public Optional<Exception> call()
+  public void run()
   {
     consumer.assign(List.of(new TopicPartition(topic, 0)));
 
@@ -93,15 +91,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();
   }
 
 
index 3148c6f..5367fdf 100644 (file)
@@ -9,6 +9,7 @@ import de.juplo.kafka.chat.backend.domain.SimpleChatHome;
 import de.juplo.kafka.chat.backend.persistence.KafkaLikeShardingStrategy;
 import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatHomeService;
 import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomFactory;
+import jakarta.annotation.PreDestroy;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.clients.consumer.Consumer;
 import org.apache.kafka.clients.producer.Producer;
@@ -43,37 +44,48 @@ public class KafkaServicesConfiguration implements ApplicationRunner
 
   @Autowired
   ChatMessageChannel chatMessageChannel;
+  @Autowired
+  ChatRoomChannel chatRoomChannel;
 
-  CompletableFuture<Optional<Exception>> chatRoomChannelConsumerJob;
-  CompletableFuture<Optional<Exception>> chatMessageChannelConsumerJob;
+  CompletableFuture<Void> chatRoomChannelConsumerJob;
+  CompletableFuture<Void> chatMessageChannelConsumerJob;
 
 
   @Override
   public void run(ApplicationArguments args) throws Exception
   {
     log.info("Starting the consumer for the ChatRoomChannel");
-    chatRoomChannelConsumerJob = taskExecutor.submitCompletable(chatMessageChannel);
-    chatRoomChannelConsumerJob.thenAccept(exceptionOptional ->
-    {
-      exceptionOptional.ifPresent();
-          log.info("SimpleConsumer exited normally, exit-status: {}", exitStatus);
-          SpringApplication.exit(context, () -> exitStatus);
-        },
-        t ->
+    chatRoomChannelConsumerJob = taskExecutor
+        .submitCompletable(chatRoomChannel)
+        .exceptionally(e ->
+        {
+          log.error("The consumer for the ChatRoomChannel exited abnormally!", e);
+          return null;
+        });
+    log.info("Starting the consumer for the ChatMessageChannel");
+    chatMessageChannelConsumerJob = taskExecutor
+        .submitCompletable(chatMessageChannel)
+        .exceptionally(e ->
         {
-          log.error("SimpleConsumer exited abnormally!", t);
-          SpringApplication.exit(context, () -> 2);
+          log.error("The consumer for the ChatMessageChannel exited abnormally!", e);
+          return null;
         });
   }
 
   @PreDestroy
-  public void shutdown() throws ExecutionException, InterruptedException
+  public void joinChatRoomChannelConsumerJob()
+  {
+    log.info("Waiting for the consumer of the ChatRoomChannel to finish its work");
+    chatRoomChannelConsumerJob.join();
+    log.info("Joined the consumer of the ChatRoomChannel");
+  }
+
+  @PreDestroy
+  public void joinChatMessageChannelConsumerJob()
   {
-    log.info("Signaling SimpleConsumer to quit its work");
-    kafkaConsumer.wakeup();
-    log.info("Waiting for SimpleConsumer to finish its work");
-    consumerJob.get();
-    log.info("SimpleConsumer finished its work");
+    log.info("Waiting for the consumer of the ChatMessageChannel to finish its work");
+    chatMessageChannelConsumerJob.join();
+    log.info("Joined the consumer of the ChatMessageChannel");
   }