1 package de.juplo.kafka.wordcount.counter;
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import lombok.extern.slf4j.Slf4j;
5 import org.apache.kafka.clients.consumer.ConsumerConfig;
6 import org.apache.kafka.streams.StreamsConfig;
7 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
8 import org.apache.kafka.streams.state.Stores;
9 import org.springframework.boot.SpringApplication;
10 import org.springframework.boot.context.properties.EnableConfigurationProperties;
11 import org.springframework.context.ConfigurableApplicationContext;
12 import org.springframework.context.annotation.Bean;
13 import org.springframework.context.annotation.Configuration;
14 import org.springframework.kafka.support.serializer.JsonDeserializer;
15 import org.springframework.kafka.support.serializer.JsonSerde;
16 import org.springframework.kafka.support.serializer.JsonSerializer;
18 import java.util.Properties;
19 import java.util.concurrent.CompletableFuture;
21 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
25 @EnableConfigurationProperties(CounterApplicationProperties.class)
27 public class CounterApplicationConfiguriation
30 public Properties propertyMap(CounterApplicationProperties properties)
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, JsonSerde.class.getName());
37 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
38 propertyMap.put(JsonSerializer.ADD_TYPE_INFO_HEADERS, false);
39 propertyMap.put(JsonDeserializer.TRUSTED_PACKAGES, CounterApplication.class.getPackageName());
40 propertyMap.put(JsonDeserializer.KEY_DEFAULT_TYPE, Word.class.getName());
41 propertyMap.put(JsonDeserializer.VALUE_DEFAULT_TYPE, Word.class.getName());
42 propertyMap.put(JsonDeserializer.USE_TYPE_INFO_HEADERS, false);
43 propertyMap.put(StreamsConfig.STATE_DIR_CONFIG, "target");
44 if (properties.getCommitInterval() != null)
45 propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, properties.getCommitInterval());
46 if (properties.getCacheMaxBytes() != null)
47 propertyMap.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, properties.getCacheMaxBytes());
48 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
53 @Bean(initMethod = "start", destroyMethod = "stop")
54 public CounterStreamProcessor streamProcessor(
55 CounterApplicationProperties properties,
56 Properties propertyMap,
57 KeyValueBytesStoreSupplier storeSupplier,
58 ObjectMapper objectMapper,
59 ConfigurableApplicationContext context)
61 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
62 properties.getInputTopic(),
63 properties.getOutputTopic(),
68 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
70 log.error("Unexpected error!", e);
71 CompletableFuture.runAsync(() ->
73 log.info("Stopping application...");
74 SpringApplication.exit(context, () -> 1);
76 return SHUTDOWN_CLIENT;
80 return streamProcessor;
84 public KeyValueBytesStoreSupplier storeSupplier()
86 return Stores.persistentKeyValueStore("counter");