1 package de.juplo.kafka.wordcount.stats;
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.errors.LogAndContinueExceptionHandler;
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.stats.StatsStreamProcessor.RANKING_STORE_NAME;
26 import static de.juplo.kafka.wordcount.stats.StatsStreamProcessor.USER_STORE_NAME;
27 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
31 @EnableConfigurationProperties(StatsApplicationProperties.class)
33 public class StatsApplicationConfiguration
36 public HostInfo applicationServer(
37 ServerProperties serverProperties,
38 StatsApplicationProperties applicationProperties) throws IOException
41 if (serverProperties.getAddress() == null)
43 HostInfo bootstrapServer = HostInfo.buildFromEndpoint(applicationProperties.getBootstrapServer());
44 Socket socket = new Socket();
45 socket.connect(new InetSocketAddress(bootstrapServer.host(), bootstrapServer.port()));
46 host = socket.getLocalAddress().getHostAddress();
50 host = serverProperties.getAddress().getHostAddress();
53 Integer port = serverProperties.getPort() == null ? 8080 : serverProperties.getPort();
55 return new HostInfo(host, port);
59 public Properties streamProcessorProperties(
60 StatsApplicationProperties applicationProperties,
61 HostInfo applicationServer)
63 Properties props = new Properties();
65 props.putAll(serializationConfig());
67 String applicationId = applicationProperties.getApplicationId();
68 String bootstrapServer = applicationProperties.getBootstrapServer();
70 props.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId);
71 props.put(StreamsConfig.APPLICATION_SERVER_CONFIG, applicationServer.host() + ":" + applicationServer.port());
72 props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
74 props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, applicationProperties.getBootstrapServer());
75 if (applicationProperties.getCommitInterval() != null)
76 props.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, applicationProperties.getCommitInterval());
77 if (applicationProperties.getCacheMaxBytes() != null)
78 props.put(StreamsConfig.STATESTORE_CACHE_MAX_BYTES_CONFIG, applicationProperties.getCacheMaxBytes());
80 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
85 static Properties serializationConfig()
87 Properties props = new Properties();
90 StreamsConfig.DEFAULT_DESERIALIZATION_EXCEPTION_HANDLER_CLASS_CONFIG,
91 LogAndContinueExceptionHandler.class);
92 props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
93 props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
95 JsonDeserializer.TYPE_MAPPINGS,
96 "stats:" + Key.class.getName() + "," +
97 "ranking:" + Ranking.class.getName() + "," +
98 "userranking:" + UserRanking.class.getName());
103 @Bean(initMethod = "start", destroyMethod = "stop")
104 public StatsStreamProcessor streamProcessor(
105 Properties streamProcessorProperties,
106 HostInfo applicationServer,
107 StatsApplicationProperties applicationProperties,
108 KeyValueBytesStoreSupplier userStoreSupplier,
109 KeyValueBytesStoreSupplier rankingStoreSupplier,
110 ConfigurableApplicationContext context)
112 StatsStreamProcessor streamProcessor = new StatsStreamProcessor(
113 streamProcessorProperties,
115 applicationProperties.getUsersInputTopic(),
116 applicationProperties.getRankingInputTopic(),
118 rankingStoreSupplier);
120 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
122 log.error("Unexpected error!", e);
123 CompletableFuture.runAsync(() ->
125 log.info("Stopping application...");
126 SpringApplication.exit(context, () -> 1);
128 return SHUTDOWN_CLIENT;
131 return streamProcessor;
135 public KeyValueBytesStoreSupplier userStoreSupplier()
137 return Stores.persistentKeyValueStore(USER_STORE_NAME);
141 public KeyValueBytesStoreSupplier rankingStoreSupplier()
143 return Stores.persistentKeyValueStore(RANKING_STORE_NAME);