7da171227a67dcd2472b6908435a2a8acb6bd976
[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.common.serialization.Serdes;
6 import org.apache.kafka.streams.StreamsConfig;
7 import org.apache.kafka.streams.state.HostInfo;
8 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
9 import org.apache.kafka.streams.state.Stores;
10 import org.springframework.boot.SpringApplication;
11 import org.springframework.boot.autoconfigure.web.ServerProperties;
12 import org.springframework.boot.context.properties.EnableConfigurationProperties;
13 import org.springframework.context.ConfigurableApplicationContext;
14 import org.springframework.context.annotation.Bean;
15 import org.springframework.context.annotation.Configuration;
16 import org.springframework.kafka.support.serializer.JsonDeserializer;
17 import org.springframework.kafka.support.serializer.JsonSerde;
18
19 import java.io.IOException;
20 import java.net.InetSocketAddress;
21 import java.net.Socket;
22 import java.util.Properties;
23 import java.util.concurrent.CompletableFuture;
24
25 import static de.juplo.kafka.wordcount.query.QueryStreamProcessor.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                 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
73
74                 return props;
75         }
76
77         static Properties serializationConfig()
78         {
79                 Properties props = new Properties();
80
81                 props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
82                 props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
83                 props.put(JsonDeserializer.VALUE_DEFAULT_TYPE, User.class.getName()); // << Does not work without this!
84                 props.put(
85                                 JsonDeserializer.TYPE_MAPPINGS,
86                                 "user:" + Key.class.getName() + "," +
87                                 "ranking:" + Ranking.class.getName() + "," +
88                                 "userdata:" + User.class.getName() + "," +
89                                 "userranking:" + UserRanking.class.getName());
90
91                 return props;
92         }
93
94         @Bean(initMethod = "start", destroyMethod = "stop")
95         public QueryStreamProcessor streamProcessor(
96                         Properties streamProcessorProperties,
97                         HostInfo applicationServer,
98                         QueryApplicationProperties applicationProperties,
99                         KeyValueBytesStoreSupplier storeSupplier,
100                         ConfigurableApplicationContext context)
101         {
102                 QueryStreamProcessor streamProcessor = new QueryStreamProcessor(
103                                 streamProcessorProperties,
104                                 applicationServer,
105                                 applicationProperties.getUsersInputTopic(),
106                                 applicationProperties.getRankingInputTopic(),
107                                 storeSupplier);
108
109                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
110                 {
111                         log.error("Unexpected error!", e);
112                         CompletableFuture.runAsync(() ->
113                         {
114                                 log.info("Stopping application...");
115                                 SpringApplication.exit(context, () -> 1);
116                         });
117                         return SHUTDOWN_CLIENT;
118                 });
119
120                 return streamProcessor;
121         }
122
123         @Bean
124         public KeyValueBytesStoreSupplier storeSupplier()
125         {
126                 return Stores.persistentKeyValueStore(STORE_NAME);
127         }
128 }