7b10ccdfbc64a00825a9f4d710a85ad3f85a423e
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / top10 / Top10ApplicationConfiguration.java
1 package de.juplo.kafka.wordcount.top10;
2
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.streams.StreamsConfig;
7 import org.springframework.boot.SpringApplication;
8 import org.springframework.boot.context.properties.EnableConfigurationProperties;
9 import org.springframework.context.ConfigurableApplicationContext;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.Configuration;
12 import org.springframework.kafka.support.serializer.JsonDeserializer;
13 import org.springframework.kafka.support.serializer.JsonSerde;
14 import org.springframework.kafka.support.serializer.JsonSerializer;
15
16 import java.util.Properties;
17 import java.util.concurrent.CompletableFuture;
18
19 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
20
21
22 @Configuration
23 @EnableConfigurationProperties(Top10ApplicationProperties.class)
24 @Slf4j
25 public class Top10ApplicationConfiguration
26 {
27         @Bean
28         public Properties streamProcessorProperties(Top10ApplicationProperties properties)
29         {
30                 Properties props = new Properties();
31
32                 props.put(StreamsConfig.APPLICATION_ID_CONFIG, properties.getApplicationId());
33                 props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServer());
34                 props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
35                 props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
36                 props.put(JsonSerializer.ADD_TYPE_INFO_HEADERS, false);
37                 props.put(JsonDeserializer.TRUSTED_PACKAGES, Word.class.getPackageName());
38                 props.put(JsonDeserializer.KEY_DEFAULT_TYPE, Word.class.getName());
39                 props.put(JsonDeserializer.VALUE_DEFAULT_TYPE, WordCount.class.getName());
40                 props.put(JsonDeserializer.USE_TYPE_INFO_HEADERS, false);
41                 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
42
43                 return props;
44         }
45
46         @Bean(initMethod = "start", destroyMethod = "stop")
47         public Top10StreamProcessor streamProcessor(
48                         Top10ApplicationProperties applicationProperties,
49                         ObjectMapper objectMapper,
50                         Properties streamProcessorProperties,
51                         ConfigurableApplicationContext context)
52         {
53                 Top10StreamProcessor streamProcessor = new Top10StreamProcessor(
54                                 applicationProperties.getInputTopic(),
55                                 applicationProperties.getOutputTopic(),
56                                 objectMapper,
57                                 streamProcessorProperties);
58
59                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
60                 {
61                         log.error("Unexpected error!", e);
62                         CompletableFuture.runAsync(() ->
63                         {
64                                 log.info("Stopping application...");
65                                 SpringApplication.exit(context, () -> 1);
66                         });
67                         return SHUTDOWN_CLIENT;
68                 });
69
70                 return streamProcessor;
71         }
72 }