package de.juplo.kafka.wordcount.query;
import com.fasterxml.jackson.databind.ObjectMapper;
+import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.state.HostInfo;
+import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+
+import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
@Configuration
@EnableConfigurationProperties(QueryApplicationProperties.class)
+@Slf4j
public class QueryApplicationConfiguration
{
@Bean
return props;
}
- @Bean
+ @Bean(initMethod = "start", destroyMethod = "stop")
public QueryStreamProcessor usersStreamProcessor(
Properties streamProcessorProperties,
HostInfo applicationServer,
ObjectMapper mapper,
ConfigurableApplicationContext context)
{
- return new QueryStreamProcessor(
+ QueryStreamProcessor streamProcessor = new QueryStreamProcessor(
streamProcessorProperties,
applicationServer,
applicationProperties.getUsersInputTopic(),
applicationProperties.getRankingInputTopic(),
- mapper,
- context);
+ mapper);
+
+ streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
+ {
+ log.error("Unexpected error!", e);
+ CompletableFuture.runAsync(() ->
+ {
+ log.info("Stopping application...");
+ SpringApplication.exit(context, () -> 1);
+ });
+ return SHUTDOWN_CLIENT;
+ });
+
+ return streamProcessor;
}
}
import org.apache.kafka.streams.state.HostInfo;
import org.apache.kafka.streams.state.QueryableStoreTypes;
import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
-import org.springframework.boot.SpringApplication;
-import org.springframework.context.ConfigurableApplicationContext;
import java.net.URI;
import java.util.Optional;
import java.util.Properties;
-import java.util.concurrent.CompletableFuture;
-
-import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
@Slf4j
HostInfo applicationServer,
String usersInputTopic,
String rankingInputTopic,
- ObjectMapper mapper,
- ConfigurableApplicationContext context)
+ ObjectMapper mapper)
{
StreamsBuilder builder = new StreamsBuilder();
.toTable(Materialized.as(storeName));
streams = new KafkaStreams(builder.build(), props);
- streams.setUncaughtExceptionHandler((Throwable e) ->
- {
- log.error("Unexpected error!", e);
- CompletableFuture.runAsync(() ->
- {
- log.info("Stopping application...");
- SpringApplication.exit(context, () -> 1);
- });
- return SHUTDOWN_CLIENT;
- });
-
hostInfo = applicationServer;
storeParameters = StoreQueryParameters.fromNameAndType(storeName, QueryableStoreTypes.keyValueStore());;
this.mapper = mapper;