1 package de.juplo.kafka.wordcount.splitter;
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;
16 import java.util.Properties;
17 import java.util.concurrent.CompletableFuture;
19 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
23 @EnableConfigurationProperties(SplitterApplicationProperties.class)
25 public class SplitterApplicationConfiguration
27 @Bean(initMethod = "start", destroyMethod = "stop")
28 public SplitterStreamProcessor streamProcessor(
29 SplitterApplicationProperties properties,
30 ConfigurableApplicationContext context)
32 Properties propertyMap = new Properties();
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, SplitterApplication.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");
43 SplitterStreamProcessor streamProcessor = new SplitterStreamProcessor(
44 properties.getInputTopic(),
45 properties.getOutputTopic(),
48 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
50 log.error("Unexpected error!", e);
51 CompletableFuture.runAsync(() ->
53 log.info("Stopping application...");
54 SpringApplication.exit(context, () -> 1);
56 return SHUTDOWN_CLIENT;
59 return streamProcessor;