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.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;
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;
22 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
26 @EnableConfigurationProperties(QueryApplicationProperties.class)
28 public class QueryApplicationConfiguration
31 public HostInfo applicationServer(
32 ServerProperties serverProperties,
33 QueryApplicationProperties applicationProperties) throws IOException
36 if (serverProperties.getAddress() == null)
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();
45 host = serverProperties.getAddress().getHostAddress();
48 Integer port = serverProperties.getPort() == null ? 8080 : serverProperties.getPort();
50 return new HostInfo(host, port);
54 public Properties streamProcessorProperties(
55 QueryApplicationProperties applicationProperties,
56 HostInfo applicationServer)
58 Properties props = new Properties();
60 props.putAll(serializationConfig());
62 String applicationId = applicationProperties.getApplicationId();
63 String bootstrapServer = applicationProperties.getBootstrapServer();
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");
73 static Properties serializationConfig()
75 Properties props = new Properties();
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());
83 @Bean(initMethod = "start", destroyMethod = "stop")
84 public QueryStreamProcessor streamProcessor(
85 Properties streamProcessorProperties,
86 HostInfo applicationServer,
87 QueryApplicationProperties applicationProperties,
89 ConfigurableApplicationContext context)
91 QueryStreamProcessor streamProcessor = new QueryStreamProcessor(
92 streamProcessorProperties,
94 applicationProperties.getUsersInputTopic(),
95 applicationProperties.getRankingInputTopic(),
98 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
100 log.error("Unexpected error!", e);
101 CompletableFuture.runAsync(() ->
103 log.info("Stopping application...");
104 SpringApplication.exit(context, () -> 1);
106 return SHUTDOWN_CLIENT;
109 return streamProcessor;