counter: 1.2.15 - Removed logging of type-headers in tests
[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
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(CounterApplicationProperties.class)
24 @Slf4j
25 public class CounterApplicationConfiguriation
26 {
27         @Bean
28         public Properties streamProcessorProperties(
29                         CounterApplicationProperties counterProperties)
30         {
31                 Properties propertyMap = serializationConfig();
32
33                 propertyMap.put(StreamsConfig.APPLICATION_ID_CONFIG, counterProperties.getApplicationId());
34                 propertyMap.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, counterProperties.getBootstrapServer());
35                 if (counterProperties.getCommitInterval() != null)
36                         propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, counterProperties.getCommitInterval());
37                 if (counterProperties.getCacheMaxBytes() != null)
38                         propertyMap.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, counterProperties.getCacheMaxBytes());
39                 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
40
41                 return propertyMap;
42         }
43
44         static Properties serializationConfig()
45         {
46                 Properties propertyMap = new Properties();
47
48                 propertyMap.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
49                 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
50                 propertyMap.put(JsonDeserializer.KEY_DEFAULT_TYPE, Word.class.getName());
51                 propertyMap.put(JsonDeserializer.VALUE_DEFAULT_TYPE, Word.class.getName());
52                 propertyMap.put(
53                                 JsonDeserializer.TYPE_MAPPINGS,
54                                 "word:" + Word.class.getName() + "," +
55                                 "counter:" + WordCounter.class.getName());
56
57                 return propertyMap;
58         }
59
60         @Bean(initMethod = "start", destroyMethod = "stop")
61         public CounterStreamProcessor streamProcessor(
62                         CounterApplicationProperties applicationProperties,
63                         Properties streamProcessorProperties,
64                         KeyValueBytesStoreSupplier storeSupplier,
65                         ConfigurableApplicationContext context)
66         {
67                 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
68                                 applicationProperties.getInputTopic(),
69                                 applicationProperties.getOutputTopic(),
70                                 streamProcessorProperties,
71                                 storeSupplier);
72
73                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
74                 {
75                         log.error("Unexpected error!", e);
76                         CompletableFuture.runAsync(() ->
77                         {
78                                 log.info("Stopping application...");
79                                 SpringApplication.exit(context, () -> 1);
80                         });
81                         return SHUTDOWN_CLIENT;
82                 });
83
84
85                 return streamProcessor;
86         }
87
88         @Bean
89         public KeyValueBytesStoreSupplier storeSupplier()
90         {
91                 return Stores.persistentKeyValueStore("counter");
92         }
93 }