NEU
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / persistence / kafka / KafkaServicesConfiguration.java
index efc86b9..3b4bc16 100644 (file)
@@ -4,8 +4,6 @@ import de.juplo.kafka.chat.backend.ChatBackendProperties;
 import de.juplo.kafka.chat.backend.domain.ChatHome;
 import de.juplo.kafka.chat.backend.domain.ShardingStrategy;
 import de.juplo.kafka.chat.backend.persistence.KafkaLikeShardingStrategy;
-import jakarta.annotation.PreDestroy;
-import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.clients.consumer.Consumer;
 import org.apache.kafka.clients.consumer.ConsumerConfig;
 import org.apache.kafka.clients.consumer.KafkaConsumer;
@@ -16,21 +14,16 @@ import org.apache.kafka.common.serialization.IntegerDeserializer;
 import org.apache.kafka.common.serialization.IntegerSerializer;
 import org.apache.kafka.common.serialization.StringDeserializer;
 import org.apache.kafka.common.serialization.StringSerializer;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.ApplicationArguments;
-import org.springframework.boot.ApplicationRunner;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.kafka.support.serializer.JsonDeserializer;
 import org.springframework.kafka.support.serializer.JsonSerializer;
-import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
 import java.time.Clock;
 import java.time.ZoneId;
-import java.util.Properties;
-import java.util.concurrent.CompletableFuture;
+import java.util.HashMap;
+import java.util.Map;
 
 
 @ConditionalOnProperty(
@@ -38,61 +31,8 @@ import java.util.concurrent.CompletableFuture;
     name = "services",
     havingValue = "kafka")
 @Configuration
-@Slf4j
-public class KafkaServicesConfiguration implements ApplicationRunner
+public class KafkaServicesConfiguration
 {
-  @Autowired
-  ThreadPoolTaskExecutor taskExecutor;
-  @Autowired
-  ConfigurableApplicationContext context;
-
-  @Autowired
-  ChatMessageChannel chatMessageChannel;
-  @Autowired
-  ChatRoomChannel chatRoomChannel;
-
-  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(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("The consumer for the ChatMessageChannel exited abnormally!", e);
-          return null;
-        });
-  }
-
-  @PreDestroy
-  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("Waiting for the consumer of the ChatMessageChannel to finish its work");
-    chatMessageChannelConsumerJob.join();
-    log.info("Joined the consumer of the ChatMessageChannel");
-  }
-
-
   @Bean
   ChatHome kafkaChatHome(
       ShardingStrategy shardingStrategy,
@@ -128,7 +68,7 @@ public class KafkaServicesConfiguration implements ApplicationRunner
 
   @Bean
   Producer<Integer, ChatRoomTo>  chatRoomChannelProducer(
-      Properties defaultProducerProperties,
+      Map<String, String> defaultProducerProperties,
       IntegerSerializer integerSerializer,
       JsonSerializer<ChatRoomTo> chatRoomSerializer)
   {
@@ -153,12 +93,12 @@ public class KafkaServicesConfiguration implements ApplicationRunner
 
   @Bean
   Consumer<Integer, ChatRoomTo>  chatRoomChannelConsumer(
-      Properties defaultConsumerProperties,
+      Map<String, String> defaultConsumerProperties,
       IntegerDeserializer integerDeserializer,
       JsonDeserializer<ChatRoomTo> chatRoomDeserializer)
   {
-    Properties properties = new Properties(defaultConsumerProperties);
-    properties.setProperty(
+    Map<String, String> properties = new HashMap<>();
+    properties.put(
         ConsumerConfig.GROUP_ID_CONFIG,
         "chat_room_channel");
     return new KafkaConsumer<>(
@@ -203,7 +143,7 @@ public class KafkaServicesConfiguration implements ApplicationRunner
 
   @Bean
   Producer<String, MessageTo>  chatMessageChannelProducer(
-      Properties defaultProducerProperties,
+      Map<String, String> defaultProducerProperties,
       StringSerializer stringSerializer,
       JsonSerializer<MessageTo> messageSerializer)
   {
@@ -228,12 +168,12 @@ public class KafkaServicesConfiguration implements ApplicationRunner
 
   @Bean
   Consumer<String, MessageTo>  chatMessageChannelConsumer(
-      Properties defaultConsumerProperties,
+      Map<String, String> defaultConsumerProperties,
       StringDeserializer stringDeserializer,
       JsonDeserializer<MessageTo> messageDeserializer)
   {
-    Properties properties = new Properties(defaultConsumerProperties);
-    properties.setProperty(
+    Map<String, String> properties = new HashMap<>();
+    properties.put(
         ConsumerConfig.GROUP_ID_CONFIG,
         "chat_message_channel");
     return new KafkaConsumer<>(
@@ -256,35 +196,27 @@ public class KafkaServicesConfiguration implements ApplicationRunner
   }
 
   @Bean
-  Properties defaultProducerProperties(ChatBackendProperties chatBackendProperties)
+  Map<String, String> defaultProducerProperties(ChatBackendProperties chatBackendProperties)
   {
-    Properties properties = new Properties();
-    properties.setProperty(
+    return Map.of(
         ProducerConfig.CLIENT_ID_CONFIG,
-        chatBackendProperties.getKafka().getClientId());
-    properties.setProperty(
+        chatBackendProperties.getKafka().getClientId(),
         ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
         chatBackendProperties.getKafka().getBootstrapServers());
-    return properties;
   }
 
   @Bean
-  Properties defaultConsumerProperties(ChatBackendProperties chatBackendProperties)
+  Map<String, String> defaultConsumerProperties(ChatBackendProperties chatBackendProperties)
   {
-    Properties properties = new Properties();
-    properties.setProperty(
+    return Map.of(
         ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
-        chatBackendProperties.getKafka().getBootstrapServers());
-    properties.setProperty(
+        chatBackendProperties.getKafka().getBootstrapServers(),
         ConsumerConfig.CLIENT_ID_CONFIG,
-        chatBackendProperties.getKafka().getClientId());
-    properties.setProperty(
+        chatBackendProperties.getKafka().getClientId(),
         ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,
-        "false");
-    properties.setProperty(
+        "false",
         ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,
         "earliest");
-    return properties;
   }
 
   @Bean