query: 1.0.6 - Refined `QueryApplicationConfiguration`
authorKai Moritz <kai@juplo.de>
Sun, 9 Jun 2024 08:29:23 +0000 (10:29 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 9 Jun 2024 19:25:15 +0000 (21:25 +0200)
--
moved lifecycle-handling of the streams-processor into the config-class

src/main/java/de/juplo/kafka/wordcount/query/QueryApplicationConfiguration.java
src/main/java/de/juplo/kafka/wordcount/query/QueryStreamProcessor.java

index 0e30e74..b546b8c 100644 (file)
@@ -1,10 +1,12 @@
 package de.juplo.kafka.wordcount.query;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.clients.consumer.ConsumerConfig;
 import org.apache.kafka.common.serialization.Serdes;
 import org.apache.kafka.streams.StreamsConfig;
 import org.apache.kafka.streams.state.HostInfo;
+import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.web.ServerProperties;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.ConfigurableApplicationContext;
@@ -15,10 +17,14 @@ import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+
+import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
 
 
 @Configuration
 @EnableConfigurationProperties(QueryApplicationProperties.class)
+@Slf4j
 public class QueryApplicationConfiguration
 {
        @Bean
@@ -74,7 +80,7 @@ public class QueryApplicationConfiguration
                return props;
        }
 
-       @Bean
+       @Bean(initMethod = "start", destroyMethod = "stop")
        public QueryStreamProcessor streamProcessor(
                        Properties streamProcessorProperties,
                        HostInfo applicationServer,
@@ -82,12 +88,24 @@ public class QueryApplicationConfiguration
                        ObjectMapper mapper,
                        ConfigurableApplicationContext context)
        {
-               return new QueryStreamProcessor(
+               QueryStreamProcessor streamProcessor = new QueryStreamProcessor(
                                streamProcessorProperties,
                                applicationServer,
                                applicationProperties.getUsersInputTopic(),
                                applicationProperties.getRankingInputTopic(),
-                               mapper,
-                               context);
+                               mapper);
+
+               streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
+               {
+                       log.error("Unexpected error!", e);
+                       CompletableFuture.runAsync(() ->
+                       {
+                               log.info("Stopping application...");
+                               SpringApplication.exit(context, () -> 1);
+                       });
+                       return SHUTDOWN_CLIENT;
+               });
+
+               return streamProcessor;
        }
 }
index 886c8cf..2d3d4c8 100644 (file)
@@ -16,15 +16,10 @@ import org.apache.kafka.streams.kstream.Materialized;
 import org.apache.kafka.streams.state.HostInfo;
 import org.apache.kafka.streams.state.QueryableStoreTypes;
 import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
-import org.springframework.boot.SpringApplication;
-import org.springframework.context.ConfigurableApplicationContext;
 
 import java.net.URI;
 import java.util.Optional;
 import java.util.Properties;
-import java.util.concurrent.CompletableFuture;
-
-import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
 
 
 @Slf4j
@@ -42,8 +37,7 @@ public class QueryStreamProcessor
                        HostInfo applicationServer,
                        String usersInputTopic,
                        String rankingInputTopic,
-                       ObjectMapper mapper,
-                       ConfigurableApplicationContext context)
+                       ObjectMapper mapper)
        {
                StreamsBuilder builder = new StreamsBuilder();
 
@@ -72,17 +66,6 @@ public class QueryStreamProcessor
                                .toTable(Materialized.as(storeName));
 
                streams = new KafkaStreams(builder.build(), props);
-               streams.setUncaughtExceptionHandler((Throwable e) ->
-               {
-                       log.error("Unexpected error!", e);
-                       CompletableFuture.runAsync(() ->
-                       {
-                               log.info("Stopping application...");
-                               SpringApplication.exit(context, () -> 1);
-                       });
-                       return SHUTDOWN_CLIENT;
-               });
-
                hostInfo = applicationServer;
                storeParameters = StoreQueryParameters.fromNameAndType(storeName, QueryableStoreTypes.keyValueStore());;
                this.mapper = mapper;