f1da4078b24ac301072d8182975541cfed1fc21b
[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.common.serialization.Serdes;
7 import org.apache.kafka.streams.StreamsConfig;
8 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
9 import org.apache.kafka.streams.state.Stores;
10 import org.springframework.boot.SpringApplication;
11 import org.springframework.boot.context.properties.EnableConfigurationProperties;
12 import org.springframework.context.ConfigurableApplicationContext;
13 import org.springframework.context.annotation.Bean;
14 import org.springframework.context.annotation.Configuration;
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 propertyMap(CounterApplicationProperties properties)
29         {
30                 Properties propertyMap = new Properties();
31
32                 propertyMap.put(StreamsConfig.APPLICATION_ID_CONFIG, properties.getApplicationId());
33                 propertyMap.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServer());
34                 propertyMap.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.StringSerde.class.getName());
35                 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.StringSerde.class.getName());
36                 propertyMap.put(StreamsConfig.STATE_DIR_CONFIG, "target");
37                 if (properties.getCommitInterval() != null)
38                         propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, properties.getCommitInterval());
39                 if (properties.getCacheMaxBytes() != null)
40                         propertyMap.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, properties.getCacheMaxBytes());
41                 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
42
43                 return propertyMap;
44         }
45
46         @Bean(initMethod = "start", destroyMethod = "stop")
47         public CounterStreamProcessor streamProcessor(
48                         CounterApplicationProperties properties,
49                         Properties propertyMap,
50                         KeyValueBytesStoreSupplier storeSupplier,
51                         ObjectMapper objectMapper,
52                         ConfigurableApplicationContext context)
53         {
54                 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
55                                 properties.getInputTopic(),
56                                 properties.getOutputTopic(),
57                                 propertyMap,
58                                 storeSupplier,
59                                 objectMapper);
60
61                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
62                 {
63                         log.error("Unexpected error!", e);
64                         CompletableFuture.runAsync(() ->
65                         {
66                                 log.info("Stopping application...");
67                                 SpringApplication.exit(context, () -> 1);
68                         });
69                         return SHUTDOWN_CLIENT;
70                 });
71
72
73                 return streamProcessor;
74         }
75
76         @Bean
77         public KeyValueBytesStoreSupplier storeSupplier()
78         {
79                 return Stores.persistentKeyValueStore("counter");
80         }
81 }