popular: 1.0.0 - Word are counted for hopping time-windows
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / popular / PopularApplicationConfiguriation.java
index 174521f..bee13e7 100644 (file)
@@ -1,10 +1,11 @@
-package de.juplo.kafka.wordcount.counter;
+package de.juplo.kafka.wordcount.popular;
 
 import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.clients.consumer.ConsumerConfig;
 import org.apache.kafka.streams.StreamsConfig;
 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
 import org.apache.kafka.streams.state.Stores;
+import org.apache.kafka.streams.state.WindowBytesStoreSupplier;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.ConfigurableApplicationContext;
@@ -13,21 +14,24 @@ import org.springframework.context.annotation.Configuration;
 import org.springframework.kafka.support.serializer.JsonDeserializer;
 import org.springframework.kafka.support.serializer.JsonSerde;
 
+import java.time.Duration;
+import java.time.ZoneId;
 import java.util.Properties;
 import java.util.concurrent.CompletableFuture;
 
-import static de.juplo.kafka.wordcount.counter.CounterStreamProcessor.STORE_NAME;
+import static de.juplo.kafka.wordcount.popular.PopularStreamProcessor.KEY_VALUE_STORE_NAME;
+import static de.juplo.kafka.wordcount.popular.PopularStreamProcessor.WINDOW_STORE_NAME;
 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
 
 
 @Configuration
-@EnableConfigurationProperties(CounterApplicationProperties.class)
+@EnableConfigurationProperties(PopularApplicationProperties.class)
 @Slf4j
-public class CounterApplicationConfiguriation
+public class PopularApplicationConfiguriation
 {
        @Bean
        public Properties streamProcessorProperties(
-                       CounterApplicationProperties counterProperties)
+                       PopularApplicationProperties counterProperties)
        {
                Properties propertyMap = serializationConfig();
 
@@ -50,23 +54,27 @@ public class CounterApplicationConfiguriation
 
                propertyMap.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
                propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
-               propertyMap.put(JsonDeserializer.TRUSTED_PACKAGES, CounterApplication.class.getPackageName());
+               propertyMap.put(JsonDeserializer.TRUSTED_PACKAGES, PopularApplication.class.getPackageName());
 
                return propertyMap;
        }
 
        @Bean(initMethod = "start", destroyMethod = "stop")
-       public CounterStreamProcessor streamProcessor(
-                       CounterApplicationProperties applicationProperties,
+       public PopularStreamProcessor streamProcessor(
+                       PopularApplicationProperties applicationProperties,
                        Properties streamProcessorProperties,
-                       KeyValueBytesStoreSupplier storeSupplier,
+                       ZoneId zone,
+                       KeyValueBytesStoreSupplier keyValueBytesStoreSupplier,
+                       WindowBytesStoreSupplier windowBytesStoreSupplier,
                        ConfigurableApplicationContext context)
        {
-               CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
+               PopularStreamProcessor streamProcessor = new PopularStreamProcessor(
                                applicationProperties.getInputTopic(),
                                applicationProperties.getOutputTopic(),
                                streamProcessorProperties,
-                               storeSupplier);
+                               zone,
+                               windowBytesStoreSupplier,
+                               keyValueBytesStoreSupplier);
 
                streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
                {
@@ -84,8 +92,24 @@ public class CounterApplicationConfiguriation
        }
 
        @Bean
-       public KeyValueBytesStoreSupplier storeSupplier()
+       public ZoneId defaultZone()
        {
-               return Stores.persistentKeyValueStore(STORE_NAME);
+               return ZoneId.systemDefault();
+       }
+
+       @Bean
+       public WindowBytesStoreSupplier windowBytesStoreSupplier()
+       {
+               return Stores.persistentWindowStore(
+                               KEY_VALUE_STORE_NAME,
+                               Duration.ofSeconds(60),
+                               Duration.ofSeconds(30),
+                               true);
+       }
+
+       @Bean
+       public KeyValueBytesStoreSupplier keyValueBytesStoreSupplier()
+       {
+               return Stores.persistentKeyValueStore(WINDOW_STORE_NAME);
        }
 }