1ee65ca16df5703d9c628e23993bd904647d24e1
[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(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
38
39                 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
40                                 properties.getInputTopic(),
41                                 properties.getOutputTopic(),
42                                 propertyMap,
43                                 objectMapper);
44
45                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
46                 {
47                         log.error("Unexpected error!", e);
48                         CompletableFuture.runAsync(() ->
49                         {
50                                 log.info("Stopping application...");
51                                 SpringApplication.exit(context, () -> 1);
52                         });
53                         return SHUTDOWN_CLIENT;
54                 });
55
56
57                 return streamProcessor;
58         }
59
60         public static void main(String[] args)
61         {
62                 SpringApplication.run(CounterApplication.class, args);
63         }
64 }