counter: 1.1.5 - Fixed a bug in the integration-test `CounterApplicationIT`
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / counter / CounterApplication.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.autoconfigure.SpringBootApplication;
12 import org.springframework.boot.context.properties.EnableConfigurationProperties;
13 import org.springframework.context.ConfigurableApplicationContext;
14 import org.springframework.context.annotation.Bean;
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 @SpringBootApplication
23 @EnableConfigurationProperties(CounterApplicationProperties.class)
24 @Slf4j
25 public class CounterApplication
26 {
27         @Bean(initMethod = "start", destroyMethod = "stop")
28         public CounterStreamProcessor streamProcessor(
29                         CounterApplicationProperties properties,
30                         KeyValueBytesStoreSupplier storeSupplier,
31                         ObjectMapper objectMapper,
32                         ConfigurableApplicationContext context)
33         {
34                 Properties propertyMap = new Properties();
35
36                 propertyMap.put(StreamsConfig.APPLICATION_ID_CONFIG, properties.getApplicationId());
37                 propertyMap.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServer());
38                 propertyMap.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.StringSerde.class.getName());
39                 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.StringSerde.class.getName());
40                 propertyMap.put(StreamsConfig.STATE_DIR_CONFIG, "target");
41                 if (properties.getCommitInterval() != null)
42                         propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, properties.getCommitInterval());
43                 if (properties.getCacheMaxBytes() != null)
44                         propertyMap.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, properties.getCacheMaxBytes());
45                 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
46
47                 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
48                                 properties.getInputTopic(),
49                                 properties.getOutputTopic(),
50                                 propertyMap,
51                                 storeSupplier,
52                                 objectMapper);
53
54                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
55                 {
56                         log.error("Unexpected error!", e);
57                         CompletableFuture.runAsync(() ->
58                         {
59                                 log.info("Stopping application...");
60                                 SpringApplication.exit(context, () -> 1);
61                         });
62                         return SHUTDOWN_CLIENT;
63                 });
64
65
66                 return streamProcessor;
67         }
68
69         @Bean
70         public KeyValueBytesStoreSupplier storeSupplier()
71         {
72                 return Stores.persistentKeyValueStore("counter");
73         }
74
75         public static void main(String[] args)
76         {
77                 SpringApplication.run(CounterApplication.class, args);
78         }
79 }