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