1 package de.juplo.kafka.wordcount.top10;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.ConsumerConfig;
5 import org.apache.kafka.streams.StreamsConfig;
6 import org.springframework.boot.SpringApplication;
7 import org.springframework.boot.context.properties.EnableConfigurationProperties;
8 import org.springframework.context.ConfigurableApplicationContext;
9 import org.springframework.context.annotation.Bean;
10 import org.springframework.context.annotation.Configuration;
11 import org.springframework.kafka.support.serializer.JsonDeserializer;
12 import org.springframework.kafka.support.serializer.JsonSerde;
13 import org.springframework.kafka.support.serializer.JsonSerializer;
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, JsonSerde.class.getName());
34 props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
35 props.put(JsonSerializer.ADD_TYPE_INFO_HEADERS, false);
36 props.put(JsonDeserializer.TRUSTED_PACKAGES, Top10Application.class.getPackageName());
37 props.put(JsonDeserializer.KEY_DEFAULT_TYPE, Word.class.getName());
38 props.put(JsonDeserializer.VALUE_DEFAULT_TYPE, Counter.class.getName());
39 props.put(JsonDeserializer.USE_TYPE_INFO_HEADERS, false);
40 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
45 @Bean(initMethod = "start", destroyMethod = "stop")
46 public Top10StreamProcessor streamProcessor(
47 Top10ApplicationProperties applicationProperties,
48 Properties streamProcessorProperties,
49 ConfigurableApplicationContext context)
51 Top10StreamProcessor streamProcessor = new Top10StreamProcessor(
52 applicationProperties.getInputTopic(),
53 applicationProperties.getOutputTopic(),
54 streamProcessorProperties);
56 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
58 log.error("Unexpected error!", e);
59 CompletableFuture.runAsync(() ->
61 log.info("Stopping application...");
62 SpringApplication.exit(context, () -> 1);
64 return SHUTDOWN_CLIENT;
67 return streamProcessor;