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.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;
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;
25 import static de.juplo.kafka.wordcount.query.QueryStreamProcessor.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);
72 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
77 static Properties serializationConfig()
79 Properties props = new Properties();
81 props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
82 props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
84 JsonDeserializer.TYPE_MAPPINGS,
85 "user:" + Key.class.getName() + "," +
86 "ranking:" + Ranking.class.getName() + "," +
87 "userdata:" + User.class.getName() + "," +
88 "userranking:" + UserRanking.class.getName());
93 @Bean(initMethod = "start", destroyMethod = "stop")
94 public QueryStreamProcessor streamProcessor(
95 Properties streamProcessorProperties,
96 HostInfo applicationServer,
97 QueryApplicationProperties applicationProperties,
98 KeyValueBytesStoreSupplier storeSupplier,
99 ConfigurableApplicationContext context)
101 QueryStreamProcessor streamProcessor = new QueryStreamProcessor(
102 streamProcessorProperties,
104 applicationProperties.getUsersInputTopic(),
105 applicationProperties.getRankingInputTopic(),
108 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
110 log.error("Unexpected error!", e);
111 CompletableFuture.runAsync(() ->
113 log.info("Stopping application...");
114 SpringApplication.exit(context, () -> 1);
116 return SHUTDOWN_CLIENT;
119 return streamProcessor;
123 public KeyValueBytesStoreSupplier storeSupplier()
125 return Stores.persistentKeyValueStore(STORE_NAME);