refactor: Simplified shutdown - channel-tasks were joined multiple times
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / implementation / kafka / KafkaServicesConfiguration.java
index b5a442f..54aa41f 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,29 @@ import java.util.Properties;
 public class KafkaServicesConfiguration
 {
   @Bean
-  ConsumerTaskRunner consumerTaskRunner(
-      ConsumerTaskExecutor infoChannelConsumerTaskExecutor,
-      ConsumerTaskExecutor dataChannelConsumerTaskExecutor,
-      InfoChannel infoChannel)
+  KafkaServicesThreadPoolTaskExecutorCustomizer kafkaServicesThreadPoolTaskExecutorCustomizer()
   {
-    return new ConsumerTaskRunner(
-        infoChannelConsumerTaskExecutor,
-        dataChannelConsumerTaskExecutor,
-        infoChannel);
+    return new KafkaServicesThreadPoolTaskExecutorCustomizer();
   }
 
   @Bean
-  ConsumerTaskExecutor infoChannelConsumerTaskExecutor(
+  ChannelTaskRunner channelTaskRunner(
+      ChannelTaskExecutor infoChannelTaskExecutor,
+      ChannelTaskExecutor dataChannelTaskExecutor)
+  {
+    return new ChannelTaskRunner(
+        infoChannelTaskExecutor,
+        dataChannelTaskExecutor);
+  }
+
+  @Bean
+  ChannelTaskExecutor infoChannelTaskExecutor(
       ThreadPoolTaskExecutor taskExecutor,
       InfoChannel infoChannel,
       Consumer<String, AbstractMessageTo> infoChannelConsumer,
       WorkAssignor infoChannelWorkAssignor)
   {
-    return new ConsumerTaskExecutor(
+    return new ChannelTaskExecutor(
         taskExecutor,
         infoChannel,
         infoChannelConsumer,
@@ -82,13 +85,13 @@ public class KafkaServicesConfiguration
   }
 
   @Bean
-  ConsumerTaskExecutor dataChannelConsumerTaskExecutor(
+  ChannelTaskExecutor dataChannelTaskExecutor(
       ThreadPoolTaskExecutor taskExecutor,
       DataChannel dataChannel,
       Consumer<String, AbstractMessageTo> dataChannelConsumer,
       WorkAssignor dataChannelWorkAssignor)
   {
-    return new ConsumerTaskExecutor(
+    return new ChannelTaskExecutor(
         taskExecutor,
         dataChannel,
         dataChannelConsumer,
@@ -124,13 +127,19 @@ public class KafkaServicesConfiguration
   InfoChannel infoChannel(
       ChatBackendProperties properties,
       Producer<String, AbstractMessageTo> producer,
-      Consumer<String, AbstractMessageTo> infoChannelConsumer)
+      Consumer<String, AbstractMessageTo> infoChannelConsumer,
+      ChannelMediator channelMediator)
   {
-    return new InfoChannel(
+    InfoChannel infoChannel = new InfoChannel(
         properties.getKafka().getInfoChannelTopic(),
         producer,
         infoChannelConsumer,
-        properties.getKafka().getInstanceUri());
+        properties.getKafka().getPollingInterval(),
+        properties.getKafka().getNumPartitions(),
+        properties.getKafka().getInstanceUri(),
+        channelMediator);
+    channelMediator.setInfoChannel(infoChannel);
+    return infoChannel;
   }
 
   @Bean
@@ -140,20 +149,29 @@ public class KafkaServicesConfiguration
       Consumer<String, AbstractMessageTo> dataChannelConsumer,
       ZoneId zoneId,
       Clock clock,
-      InfoChannel infoChannel,
+      ChannelMediator channelMediator,
       ShardingPublisherStrategy shardingPublisherStrategy)
   {
-    return new DataChannel(
+    DataChannel dataChannel = new DataChannel(
         properties.getInstanceId(),
         properties.getKafka().getDataChannelTopic(),
         producer,
         dataChannelConsumer,
         zoneId,
         properties.getKafka().getNumPartitions(),
+        properties.getKafka().getPollingInterval(),
         properties.getChatroomBufferSize(),
         clock,
-        infoChannel,
+        channelMediator,
         shardingPublisherStrategy);
+    channelMediator.setDataChannel(dataChannel);
+    return dataChannel;
+  }
+
+  @Bean
+  ChannelMediator channelMediator()
+  {
+    return new ChannelMediator();
   }
 
   @Bean
@@ -302,4 +320,17 @@ public class KafkaServicesConfiguration
   {
     return ZoneId.systemDefault();
   }
+
+  @Bean
+  ChannelReactiveHealthIndicator dataChannelHealthIndicator(
+      DataChannel dataChannel)
+  {
+    return new ChannelReactiveHealthIndicator(dataChannel);
+  }
+
+  @Bean
+  ChannelReactiveHealthIndicator infoChannelHealthIndicator(InfoChannel infoChannel)
+  {
+    return new ChannelReactiveHealthIndicator(infoChannel);
+  }
 }