query: 2.0.0 - (RED) The keys of the top10-topic are deserialized as JSON
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / query / QueryApplicationConfiguration.java
1 package de.juplo.kafka.wordcount.query;
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.HostInfo;
7 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
8 import org.apache.kafka.streams.state.Stores;
9 import org.springframework.boot.SpringApplication;
10 import org.springframework.boot.autoconfigure.web.ServerProperties;
11 import org.springframework.boot.context.properties.EnableConfigurationProperties;
12 import org.springframework.context.ConfigurableApplicationContext;
13 import org.springframework.context.annotation.Bean;
14 import org.springframework.context.annotation.Configuration;
15 import org.springframework.kafka.support.serializer.JsonDeserializer;
16 import org.springframework.kafka.support.serializer.JsonSerde;
17
18 import java.io.IOException;
19 import java.net.InetSocketAddress;
20 import java.net.Socket;
21 import java.util.Properties;
22 import java.util.concurrent.CompletableFuture;
23
24 import static de.juplo.kafka.wordcount.query.QueryStreamProcessor.RANKING_STORE_NAME;
25 import static de.juplo.kafka.wordcount.query.QueryStreamProcessor.USER_STORE_NAME;
26 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
27
28
29 @Configuration
30 @EnableConfigurationProperties(QueryApplicationProperties.class)
31 @Slf4j
32 public class QueryApplicationConfiguration
33 {
34         @Bean
35         public HostInfo applicationServer(
36                         ServerProperties serverProperties,
37                         QueryApplicationProperties applicationProperties) throws IOException
38         {
39                 String host;
40                 if (serverProperties.getAddress() == null)
41                 {
42                         HostInfo bootstrapServer = HostInfo.buildFromEndpoint(applicationProperties.getBootstrapServer());
43                         Socket socket = new Socket();
44                         socket.connect(new InetSocketAddress(bootstrapServer.host(), bootstrapServer.port()));
45                         host = socket.getLocalAddress().getHostAddress();
46                 }
47                 else
48                 {
49                         host = serverProperties.getAddress().getHostAddress();
50                 }
51
52                 Integer port = serverProperties.getPort() == null ? 8080 : serverProperties.getPort();
53
54                 return new HostInfo(host, port);
55         }
56
57         @Bean
58         public Properties streamProcessorProperties(
59                         QueryApplicationProperties applicationProperties,
60                         HostInfo applicationServer)
61         {
62                 Properties props = new Properties();
63
64                 props.putAll(serializationConfig());
65
66                 String applicationId = applicationProperties.getApplicationId();
67                 String bootstrapServer = applicationProperties.getBootstrapServer();
68
69                 props.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId);
70                 props.put(StreamsConfig.APPLICATION_SERVER_CONFIG, applicationServer.host() + ":" + applicationServer.port());
71                 props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
72
73                 props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, applicationProperties.getBootstrapServer());
74                 if (applicationProperties.getCommitInterval() != null)
75                         props.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, applicationProperties.getCommitInterval());
76                 if (applicationProperties.getCacheMaxBytes() != null)
77                         props.put(StreamsConfig.STATESTORE_CACHE_MAX_BYTES_CONFIG, applicationProperties.getCacheMaxBytes());
78
79                 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
80
81                 return props;
82         }
83
84         static Properties serializationConfig()
85         {
86                 Properties props = new Properties();
87
88                 props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
89                 props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
90                 props.put(
91                                 JsonDeserializer.TYPE_MAPPINGS,
92                                 "user:" + Key.class.getName() + "," +
93                                 "ranking:" + Ranking.class.getName() + "," +
94                                 "userranking:" + UserRanking.class.getName());
95
96                 return props;
97         }
98
99         @Bean(initMethod = "start", destroyMethod = "stop")
100         public QueryStreamProcessor streamProcessor(
101                         Properties streamProcessorProperties,
102                         HostInfo applicationServer,
103                         QueryApplicationProperties applicationProperties,
104                         KeyValueBytesStoreSupplier userStoreSupplier,
105                         KeyValueBytesStoreSupplier rankingStoreSupplier,
106                         ConfigurableApplicationContext context)
107         {
108                 QueryStreamProcessor streamProcessor = new QueryStreamProcessor(
109                                 streamProcessorProperties,
110                                 applicationServer,
111                                 applicationProperties.getUsersInputTopic(),
112                                 applicationProperties.getRankingInputTopic(),
113                                 userStoreSupplier,
114                                 rankingStoreSupplier);
115
116                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
117                 {
118                         log.error("Unexpected error!", e);
119                         CompletableFuture.runAsync(() ->
120                         {
121                                 log.info("Stopping application...");
122                                 SpringApplication.exit(context, () -> 1);
123                         });
124                         return SHUTDOWN_CLIENT;
125                 });
126
127                 return streamProcessor;
128         }
129
130         @Bean
131         public KeyValueBytesStoreSupplier userStoreSupplier()
132         {
133                 return Stores.persistentKeyValueStore(USER_STORE_NAME);
134         }
135
136         @Bean
137         public KeyValueBytesStoreSupplier rankingStoreSupplier()
138         {
139                 return Stores.persistentKeyValueStore(RANKING_STORE_NAME);
140         }
141 }