package de.juplo.kafka.wordcount.query;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.kafka.streams.state.HostInfo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.annotation.Bean;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.Socket;
@SpringBootApplication
@EnableConfigurationProperties(QueryApplicationProperties.class)
public class QueryApplication
{
+ @Bean
+ public QueryStreamProcessor usersStreamProcessor(
+ ServerProperties serverProperties,
+ QueryApplicationProperties properties,
+ ObjectMapper mapper,
+ ConfigurableApplicationContext context) throws IOException
+ {
+ String host;
+ if (serverProperties.getAddress() == null)
+ {
+ HostInfo bootstrapServer = HostInfo.buildFromEndpoint(properties.getBootstrapServer());
+ Socket socket = new Socket();
+ socket.connect(new InetSocketAddress(bootstrapServer.host(), bootstrapServer.port()));
+ host = socket.getLocalAddress().getHostAddress();
+ }
+ else
+ {
+ host = serverProperties.getAddress().getHostAddress();
+ }
+
+ Integer port = serverProperties.getPort() == null ? 8080 : serverProperties.getPort();
+
+ return new QueryStreamProcessor(
+ properties.getApplicationId(),
+ new HostInfo(host, port),
+ properties.getBootstrapServer(),
+ properties.getUsersInputTopic(),
+ properties.getRankingInputTopic(),
+ mapper,
+ context);
+ }
+
+
public static void main(String[] args)
{
SpringApplication.run(QueryApplication.class, args);
import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
-import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Slf4j
-@Component
public class QueryStreamProcessor
{
public final KafkaStreams streams;
public QueryStreamProcessor(
- QueryApplicationProperties properties,
+ String applicationId,
+ HostInfo applicationServer,
+ String bootstrapServer,
+ String usersInputTopic,
+ String rankingInputTopic,
ObjectMapper mapper,
ConfigurableApplicationContext context)
{
StreamsBuilder builder = new StreamsBuilder();
- KTable<String, String> users = builder.table(properties.getUsersInputTopic());
- KStream<String, String> rankings = builder.stream(properties.getRankingInputTopic());
+ KTable<String, String> users = builder.table(usersInputTopic);
+ KStream<String, String> rankings = builder.stream(rankingInputTopic);
rankings
.join(users, (rankingJson, userJson) ->
.toTable(Materialized.as(storeName));
Properties props = new Properties();
- props.put(StreamsConfig.APPLICATION_ID_CONFIG, properties.getApplicationId());
- props.put(StreamsConfig.APPLICATION_SERVER_CONFIG, properties.getApplicationServer());
- props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServer());
+ props.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId);
+ props.put(StreamsConfig.APPLICATION_SERVER_CONFIG, applicationServer.host() + ":" + applicationServer.port());
+ props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
return SHUTDOWN_CLIENT;
});
- hostInfo = HostInfo.buildFromEndpoint(properties.getApplicationServer());
+ hostInfo = applicationServer;
storeParameters = StoreQueryParameters.fromNameAndType(storeName, QueryableStoreTypes.keyValueStore());;
this.mapper = mapper;
}