50a53645ceb860b9cdb1a8f555b2ac1fba48c623
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / query / QueryApplicationConfiguration.java
1 package de.juplo.kafka.wordcount.query;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import lombok.extern.slf4j.Slf4j;
5 import org.apache.kafka.clients.consumer.ConsumerConfig;
6 import org.apache.kafka.common.serialization.Serdes;
7 import org.apache.kafka.streams.StreamsConfig;
8 import org.apache.kafka.streams.state.HostInfo;
9 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
10 import org.apache.kafka.streams.state.Stores;
11 import org.springframework.boot.SpringApplication;
12 import org.springframework.boot.autoconfigure.web.ServerProperties;
13 import org.springframework.boot.context.properties.EnableConfigurationProperties;
14 import org.springframework.context.ConfigurableApplicationContext;
15 import org.springframework.context.annotation.Bean;
16 import org.springframework.context.annotation.Configuration;
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.STORE_NAME;
25 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
26
27
28 @Configuration
29 @EnableConfigurationProperties(QueryApplicationProperties.class)
30 @Slf4j
31 public class QueryApplicationConfiguration
32 {
33         @Bean
34         public HostInfo applicationServer(
35                         ServerProperties serverProperties,
36                         QueryApplicationProperties applicationProperties) throws IOException
37         {
38                 String host;
39                 if (serverProperties.getAddress() == null)
40                 {
41                         HostInfo bootstrapServer = HostInfo.buildFromEndpoint(applicationProperties.getBootstrapServer());
42                         Socket socket = new Socket();
43                         socket.connect(new InetSocketAddress(bootstrapServer.host(), bootstrapServer.port()));
44                         host = socket.getLocalAddress().getHostAddress();
45                 }
46                 else
47                 {
48                         host = serverProperties.getAddress().getHostAddress();
49                 }
50
51                 Integer port = serverProperties.getPort() == null ? 8080 : serverProperties.getPort();
52
53                 return new HostInfo(host, port);
54         }
55
56         @Bean
57         public Properties streamProcessorProperties(
58                         QueryApplicationProperties applicationProperties,
59                         HostInfo applicationServer)
60         {
61                 Properties props = new Properties();
62
63                 props.putAll(serializationConfig());
64
65                 String applicationId = applicationProperties.getApplicationId();
66                 String bootstrapServer = applicationProperties.getBootstrapServer();
67
68                 props.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId);
69                 props.put(StreamsConfig.APPLICATION_SERVER_CONFIG, applicationServer.host() + ":" + applicationServer.port());
70                 props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
71                 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
72
73                 return props;
74         }
75
76         static Properties serializationConfig()
77         {
78                 Properties props = new Properties();
79
80                 props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
81                 props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
82
83                 return props;
84         }
85
86         @Bean(initMethod = "start", destroyMethod = "stop")
87         public QueryStreamProcessor streamProcessor(
88                         Properties streamProcessorProperties,
89                         HostInfo applicationServer,
90                         QueryApplicationProperties applicationProperties,
91                         KeyValueBytesStoreSupplier storeSupplier,
92                         ObjectMapper mapper,
93                         ConfigurableApplicationContext context)
94         {
95                 QueryStreamProcessor streamProcessor = new QueryStreamProcessor(
96                                 streamProcessorProperties,
97                                 applicationServer,
98                                 applicationProperties.getUsersInputTopic(),
99                                 applicationProperties.getRankingInputTopic(),
100                                 storeSupplier,
101                                 mapper);
102
103                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
104                 {
105                         log.error("Unexpected error!", e);
106                         CompletableFuture.runAsync(() ->
107                         {
108                                 log.info("Stopping application...");
109                                 SpringApplication.exit(context, () -> 1);
110                         });
111                         return SHUTDOWN_CLIENT;
112                 });
113
114                 return streamProcessor;
115         }
116
117         @Bean
118         public KeyValueBytesStoreSupplier storeSupplier()
119         {
120                 return Stores.persistentKeyValueStore(STORE_NAME);
121         }
122 }