query: 1.0.6 - Refined `QueryAppilcationConfiguration`
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / query / QueryStreamProcessor.java
index f7dc750..886c8cf 100644 (file)
@@ -2,10 +2,14 @@ package de.juplo.kafka.wordcount.query;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.kafka.clients.consumer.ConsumerConfig;
 import org.apache.kafka.common.serialization.Serdes;
-import org.apache.kafka.streams.*;
+import org.apache.kafka.streams.KafkaStreams;
+import org.apache.kafka.streams.KeyQueryMetadata;
+import org.apache.kafka.streams.StoreQueryParameters;
+import org.apache.kafka.streams.StreamsBuilder;
 import org.apache.kafka.streams.kstream.KStream;
 import org.apache.kafka.streams.kstream.KTable;
 import org.apache.kafka.streams.kstream.Materialized;
@@ -14,10 +18,7 @@ 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 org.springframework.stereotype.Component;
 
-import javax.annotation.PostConstruct;
-import javax.annotation.PreDestroy;
 import java.net.URI;
 import java.util.Optional;
 import java.util.Properties;
@@ -27,7 +28,6 @@ import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.St
 
 
 @Slf4j
-@Component
 public class QueryStreamProcessor
 {
        public final KafkaStreams streams;
@@ -38,14 +38,17 @@ public class QueryStreamProcessor
 
 
        public QueryStreamProcessor(
-                       QueryApplicationProperties properties,
+                       Properties props,
+                       HostInfo applicationServer,
+                       String usersInputTopic,
+                       String rankingInputTopic,
                        ObjectMapper mapper,
                        ConfigurableApplicationContext context)
        {
                StreamsBuilder builder = new StreamsBuilder();
 
-               KTable<String, String> users = builder.table(properties.getUsersInputTopic());
-               KStream<String, String> rankings = builder.stream(properties.getRankingInputTopic());
+               KTable<String, String> users = builder.table(usersInputTopic);
+               KStream<String, String> rankings = builder.stream(rankingInputTopic);
 
                rankings
                                .join(users, (rankingJson, userJson) ->
@@ -68,14 +71,6 @@ public class QueryStreamProcessor
                                })
                                .toTable(Materialized.as(storeName));
 
-               Properties props = new Properties();
-               props.put(StreamsConfig.APPLICATION_ID_CONFIG, properties.getApplicationId());
-               props.put(StreamsConfig.APPLICATION_SERVER_CONFIG, properties.getApplicationServer());
-               props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServer());
-               props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
-               props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
-               props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
-
                streams = new KafkaStreams(builder.build(), props);
                streams.setUncaughtExceptionHandler((Throwable e) ->
                {
@@ -88,7 +83,7 @@ public class QueryStreamProcessor
                        return SHUTDOWN_CLIENT;
                });
 
-               hostInfo = HostInfo.buildFromEndpoint(properties.getApplicationServer());
+               hostInfo = applicationServer;
                storeParameters = StoreQueryParameters.fromNameAndType(storeName, QueryableStoreTypes.keyValueStore());;
                this.mapper = mapper;
        }
@@ -99,7 +94,7 @@ public class QueryStreamProcessor
                HostInfo activeHost = metadata.activeHost();
                log.debug("Local store for {}: {}, {}:{}", username, metadata.partition(), activeHost.host(), activeHost.port());
 
-               if (activeHost.equals(this.hostInfo))
+               if (activeHost.equals(this.hostInfo) || activeHost.equals(HostInfo.unavailable()))
                {
                        return Optional.empty();
                }