top10: 1.2.1 - (GREEN) Made the aggregation state accessible
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / top10 / Top10ApplicationConfiguration.java
1 package de.juplo.kafka.wordcount.top10;
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 import org.springframework.kafka.support.serializer.JsonSerializer;
16
17 import java.util.Properties;
18 import java.util.concurrent.CompletableFuture;
19
20 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
21
22
23 @Configuration
24 @EnableConfigurationProperties(Top10ApplicationProperties.class)
25 @Slf4j
26 public class Top10ApplicationConfiguration
27 {
28         @Bean
29         public Properties streamProcessorProperties(Top10ApplicationProperties properties)
30         {
31                 Properties props = new Properties();
32
33                 props.put(StreamsConfig.APPLICATION_ID_CONFIG, properties.getApplicationId());
34                 props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServer());
35                 props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
36                 props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
37                 props.put(JsonDeserializer.TRUSTED_PACKAGES, Top10Application.class.getPackageName());
38                 props.put(JsonDeserializer.KEY_DEFAULT_TYPE, User.class.getName());
39                 props.put(JsonDeserializer.VALUE_DEFAULT_TYPE, Ranking.class.getName());
40                 props.put(
41                                 JsonDeserializer.TYPE_MAPPINGS,
42                                 "word:" + Key.class.getName() + "," +
43                                 "counter:" + Entry.class.getName() + "," +
44                                 "user:" + User.class.getName() + "," +
45                                 "ranking:" + Ranking.class.getName());
46                 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
47                 if (properties.getCommitInterval() != null)
48                         props.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, properties.getCommitInterval());
49                 if (properties.getCacheMaxBytes() != null)
50                         props.put(StreamsConfig.STATESTORE_CACHE_MAX_BYTES_CONFIG, properties.getCacheMaxBytes());
51                 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
52
53                 return props;
54         }
55
56         @Bean(initMethod = "start", destroyMethod = "stop")
57         public Top10StreamProcessor streamProcessor(
58                         Top10ApplicationProperties applicationProperties,
59                         Properties streamProcessorProperties,
60                         KeyValueBytesStoreSupplier storeSupplier,
61                         ConfigurableApplicationContext context)
62         {
63                 Top10StreamProcessor streamProcessor = new Top10StreamProcessor(
64                                 applicationProperties.getInputTopic(),
65                                 applicationProperties.getOutputTopic(),
66                                 streamProcessorProperties,
67                                 storeSupplier);
68
69                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
70                 {
71                         log.error("Unexpected error!", e);
72                         CompletableFuture.runAsync(() ->
73                         {
74                                 log.info("Stopping application...");
75                                 SpringApplication.exit(context, () -> 1);
76                         });
77                         return SHUTDOWN_CLIENT;
78                 });
79
80                 return streamProcessor;
81         }
82
83         @Bean
84         public KeyValueBytesStoreSupplier storeSupplier()
85         {
86                 return Stores.persistentKeyValueStore("top10");
87         }
88 }