1 package de.juplo.kafka.wordcount.counter;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.ConsumerConfig;
5 import org.apache.kafka.streams.StreamsConfig;
6 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
7 import org.apache.kafka.streams.state.Stores;
8 import org.springframework.boot.SpringApplication;
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.context.annotation.Configuration;
13 import org.springframework.kafka.support.serializer.JsonDeserializer;
14 import org.springframework.kafka.support.serializer.JsonSerde;
15 import org.springframework.kafka.support.serializer.JsonSerializer;
17 import java.util.Properties;
18 import java.util.concurrent.CompletableFuture;
20 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
24 @EnableConfigurationProperties(CounterApplicationProperties.class)
26 public class CounterApplicationConfiguriation
29 public Properties propertyMap(CounterApplicationProperties properties)
31 Properties propertyMap = new Properties();
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, JsonSerde.class.getName());
36 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
37 propertyMap.put(JsonSerializer.ADD_TYPE_INFO_HEADERS, false);
38 propertyMap.put(JsonDeserializer.TRUSTED_PACKAGES, CounterApplication.class.getPackageName());
39 propertyMap.put(JsonDeserializer.KEY_DEFAULT_TYPE, Word.class.getName());
40 propertyMap.put(JsonDeserializer.VALUE_DEFAULT_TYPE, Word.class.getName());
41 propertyMap.put(JsonDeserializer.USE_TYPE_INFO_HEADERS, false);
42 propertyMap.put(StreamsConfig.STATE_DIR_CONFIG, "target");
43 if (properties.getCommitInterval() != null)
44 propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, properties.getCommitInterval());
45 if (properties.getCacheMaxBytes() != null)
46 propertyMap.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, properties.getCacheMaxBytes());
47 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
52 @Bean(initMethod = "start", destroyMethod = "stop")
53 public CounterStreamProcessor streamProcessor(
54 CounterApplicationProperties properties,
55 Properties propertyMap,
56 KeyValueBytesStoreSupplier storeSupplier,
57 ConfigurableApplicationContext context)
59 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
60 properties.getInputTopic(),
61 properties.getOutputTopic(),
65 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
67 log.error("Unexpected error!", e);
68 CompletableFuture.runAsync(() ->
70 log.info("Stopping application...");
71 SpringApplication.exit(context, () -> 1);
73 return SHUTDOWN_CLIENT;
77 return streamProcessor;
81 public KeyValueBytesStoreSupplier storeSupplier()
83 return Stores.persistentKeyValueStore("counter");