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;
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(CounterApplicationProperties.class)
25 public class CounterApplicationConfiguriation
28 public Properties streamProcessorProperties(CounterApplicationProperties counterProperties)
30 Properties propertyMap = new Properties();
32 propertyMap.put(StreamsConfig.APPLICATION_ID_CONFIG, counterProperties.getApplicationId());
33 propertyMap.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, counterProperties.getBootstrapServer());
34 propertyMap.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
35 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
36 propertyMap.put(JsonDeserializer.TRUSTED_PACKAGES, CounterApplication.class.getPackageName());
37 propertyMap.put(JsonDeserializer.KEY_DEFAULT_TYPE, Word.class.getName());
38 propertyMap.put(JsonDeserializer.VALUE_DEFAULT_TYPE, Word.class.getName());
40 JsonDeserializer.TYPE_MAPPINGS,
41 "word:" + Word.class.getName() + "," +
42 "counter:" + WordCounter.class.getName());
43 propertyMap.put(JsonDeserializer.REMOVE_TYPE_INFO_HEADERS, Boolean.FALSE);
44 propertyMap.put(StreamsConfig.STATE_DIR_CONFIG, "target");
45 if (counterProperties.getCommitInterval() != null)
46 propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, counterProperties.getCommitInterval());
47 if (counterProperties.getCacheMaxBytes() != null)
48 propertyMap.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, counterProperties.getCacheMaxBytes());
49 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
54 @Bean(initMethod = "start", destroyMethod = "stop")
55 public CounterStreamProcessor streamProcessor(
56 CounterApplicationProperties applicationProperties,
57 Properties streamProcessorProperties,
58 KeyValueBytesStoreSupplier storeSupplier,
59 ConfigurableApplicationContext context)
61 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
62 applicationProperties.getInputTopic(),
63 applicationProperties.getOutputTopic(),
64 streamProcessorProperties,
67 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
69 log.error("Unexpected error!", e);
70 CompletableFuture.runAsync(() ->
72 log.info("Stopping application...");
73 SpringApplication.exit(context, () -> 1);
75 return SHUTDOWN_CLIENT;
79 return streamProcessor;
83 public KeyValueBytesStoreSupplier storeSupplier()
85 return Stores.persistentKeyValueStore("counter");