splitter: 1.1.5 - Updated Spring Boot to `3.2.5`
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / splitter / SplitterApplication.java
1 package de.juplo.kafka.wordcount.splitter;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.ConsumerConfig;
5 import org.apache.kafka.common.serialization.Serdes;
6 import org.apache.kafka.streams.StreamsConfig;
7 import org.springframework.boot.SpringApplication;
8 import org.springframework.boot.autoconfigure.SpringBootApplication;
9 import org.springframework.boot.context.properties.EnableConfigurationProperties;
10 import org.springframework.context.ConfigurableApplicationContext;
11 import org.springframework.context.annotation.Bean;
12 import org.springframework.kafka.support.serializer.JsonDeserializer;
13 import org.springframework.kafka.support.serializer.JsonSerde;
14 import org.springframework.kafka.support.serializer.JsonSerializer;
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(SplitterApplicationProperties.class)
24 @Slf4j
25 public class SplitterApplication
26 {
27         @Bean(initMethod = "start", destroyMethod = "stop")
28         public SplitterStreamProcessor streamProcessor(
29                         SplitterApplicationProperties properties,
30                         ConfigurableApplicationContext context)
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, Serdes.StringSerde.class.getName());
37                 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
38                 propertyMap.put(JsonDeserializer.TRUSTED_PACKAGES, Recording.class.getName()    );
39                 propertyMap.put(JsonDeserializer.VALUE_DEFAULT_TYPE, Recording.class.getName());
40                 propertyMap.put(JsonSerializer.ADD_TYPE_INFO_HEADERS, false);
41                 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
42
43                 SplitterStreamProcessor streamProcessor = new SplitterStreamProcessor(
44                                 properties.getInputTopic(),
45                                 properties.getOutputTopic(),
46                                 propertyMap);
47
48                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
49         {
50                 log.error("Unexpected error!", e);
51                 CompletableFuture.runAsync(() ->
52                 {
53                         log.info("Stopping application...");
54                         SpringApplication.exit(context, () -> 1);
55                 });
56                 return SHUTDOWN_CLIENT;
57         });
58
59
60                 return streamProcessor;
61         }
62
63         public static void main(String[] args)
64         {
65                 SpringApplication.run(SplitterApplication.class, args);
66         }
67 }