1 package de.juplo.kafka.wordcount.query;
3 import com.fasterxml.jackson.databind.ObjectMapper;
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.springframework.boot.autoconfigure.web.ServerProperties;
9 import org.springframework.boot.context.properties.EnableConfigurationProperties;
10 import org.springframework.context.ConfigurableApplicationContext;
11 import org.springframework.context.annotation.Bean;
12 import org.springframework.context.annotation.Configuration;
14 import java.io.IOException;
15 import java.net.InetSocketAddress;
16 import java.net.Socket;
17 import java.util.Properties;
21 @EnableConfigurationProperties(QueryApplicationProperties.class)
22 public class QueryApplicationConfiguration
25 public HostInfo applicationServer(
26 ServerProperties serverProperties,
27 QueryApplicationProperties applicationProperties) throws IOException
30 if (serverProperties.getAddress() == null)
32 HostInfo bootstrapServer = HostInfo.buildFromEndpoint(applicationProperties.getBootstrapServer());
33 Socket socket = new Socket();
34 socket.connect(new InetSocketAddress(bootstrapServer.host(), bootstrapServer.port()));
35 host = socket.getLocalAddress().getHostAddress();
39 host = serverProperties.getAddress().getHostAddress();
42 Integer port = serverProperties.getPort() == null ? 8080 : serverProperties.getPort();
44 return new HostInfo(host, port);
48 public Properties streamProcessorProperties(
49 QueryApplicationProperties applicationProperties,
50 HostInfo applicationServer)
52 Properties props = new Properties();
54 String applicationId = applicationProperties.getApplicationId();
55 String bootstrapServer = applicationProperties.getBootstrapServer();
57 props.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId);
58 props.put(StreamsConfig.APPLICATION_SERVER_CONFIG, applicationServer.host() + ":" + applicationServer.port());
59 props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
60 props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
61 props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
62 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
68 public QueryStreamProcessor streamProcessor(
69 Properties streamProcessorProperties,
70 HostInfo applicationServer,
71 QueryApplicationProperties applicationProperties,
73 ConfigurableApplicationContext context)
75 return new QueryStreamProcessor(
76 streamProcessorProperties,
78 applicationProperties.getUsersInputTopic(),
79 applicationProperties.getRankingInputTopic(),