counter: 1.3.0 - (RED) Introduced domain-class `User` as key
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / counter / CounterApplicationConfiguriation.java
1 package de.juplo.kafka.wordcount.counter;
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.springframework.boot.SpringApplication;
9 import org.springframework.boot.context.properties.EnableConfigurationProperties;
10 import org.springframework.context.ConfigurableApplicationContext;
11 import org.springframework.context.annotation.Bean;
12 import org.springframework.context.annotation.Configuration;
13 import org.springframework.kafka.support.serializer.JsonDeserializer;
14 import org.springframework.kafka.support.serializer.JsonSerde;
15
16 import java.util.Properties;
17 import java.util.concurrent.CompletableFuture;
18
19 import static de.juplo.kafka.wordcount.counter.CounterStreamProcessor.STORE_NAME;
20 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
21
22
23 @Configuration
24 @EnableConfigurationProperties(CounterApplicationProperties.class)
25 @Slf4j
26 public class CounterApplicationConfiguriation
27 {
28         @Bean
29         public Properties streamProcessorProperties(
30                         CounterApplicationProperties counterProperties)
31         {
32                 Properties propertyMap = serializationConfig();
33
34                 propertyMap.put(StreamsConfig.APPLICATION_ID_CONFIG, counterProperties.getApplicationId());
35
36                 propertyMap.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, counterProperties.getBootstrapServer());
37                 if (counterProperties.getCommitInterval() != null)
38                         propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, counterProperties.getCommitInterval());
39                 if (counterProperties.getCacheMaxBytes() != null)
40                         propertyMap.put(StreamsConfig.STATESTORE_CACHE_MAX_BYTES_CONFIG, counterProperties.getCacheMaxBytes());
41
42                 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
43
44                 return propertyMap;
45         }
46
47         static Properties serializationConfig()
48         {
49                 Properties propertyMap = new Properties();
50
51                 propertyMap.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
52                 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
53                 propertyMap.put(JsonDeserializer.KEY_DEFAULT_TYPE, User.class.getName());
54                 propertyMap.put(JsonDeserializer.VALUE_DEFAULT_TYPE, Word.class.getName());
55                 propertyMap.put(
56                                 JsonDeserializer.TYPE_MAPPINGS,
57                                 "user:" + User.class.getName() + "," +
58                                 "word:" + Word.class.getName() + "," +
59                                 "counter:" + WordCounter.class.getName());
60
61                 return propertyMap;
62         }
63
64         @Bean(initMethod = "start", destroyMethod = "stop")
65         public CounterStreamProcessor streamProcessor(
66                         CounterApplicationProperties applicationProperties,
67                         Properties streamProcessorProperties,
68                         KeyValueBytesStoreSupplier storeSupplier,
69                         ConfigurableApplicationContext context)
70         {
71                 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
72                                 applicationProperties.getInputTopic(),
73                                 applicationProperties.getOutputTopic(),
74                                 streamProcessorProperties,
75                                 storeSupplier);
76
77                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
78                 {
79                         log.error("Unexpected error!", e);
80                         CompletableFuture.runAsync(() ->
81                         {
82                                 log.info("Stopping application...");
83                                 SpringApplication.exit(context, () -> 1);
84                         });
85                         return SHUTDOWN_CLIENT;
86                 });
87
88
89                 return streamProcessor;
90         }
91
92         @Bean
93         public KeyValueBytesStoreSupplier storeSupplier()
94         {
95                 return Stores.persistentKeyValueStore(STORE_NAME);
96         }
97 }