popular: 1.2.0 - Refined `WindowedWord` (timestamp as string)
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / popular / PopularApplicationConfiguriation.java
1 package de.juplo.kafka.wordcount.popular;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.ConsumerConfig;
5 import org.apache.kafka.streams.StreamsConfig;
6 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
7 import org.apache.kafka.streams.state.Stores;
8 import org.apache.kafka.streams.state.WindowBytesStoreSupplier;
9 import org.springframework.boot.SpringApplication;
10 import org.springframework.boot.context.properties.EnableConfigurationProperties;
11 import org.springframework.context.ConfigurableApplicationContext;
12 import org.springframework.context.annotation.Bean;
13 import org.springframework.context.annotation.Configuration;
14 import org.springframework.kafka.support.serializer.JsonDeserializer;
15 import org.springframework.kafka.support.serializer.JsonSerde;
16
17 import java.time.ZoneId;
18 import java.util.Properties;
19 import java.util.concurrent.CompletableFuture;
20
21 import static de.juplo.kafka.wordcount.popular.PopularStreamProcessor.*;
22 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
23
24
25 @Configuration
26 @EnableConfigurationProperties(PopularApplicationProperties.class)
27 @Slf4j
28 public class PopularApplicationConfiguriation
29 {
30         @Bean
31         public Properties streamProcessorProperties(
32                         PopularApplicationProperties counterProperties)
33         {
34                 Properties propertyMap = serializationConfig();
35
36                 propertyMap.put(StreamsConfig.APPLICATION_ID_CONFIG, counterProperties.getApplicationId());
37
38                 propertyMap.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, counterProperties.getBootstrapServer());
39                 if (counterProperties.getCommitInterval() != null)
40                         propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, counterProperties.getCommitInterval());
41                 if (counterProperties.getCacheMaxBytes() != null)
42                         propertyMap.put(StreamsConfig.STATESTORE_CACHE_MAX_BYTES_CONFIG, counterProperties.getCacheMaxBytes());
43
44                 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
45
46                 return propertyMap;
47         }
48
49         static Properties serializationConfig()
50         {
51                 Properties propertyMap = new Properties();
52
53                 propertyMap.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
54                 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
55                 propertyMap.put(JsonDeserializer.TRUSTED_PACKAGES, PopularApplication.class.getPackageName());
56
57                 return propertyMap;
58         }
59
60         @Bean(initMethod = "start", destroyMethod = "stop")
61         public PopularStreamProcessor streamProcessor(
62                         PopularApplicationProperties applicationProperties,
63                         Properties streamProcessorProperties,
64                         KeyValueBytesStoreSupplier keyValueBytesStoreSupplier,
65                         WindowBytesStoreSupplier windowBytesStoreSupplier,
66                         ConfigurableApplicationContext context)
67         {
68                 PopularStreamProcessor streamProcessor = new PopularStreamProcessor(
69                                 applicationProperties.getInputTopic(),
70                                 applicationProperties.getOutputTopic(),
71                                 streamProcessorProperties,
72                                 windowBytesStoreSupplier,
73                                 keyValueBytesStoreSupplier);
74
75                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
76                 {
77                         log.error("Unexpected error!", e);
78                         CompletableFuture.runAsync(() ->
79                         {
80                                 log.info("Stopping application...");
81                                 SpringApplication.exit(context, () -> 1);
82                         });
83                         return SHUTDOWN_CLIENT;
84                 });
85
86
87                 return streamProcessor;
88         }
89
90         @Bean
91         public WindowBytesStoreSupplier windowBytesStoreSupplier()
92         {
93                 return Stores.persistentWindowStore(
94                                 KEY_VALUE_STORE_NAME,
95                                 WINDOW_SIZE.multipliedBy(2),
96                                 WINDOW_SIZE,
97                                 false); // << Must always be `false` for normal use-cases!
98         }
99
100         @Bean
101         public KeyValueBytesStoreSupplier keyValueBytesStoreSupplier()
102         {
103                 return Stores.persistentKeyValueStore(WINDOW_STORE_NAME);
104         }
105 }