top10: 1.2.1 - (RED) Fixed de-/serialization, turned of caching in IT
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / top10 / Top10ApplicationConfiguration.java
1 package de.juplo.kafka.wordcount.top10;
2
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;
14
15 import java.util.Properties;
16 import java.util.concurrent.CompletableFuture;
17
18 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
19
20
21 @Configuration
22 @EnableConfigurationProperties(Top10ApplicationProperties.class)
23 @Slf4j
24 public class Top10ApplicationConfiguration
25 {
26         @Bean
27         public Properties streamProcessorProperties(Top10ApplicationProperties properties)
28         {
29                 Properties props = new Properties();
30
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(JsonDeserializer.TRUSTED_PACKAGES, Top10Application.class.getPackageName());
36                 props.put(JsonDeserializer.KEY_DEFAULT_TYPE, User.class.getName());
37                 props.put(JsonDeserializer.VALUE_DEFAULT_TYPE, Ranking.class.getName());
38                 props.put(
39                                 JsonDeserializer.TYPE_MAPPINGS,
40                                 "word:" + Key.class.getName() + "," +
41                                 "counter:" + Entry.class.getName() + "," +
42                                 "user:" + User.class.getName() + "," +
43                                 "ranking:" + Ranking.class.getName());
44                 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
45                 if (properties.getCommitInterval() != null)
46                         props.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, properties.getCommitInterval());
47                 if (properties.getCacheMaxBytes() != null)
48                         props.put(StreamsConfig.STATESTORE_CACHE_MAX_BYTES_CONFIG, properties.getCacheMaxBytes());
49                 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
50
51                 return props;
52         }
53
54         @Bean(initMethod = "start", destroyMethod = "stop")
55         public Top10StreamProcessor streamProcessor(
56                         Top10ApplicationProperties applicationProperties,
57                         Properties streamProcessorProperties,
58                         ConfigurableApplicationContext context)
59         {
60                 Top10StreamProcessor streamProcessor = new Top10StreamProcessor(
61                                 applicationProperties.getInputTopic(),
62                                 applicationProperties.getOutputTopic(),
63                                 streamProcessorProperties);
64
65                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
66                 {
67                         log.error("Unexpected error!", e);
68                         CompletableFuture.runAsync(() ->
69                         {
70                                 log.info("Stopping application...");
71                                 SpringApplication.exit(context, () -> 1);
72                         });
73                         return SHUTDOWN_CLIENT;
74                 });
75
76                 return streamProcessor;
77         }
78 }