counter: 1.2.14 - Set up type-mappings for JSON-Deserialization
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / counter / CounterApplicationConfiguriation.java
1 package de.juplo.kafka.wordcount.counter;
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.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
8 import org.apache.kafka.streams.state.Stores;
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;
14 import org.springframework.kafka.support.serializer.JsonDeserializer;
15 import org.springframework.kafka.support.serializer.JsonSerde;
16 import org.springframework.kafka.support.serializer.JsonSerializer;
17
18 import java.util.Properties;
19 import java.util.concurrent.CompletableFuture;
20
21 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
22
23
24 @Configuration
25 @EnableConfigurationProperties(CounterApplicationProperties.class)
26 @Slf4j
27 public class CounterApplicationConfiguriation
28 {
29         @Bean
30         public Properties propertyMap(CounterApplicationProperties properties)
31         {
32                 Properties propertyMap = new Properties();
33
34                 propertyMap.put(StreamsConfig.APPLICATION_ID_CONFIG, properties.getApplicationId());
35                 propertyMap.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServer());
36                 propertyMap.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
37                 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
38                 propertyMap.put(JsonSerializer.ADD_TYPE_INFO_HEADERS, false);
39                 propertyMap.put(JsonDeserializer.TRUSTED_PACKAGES, Word.class.getPackageName());
40                 propertyMap.put(JsonDeserializer.KEY_DEFAULT_TYPE, Word.class.getName());
41                 propertyMap.put(JsonDeserializer.VALUE_DEFAULT_TYPE, Word.class.getName());
42                 propertyMap.put(JsonDeserializer.USE_TYPE_INFO_HEADERS, false);
43                 propertyMap.put(StreamsConfig.STATE_DIR_CONFIG, "target");
44                 if (properties.getCommitInterval() != null)
45                         propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, properties.getCommitInterval());
46                 if (properties.getCacheMaxBytes() != null)
47                         propertyMap.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, properties.getCacheMaxBytes());
48                 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
49
50                 return propertyMap;
51         }
52
53         @Bean(initMethod = "start", destroyMethod = "stop")
54         public CounterStreamProcessor streamProcessor(
55                         CounterApplicationProperties properties,
56                         Properties propertyMap,
57                         KeyValueBytesStoreSupplier storeSupplier,
58                         ObjectMapper objectMapper,
59                         ConfigurableApplicationContext context)
60         {
61                 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
62                                 properties.getInputTopic(),
63                                 properties.getOutputTopic(),
64                                 propertyMap,
65                                 storeSupplier,
66                                 objectMapper);
67
68                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
69                 {
70                         log.error("Unexpected error!", e);
71                         CompletableFuture.runAsync(() ->
72                         {
73                                 log.info("Stopping application...");
74                                 SpringApplication.exit(context, () -> 1);
75                         });
76                         return SHUTDOWN_CLIENT;
77                 });
78
79
80                 return streamProcessor;
81         }
82
83         @Bean
84         public KeyValueBytesStoreSupplier storeSupplier()
85         {
86                 return Stores.persistentKeyValueStore("counter");
87         }
88 }