b546b8c5183e59b9cee5e533f12d7e518937c5bd
[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.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
16 import java.io.IOException;
17 import java.net.InetSocketAddress;
18 import java.net.Socket;
19 import java.util.Properties;
20 import java.util.concurrent.CompletableFuture;
21
22 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
23
24
25 @Configuration
26 @EnableConfigurationProperties(QueryApplicationProperties.class)
27 @Slf4j
28 public class QueryApplicationConfiguration
29 {
30         @Bean
31         public HostInfo applicationServer(
32                         ServerProperties serverProperties,
33                         QueryApplicationProperties applicationProperties) throws IOException
34         {
35                 String host;
36                 if (serverProperties.getAddress() == null)
37                 {
38                         HostInfo bootstrapServer = HostInfo.buildFromEndpoint(applicationProperties.getBootstrapServer());
39                         Socket socket = new Socket();
40                         socket.connect(new InetSocketAddress(bootstrapServer.host(), bootstrapServer.port()));
41                         host = socket.getLocalAddress().getHostAddress();
42                 }
43                 else
44                 {
45                         host = serverProperties.getAddress().getHostAddress();
46                 }
47
48                 Integer port = serverProperties.getPort() == null ? 8080 : serverProperties.getPort();
49
50                 return new HostInfo(host, port);
51         }
52
53         @Bean
54         public Properties streamProcessorProperties(
55                         QueryApplicationProperties applicationProperties,
56                         HostInfo applicationServer)
57         {
58                 Properties props = new Properties();
59
60                 props.putAll(serializationConfig());
61
62                 String applicationId = applicationProperties.getApplicationId();
63                 String bootstrapServer = applicationProperties.getBootstrapServer();
64
65                 props.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId);
66                 props.put(StreamsConfig.APPLICATION_SERVER_CONFIG, applicationServer.host() + ":" + applicationServer.port());
67                 props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
68                 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
69
70                 return props;
71         }
72
73         static Properties serializationConfig()
74         {
75                 Properties props = new Properties();
76
77                 props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
78                 props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
79
80                 return props;
81         }
82
83         @Bean(initMethod = "start", destroyMethod = "stop")
84         public QueryStreamProcessor streamProcessor(
85                         Properties streamProcessorProperties,
86                         HostInfo applicationServer,
87                         QueryApplicationProperties applicationProperties,
88                         ObjectMapper mapper,
89                         ConfigurableApplicationContext context)
90         {
91                 QueryStreamProcessor streamProcessor = new QueryStreamProcessor(
92                                 streamProcessorProperties,
93                                 applicationServer,
94                                 applicationProperties.getUsersInputTopic(),
95                                 applicationProperties.getRankingInputTopic(),
96                                 mapper);
97
98                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
99                 {
100                         log.error("Unexpected error!", e);
101                         CompletableFuture.runAsync(() ->
102                         {
103                                 log.info("Stopping application...");
104                                 SpringApplication.exit(context, () -> 1);
105                         });
106                         return SHUTDOWN_CLIENT;
107                 });
108
109                 return streamProcessor;
110         }
111 }