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