1bcc8348153f96a57763e92d86ede47e0234f760
[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(JsonDeserializer.TRUSTED_PACKAGES, Word.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(
42                                 JsonDeserializer.TYPE_MAPPINGS,
43                                 "W:" + Word.class.getName() + "," +
44                                 "C:" + WordCount.class.getName());
45                 propertyMap.put(StreamsConfig.STATE_DIR_CONFIG, "target");
46                 if (properties.getCommitInterval() != null)
47                         propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, properties.getCommitInterval());
48                 if (properties.getCacheMaxBytes() != null)
49                         propertyMap.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, properties.getCacheMaxBytes());
50                 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
51
52                 return propertyMap;
53         }
54
55         @Bean(initMethod = "start", destroyMethod = "stop")
56         public CounterStreamProcessor streamProcessor(
57                         CounterApplicationProperties properties,
58                         Properties propertyMap,
59                         KeyValueBytesStoreSupplier storeSupplier,
60                         ObjectMapper objectMapper,
61                         ConfigurableApplicationContext context)
62         {
63                 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
64                                 properties.getInputTopic(),
65                                 properties.getOutputTopic(),
66                                 propertyMap,
67                                 storeSupplier,
68                                 objectMapper);
69
70                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
71                 {
72                         log.error("Unexpected error!", e);
73                         CompletableFuture.runAsync(() ->
74                         {
75                                 log.info("Stopping application...");
76                                 SpringApplication.exit(context, () -> 1);
77                         });
78                         return SHUTDOWN_CLIENT;
79                 });
80
81
82                 return streamProcessor;
83         }
84
85         @Bean
86         public KeyValueBytesStoreSupplier storeSupplier()
87         {
88                 return Stores.persistentKeyValueStore("counter");
89         }
90 }