splitter: 1.2.0 - A domain-class (``User``) is used as key
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / splitter / SplitterApplicationConfiguration.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.context.properties.EnableConfigurationProperties;
9 import org.springframework.context.ConfigurableApplicationContext;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.Configuration;
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 @Configuration
23 @EnableConfigurationProperties(SplitterApplicationProperties.class)
24 @Slf4j
25 public class SplitterApplicationConfiguration
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, JsonSerde.class.getName());
37                 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
38                 propertyMap.put(JsonDeserializer.TRUSTED_PACKAGES, SplitterApplication.class.getName());
39                 propertyMap.put(JsonDeserializer.KEY_DEFAULT_TYPE, User.class.getName());
40                 propertyMap.put(JsonDeserializer.VALUE_DEFAULT_TYPE, Recording.class.getName());
41                 propertyMap.put(JsonSerializer.ADD_TYPE_INFO_HEADERS, false);
42                 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
43
44                 SplitterStreamProcessor streamProcessor = new SplitterStreamProcessor(
45                                 properties.getInputTopic(),
46                                 properties.getOutputTopic(),
47                                 propertyMap);
48
49                 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
50                 {
51                         log.error("Unexpected error!", e);
52                         CompletableFuture.runAsync(() ->
53                         {
54                                 log.info("Stopping application...");
55                                 SpringApplication.exit(context, () -> 1);
56                         });
57                         return SHUTDOWN_CLIENT;
58                 });
59
60                 return streamProcessor;
61         }
62 }