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