1 package de.juplo.kafka.wordcount.query;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.ConsumerConfig;
5 import org.apache.kafka.streams.StreamsConfig;
6 import org.apache.kafka.streams.state.HostInfo;
7 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
8 import org.apache.kafka.streams.state.Stores;
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 import org.springframework.kafka.support.serializer.JsonDeserializer;
16 import org.springframework.kafka.support.serializer.JsonSerde;
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.RANKING_STORE_NAME;
25 import static de.juplo.kafka.wordcount.query.QueryStreamProcessor.USER_STORE_NAME;
26 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
30 @EnableConfigurationProperties(QueryApplicationProperties.class)
32 public class QueryApplicationConfiguration
35 public HostInfo applicationServer(
36 ServerProperties serverProperties,
37 QueryApplicationProperties applicationProperties) throws IOException
40 if (serverProperties.getAddress() == null)
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();
49 host = serverProperties.getAddress().getHostAddress();
52 Integer port = serverProperties.getPort() == null ? 8080 : serverProperties.getPort();
54 return new HostInfo(host, port);
58 public Properties streamProcessorProperties(
59 QueryApplicationProperties applicationProperties,
60 HostInfo applicationServer)
62 Properties props = new Properties();
64 props.putAll(serializationConfig());
66 String applicationId = applicationProperties.getApplicationId();
67 String bootstrapServer = applicationProperties.getBootstrapServer();
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);
73 props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, applicationProperties.getBootstrapServer());
74 if (applicationProperties.getCommitInterval() != null)
75 props.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, applicationProperties.getCommitInterval());
76 if (applicationProperties.getCacheMaxBytes() != null)
77 props.put(StreamsConfig.STATESTORE_CACHE_MAX_BYTES_CONFIG, applicationProperties.getCacheMaxBytes());
79 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
84 static Properties serializationConfig()
86 Properties props = new Properties();
88 props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
89 props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
91 JsonDeserializer.TYPE_MAPPINGS,
92 "user:" + Key.class.getName() + "," +
93 "ranking:" + Ranking.class.getName() + "," +
94 "userranking:" + UserRanking.class.getName());
99 @Bean(initMethod = "start", destroyMethod = "stop")
100 public QueryStreamProcessor streamProcessor(
101 Properties streamProcessorProperties,
102 HostInfo applicationServer,
103 QueryApplicationProperties applicationProperties,
104 KeyValueBytesStoreSupplier userStoreSupplier,
105 KeyValueBytesStoreSupplier rankingStoreSupplier,
106 ConfigurableApplicationContext context)
108 QueryStreamProcessor streamProcessor = new QueryStreamProcessor(
109 streamProcessorProperties,
111 applicationProperties.getUsersInputTopic(),
112 applicationProperties.getRankingInputTopic(),
114 rankingStoreSupplier);
116 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
118 log.error("Unexpected error!", e);
119 CompletableFuture.runAsync(() ->
121 log.info("Stopping application...");
122 SpringApplication.exit(context, () -> 1);
124 return SHUTDOWN_CLIENT;
127 return streamProcessor;
131 public KeyValueBytesStoreSupplier userStoreSupplier()
133 return Stores.persistentKeyValueStore(USER_STORE_NAME);
137 public KeyValueBytesStoreSupplier rankingStoreSupplier()
139 return Stores.persistentKeyValueStore(RANKING_STORE_NAME);