counter: 1.1.4 - Introduced parameters for the config needed by the test
[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.springframework.boot.SpringApplication;
9 import org.springframework.boot.autoconfigure.SpringBootApplication;
10 import org.springframework.boot.context.properties.EnableConfigurationProperties;
11 import org.springframework.context.ConfigurableApplicationContext;
12 import org.springframework.context.annotation.Bean;
13
14 import java.util.Properties;
15 import java.util.concurrent.CompletableFuture;
16
17 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
18
19
20 @SpringBootApplication
21 @EnableConfigurationProperties(CounterApplicationProperties.class)
22 @Slf4j
23 public class CounterApplication
24 {
25         @Bean(initMethod = "start", destroyMethod = "stop")
26         public CounterStreamProcessor streamProcessor(
27                         CounterApplicationProperties properties,
28                         ObjectMapper objectMapper,
29                         ConfigurableApplicationContext context)
30         {
31                 Properties propertyMap = new Properties();
32
33                 propertyMap.put(StreamsConfig.APPLICATION_ID_CONFIG, properties.getApplicationId());
34                 propertyMap.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServer());
35                 propertyMap.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.StringSerde.class.getName());
36                 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.StringSerde.class.getName());
37                 propertyMap.put(StreamsConfig.STATE_DIR_CONFIG, "target");
38                 if (properties.getCommitInterval() != null)
39                         propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, properties.getCommitInterval());
40                 if (properties.getCacheMaxBytes() != null)
41                         propertyMap.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, properties.getCacheMaxBytes());
42                 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
43
44                 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
45                                 properties.getInputTopic(),
46                                 properties.getOutputTopic(),
47                                 propertyMap,
48                                 objectMapper);
49
50                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
51                 {
52                         log.error("Unexpected error!", e);
53                         CompletableFuture.runAsync(() ->
54                         {
55                                 log.info("Stopping application...");
56                                 SpringApplication.exit(context, () -> 1);
57                         });
58                         return SHUTDOWN_CLIENT;
59                 });
60
61
62                 return streamProcessor;
63         }
64
65         public static void main(String[] args)
66         {
67                 SpringApplication.run(CounterApplication.class, args);
68         }
69 }