From: Kai Moritz Date: Sun, 9 Jun 2024 08:29:23 +0000 (+0200) Subject: query: 1.0.6 - Refined `QueryApplicationConfiguration` X-Git-Tag: query-with-kafkaproducer~16 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=9f2fda277bdd947461dc5b6dd350c29944fb43c3;p=demos%2Fkafka%2Fwordcount query: 1.0.6 - Refined `QueryApplicationConfiguration` -- moved lifecycle-handling of the streams-processor into the config-class --- diff --git a/src/main/java/de/juplo/kafka/wordcount/query/QueryApplicationConfiguration.java b/src/main/java/de/juplo/kafka/wordcount/query/QueryApplicationConfiguration.java index 0e30e74..b546b8c 100644 --- a/src/main/java/de/juplo/kafka/wordcount/query/QueryApplicationConfiguration.java +++ b/src/main/java/de/juplo/kafka/wordcount/query/QueryApplicationConfiguration.java @@ -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; } } diff --git a/src/main/java/de/juplo/kafka/wordcount/query/QueryStreamProcessor.java b/src/main/java/de/juplo/kafka/wordcount/query/QueryStreamProcessor.java index 886c8cf..2d3d4c8 100644 --- a/src/main/java/de/juplo/kafka/wordcount/query/QueryStreamProcessor.java +++ b/src/main/java/de/juplo/kafka/wordcount/query/QueryStreamProcessor.java @@ -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;