WIP:refactor: Refined channel-states, introduced `ChannelState` -- ALIGN
authorKai Moritz <kai@juplo.de>
Mon, 4 Mar 2024 14:05:23 +0000 (15:05 +0100)
committerKai Moritz <kai@juplo.de>
Mon, 4 Mar 2024 14:05:23 +0000 (15:05 +0100)
src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/ChannelExecutor.java
src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/ChannelRunner.java
src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaServicesApplicationRunner.java
src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaServicesConfiguration.java
src/test/java/de/juplo/kafka/chat/backend/KafkaConfigurationIT.java
src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaChatHomeServiceTest.java
src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaTestUtils.java

index 9425bdf..fb55448 100644 (file)
@@ -12,7 +12,7 @@ import java.util.concurrent.CompletableFuture;
 
 @RequiredArgsConstructor
 @Slf4j
-public class ConsumerTaskExecutor
+public class ChannelExecutor
 {
   private final ThreadPoolTaskExecutor taskExecutor;
   private final Runnable consumerTask;
index 0c6de1d..9520448 100644 (file)
@@ -6,26 +6,26 @@ import lombok.extern.slf4j.Slf4j;
 
 @RequiredArgsConstructor
 @Slf4j
-public class ConsumerTaskRunner
+public class ChannelRunner
 {
-  private final ConsumerTaskExecutor infoChannelConsumerTaskExecutor;
-  private final ConsumerTaskExecutor dataChannelConsumerTaskExecutor;
+  private final ChannelExecutor infoChannelChannelExecutor;
+  private final ChannelExecutor dataChannelChannelExecutor;
   private final InfoChannel infoChannel;
 
   public void executeConsumerTasks()
   {
-    infoChannelConsumerTaskExecutor.executeConsumerTask();
-    dataChannelConsumerTaskExecutor.executeConsumerTask();
+    infoChannelChannelExecutor.executeConsumerTask();
+    dataChannelChannelExecutor.executeConsumerTask();
   }
 
   public void joinConsumerTasks() throws InterruptedException
   {
-    dataChannelConsumerTaskExecutor.joinConsumerTaskJob();
+    dataChannelChannelExecutor.joinConsumerTaskJob();
     while (infoChannel.getChannelState() != ChannelState.SHUTTING_DOWN)
     {
       log.info("Waiting for {} to shut down...", infoChannel);
       Thread.sleep(1000);
     }
-    infoChannelConsumerTaskExecutor.joinConsumerTaskJob();
+    infoChannelChannelExecutor.joinConsumerTaskJob();
   }
 }
index 69e94c9..ba42707 100644 (file)
@@ -16,18 +16,18 @@ import org.springframework.stereotype.Component;
 @RequiredArgsConstructor
 public class KafkaServicesApplicationRunner implements ApplicationRunner
 {
-  private final ConsumerTaskRunner consumerTaskRunner;
+  private final ChannelRunner channelRunner;
 
 
   @Override
   public void run(ApplicationArguments args)
   {
-    consumerTaskRunner.executeConsumerTasks();
+    channelRunner.executeConsumerTasks();
   }
 
   @PreDestroy
   public void joinConsumerTasks() throws InterruptedException
   {
-    consumerTaskRunner.joinConsumerTasks();
+    channelRunner.joinConsumerTasks();
   }
 }
index 3337127..33f8a6e 100644 (file)
@@ -1,7 +1,6 @@
 package de.juplo.kafka.chat.backend.implementation.kafka;
 
 import de.juplo.kafka.chat.backend.ChatBackendProperties;
-import de.juplo.kafka.chat.backend.domain.ChatHomeService;
 import de.juplo.kafka.chat.backend.domain.ShardingPublisherStrategy;
 import de.juplo.kafka.chat.backend.implementation.haproxy.HaproxyShardingPublisherStrategy;
 import de.juplo.kafka.chat.backend.implementation.kafka.messages.AbstractMessageTo;
@@ -40,25 +39,25 @@ import java.util.Properties;
 public class KafkaServicesConfiguration
 {
   @Bean
-  ConsumerTaskRunner consumerTaskRunner(
-      ConsumerTaskExecutor infoChannelConsumerTaskExecutor,
-      ConsumerTaskExecutor dataChannelConsumerTaskExecutor,
+  ChannelRunner consumerTaskRunner(
+      ChannelExecutor infoChannelChannelExecutor,
+      ChannelExecutor dataChannelChannelExecutor,
       InfoChannel infoChannel)
   {
-    return new ConsumerTaskRunner(
-        infoChannelConsumerTaskExecutor,
-        dataChannelConsumerTaskExecutor,
+    return new ChannelRunner(
+        infoChannelChannelExecutor,
+        dataChannelChannelExecutor,
         infoChannel);
   }
 
   @Bean
-  ConsumerTaskExecutor infoChannelConsumerTaskExecutor(
+  ChannelExecutor infoChannelConsumerTaskExecutor(
       ThreadPoolTaskExecutor taskExecutor,
       InfoChannel infoChannel,
       Consumer<String, AbstractMessageTo> infoChannelConsumer,
       WorkAssignor infoChannelWorkAssignor)
   {
-    return new ConsumerTaskExecutor(
+    return new ChannelExecutor(
         taskExecutor,
         infoChannel,
         infoChannelConsumer,
@@ -82,13 +81,13 @@ public class KafkaServicesConfiguration
   }
 
   @Bean
-  ConsumerTaskExecutor dataChannelConsumerTaskExecutor(
+  ChannelExecutor dataChannelConsumerTaskExecutor(
       ThreadPoolTaskExecutor taskExecutor,
       DataChannel dataChannel,
       Consumer<String, AbstractMessageTo> dataChannelConsumer,
       WorkAssignor dataChannelWorkAssignor)
   {
-    return new ConsumerTaskExecutor(
+    return new ChannelExecutor(
         taskExecutor,
         dataChannel,
         dataChannelConsumer,
index e01e012..88a9b05 100644 (file)
@@ -43,21 +43,21 @@ class KafkaConfigurationIT extends AbstractConfigurationWithShardingIT
   @BeforeAll
   public static void sendAndLoadStoredData(
       @Autowired KafkaTemplate<String, String> messageTemplate,
-      @Autowired ConsumerTaskRunner consumerTaskRunner)
+      @Autowired ChannelRunner channelRunner)
   {
     KafkaTestUtils.sendAndLoadStoredData(
         messageTemplate,
         INFO_TOPIC,
         DATA_TOPIC,
-        consumerTaskRunner);
+        channelRunner);
   }
 
   @AfterAll
   static void joinConsumerTasks(
-      @Autowired ConsumerTaskRunner consumerTaskRunner)
+      @Autowired ChannelRunner channelRunner)
       throws InterruptedException
   {
-    KafkaTestUtils.joinConsumerTasks(consumerTaskRunner);
+    KafkaTestUtils.joinConsumerTasks(channelRunner);
   }
 
 
index 180ff15..6b75c46 100644 (file)
@@ -44,20 +44,20 @@ public class KafkaChatHomeServiceTest extends ChatHomeServiceWithShardsTest
   @BeforeAll
   static void sendAndLoadStoredData(
       @Autowired KafkaTemplate<String, String> messageTemplate,
-      @Autowired ConsumerTaskRunner consumerTaskRunner)
+      @Autowired ChannelRunner channelRunner)
   {
     KafkaTestUtils.sendAndLoadStoredData(
         messageTemplate,
         INFO_TOPIC,
         DATA_TOPIC,
-        consumerTaskRunner);
+        channelRunner);
   }
 
   @AfterAll
   static void joinConsumerTasks(
-      @Autowired ConsumerTaskRunner consumerTaskRunner)
+      @Autowired ChannelRunner channelRunner)
       throws InterruptedException
   {
-    KafkaTestUtils.joinConsumerTasks(consumerTaskRunner);
+    KafkaTestUtils.joinConsumerTasks(channelRunner);
   }
 }
index 2ede202..eb1a19d 100644 (file)
@@ -44,7 +44,7 @@ public abstract class KafkaTestUtils
       KafkaTemplate<String, String> messageTemplate,
       String infoTopic,
       String dataTopic,
-      ConsumerTaskRunner consumerTaskRunner)
+      ChannelRunner channelRunner)
   {
     send(messageTemplate, infoTopic, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\": \"5c73531c-6fc4-426c-adcb-afc5c140a0f7\", \"shard\": 2, \"name\": \"FOO\" }", "event_chatroom_created");
     send(messageTemplate, dataTopic, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 1, \"user\" : \"peter\", \"text\" : \"Hallo, ich heiße Peter!\" }", "event_chatmessage_received");
@@ -52,7 +52,7 @@ public abstract class KafkaTestUtils
     send(messageTemplate, dataTopic, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 2, \"user\" : \"peter\", \"text\" : \"Willst du mit mir gehen?\" }", "event_chatmessage_received");
     send(messageTemplate, dataTopic, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 1, \"user\" : \"klaus\", \"text\" : \"Ja? Nein? Vielleicht??\" }", "event_chatmessage_received");
 
-    consumerTaskRunner.executeConsumerTasks();
+    channelRunner.executeConsumerTasks();
   }
 
   private static void send(
@@ -72,8 +72,8 @@ public abstract class KafkaTestUtils
         new TopicPartition(result.getRecordMetadata().topic(), result.getRecordMetadata().partition()));
   }
 
-  public static void joinConsumerTasks(ConsumerTaskRunner consumerTaskRunner) throws InterruptedException
+  public static void joinConsumerTasks(ChannelRunner channelRunner) throws InterruptedException
   {
-    consumerTaskRunner.joinConsumerTasks();
+    channelRunner.joinConsumerTasks();
   }
 }