counter: 1.2.6 - Removed unused explicit references to `ObjectMapper`
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / counter / CounterApplicationConfiguriation.java
1 package de.juplo.kafka.wordcount.counter;
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.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
7 import org.apache.kafka.streams.state.Stores;
8 import org.springframework.boot.SpringApplication;
9 import org.springframework.boot.context.properties.EnableConfigurationProperties;
10 import org.springframework.context.ConfigurableApplicationContext;
11 import org.springframework.context.annotation.Bean;
12 import org.springframework.context.annotation.Configuration;
13 import org.springframework.kafka.support.serializer.JsonDeserializer;
14 import org.springframework.kafka.support.serializer.JsonSerde;
15 import org.springframework.kafka.support.serializer.JsonSerializer;
16
17 import java.util.Properties;
18 import java.util.concurrent.CompletableFuture;
19
20 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
21
22
23 @Configuration
24 @EnableConfigurationProperties(CounterApplicationProperties.class)
25 @Slf4j
26 public class CounterApplicationConfiguriation
27 {
28         @Bean
29         public Properties propertyMap(CounterApplicationProperties properties)
30         {
31                 Properties propertyMap = new Properties();
32
33                 propertyMap.put(StreamsConfig.APPLICATION_ID_CONFIG, properties.getApplicationId());
34                 propertyMap.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServer());
35                 propertyMap.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
36                 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
37                 propertyMap.put(JsonSerializer.ADD_TYPE_INFO_HEADERS, false);
38                 propertyMap.put(JsonDeserializer.TRUSTED_PACKAGES, CounterApplication.class.getPackageName());
39                 propertyMap.put(JsonDeserializer.KEY_DEFAULT_TYPE, Word.class.getName());
40                 propertyMap.put(JsonDeserializer.VALUE_DEFAULT_TYPE, Word.class.getName());
41                 propertyMap.put(JsonDeserializer.USE_TYPE_INFO_HEADERS, false);
42                 propertyMap.put(StreamsConfig.STATE_DIR_CONFIG, "target");
43                 if (properties.getCommitInterval() != null)
44                         propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, properties.getCommitInterval());
45                 if (properties.getCacheMaxBytes() != null)
46                         propertyMap.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, properties.getCacheMaxBytes());
47                 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
48
49                 return propertyMap;
50         }
51
52         @Bean(initMethod = "start", destroyMethod = "stop")
53         public CounterStreamProcessor streamProcessor(
54                         CounterApplicationProperties properties,
55                         Properties propertyMap,
56                         KeyValueBytesStoreSupplier storeSupplier,
57                         ConfigurableApplicationContext context)
58         {
59                 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
60                                 properties.getInputTopic(),
61                                 properties.getOutputTopic(),
62                                 propertyMap,
63                                 storeSupplier);
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
77                 return streamProcessor;
78         }
79
80         @Bean
81         public KeyValueBytesStoreSupplier storeSupplier()
82         {
83                 return Stores.persistentKeyValueStore("counter");
84         }
85 }