e2a55c0d74c9ee0ffae1fbca4ca70db19fe84e8b
[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(CounterApplicationProperties counterProperties)
29         {
30                 Properties propertyMap = new Properties();
31
32                 propertyMap.put(StreamsConfig.APPLICATION_ID_CONFIG, counterProperties.getApplicationId());
33                 propertyMap.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, counterProperties.getBootstrapServer());
34                 propertyMap.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
35                 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
36                 propertyMap.put(JsonDeserializer.TRUSTED_PACKAGES, CounterApplication.class.getPackageName());
37                 propertyMap.put(JsonDeserializer.KEY_DEFAULT_TYPE, Word.class.getName());
38                 propertyMap.put(JsonDeserializer.VALUE_DEFAULT_TYPE, Word.class.getName());
39                 propertyMap.put(JsonDeserializer.REMOVE_TYPE_INFO_HEADERS, Boolean.FALSE);
40                 propertyMap.put(StreamsConfig.STATE_DIR_CONFIG, "target");
41                 if (counterProperties.getCommitInterval() != null)
42                         propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, counterProperties.getCommitInterval());
43                 if (counterProperties.getCacheMaxBytes() != null)
44                         propertyMap.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, counterProperties.getCacheMaxBytes());
45                 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
46
47                 return propertyMap;
48         }
49
50         @Bean(initMethod = "start", destroyMethod = "stop")
51         public CounterStreamProcessor streamProcessor(
52                         CounterApplicationProperties applicationProperties,
53                         Properties streamProcessorProperties,
54                         KeyValueBytesStoreSupplier storeSupplier,
55                         ConfigurableApplicationContext context)
56         {
57                 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
58                                 applicationProperties.getInputTopic(),
59                                 applicationProperties.getOutputTopic(),
60                                 streamProcessorProperties,
61                                 storeSupplier);
62
63                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
64                 {
65                         log.error("Unexpected error!", e);
66                         CompletableFuture.runAsync(() ->
67                         {
68                                 log.info("Stopping application...");
69                                 SpringApplication.exit(context, () -> 1);
70                         });
71                         return SHUTDOWN_CLIENT;
72                 });
73
74
75                 return streamProcessor;
76         }
77
78         @Bean
79         public KeyValueBytesStoreSupplier storeSupplier()
80         {
81                 return Stores.persistentKeyValueStore("counter");
82         }
83 }