1 package de.juplo.kafka.wordcount.top10;
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.KeyValueBytesStoreSupplier;
9 import org.springframework.boot.SpringApplication;
10 import org.springframework.boot.context.properties.EnableConfigurationProperties;
11 import org.springframework.context.ConfigurableApplicationContext;
12 import org.springframework.context.annotation.Bean;
13 import org.springframework.context.annotation.Configuration;
15 import java.util.Properties;
16 import java.util.concurrent.CompletableFuture;
18 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
22 @EnableConfigurationProperties(Top10ApplicationProperties.class)
24 public class Top10ApplicationConfiguration
27 public Properties streamProcessorProperties(Top10ApplicationProperties properties)
29 Properties props = new Properties();
31 props.put(StreamsConfig.APPLICATION_ID_CONFIG, properties.getApplicationId());
32 props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServer());
33 props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
34 props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
35 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
40 @Bean(initMethod = "start", destroyMethod = "stop")
41 public Top10StreamProcessor streamProcessor(
42 Top10ApplicationProperties applicationProperties,
43 ObjectMapper objectMapper,
44 Properties streamProcessorProperties,
45 ConfigurableApplicationContext context)
47 Top10StreamProcessor streamProcessor = new Top10StreamProcessor(
48 applicationProperties.getInputTopic(),
49 applicationProperties.getOutputTopic(),
51 streamProcessorProperties);
53 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
55 log.error("Unexpected error!", e);
56 CompletableFuture.runAsync(() ->
58 log.info("Stopping application...");
59 SpringApplication.exit(context, () -> 1);
61 return SHUTDOWN_CLIENT;
64 return streamProcessor;