1 package de.juplo.kafka.wordcount.query;
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;
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;
24 import static de.juplo.kafka.wordcount.query.QueryStreamProcessor.STORE_NAME;
25 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
29 @EnableConfigurationProperties(QueryApplicationProperties.class)
31 public class QueryApplicationConfiguration
34 public HostInfo applicationServer(
35 ServerProperties serverProperties,
36 QueryApplicationProperties applicationProperties) throws IOException
39 if (serverProperties.getAddress() == null)
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();
48 host = serverProperties.getAddress().getHostAddress();
51 Integer port = serverProperties.getPort() == null ? 8080 : serverProperties.getPort();
53 return new HostInfo(host, port);
57 public Properties streamProcessorProperties(
58 QueryApplicationProperties applicationProperties,
59 HostInfo applicationServer)
61 Properties props = new Properties();
63 props.putAll(serializationConfig());
65 String applicationId = applicationProperties.getApplicationId();
66 String bootstrapServer = applicationProperties.getBootstrapServer();
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");
76 static Properties serializationConfig()
78 Properties props = new Properties();
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());
86 @Bean(initMethod = "start", destroyMethod = "stop")
87 public QueryStreamProcessor streamProcessor(
88 Properties streamProcessorProperties,
89 HostInfo applicationServer,
90 QueryApplicationProperties applicationProperties,
91 KeyValueBytesStoreSupplier storeSupplier,
93 ConfigurableApplicationContext context)
95 QueryStreamProcessor streamProcessor = new QueryStreamProcessor(
96 streamProcessorProperties,
98 applicationProperties.getUsersInputTopic(),
99 applicationProperties.getRankingInputTopic(),
103 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
105 log.error("Unexpected error!", e);
106 CompletableFuture.runAsync(() ->
108 log.info("Stopping application...");
109 SpringApplication.exit(context, () -> 1);
111 return SHUTDOWN_CLIENT;
114 return streamProcessor;
118 public KeyValueBytesStoreSupplier storeSupplier()
120 return Stores.persistentKeyValueStore(STORE_NAME);