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(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());
42 JsonDeserializer.TYPE_MAPPINGS,
43 "W:" + Word.class.getName() + "," +
44 "C:" + WordCount.class.getName());
45 propertyMap.put(StreamsConfig.STATE_DIR_CONFIG, "target");
46 if (properties.getCommitInterval() != null)
47 propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, properties.getCommitInterval());
48 if (properties.getCacheMaxBytes() != null)
49 propertyMap.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, properties.getCacheMaxBytes());
50 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
55 @Bean(initMethod = "start", destroyMethod = "stop")
56 public CounterStreamProcessor streamProcessor(
57 CounterApplicationProperties properties,
58 Properties propertyMap,
59 KeyValueBytesStoreSupplier storeSupplier,
60 ObjectMapper objectMapper,
61 ConfigurableApplicationContext context)
63 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
64 properties.getInputTopic(),
65 properties.getOutputTopic(),
70 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
72 log.error("Unexpected error!", e);
73 CompletableFuture.runAsync(() ->
75 log.info("Stopping application...");
76 SpringApplication.exit(context, () -> 1);
78 return SHUTDOWN_CLIENT;
82 return streamProcessor;
86 public KeyValueBytesStoreSupplier storeSupplier()
88 return Stores.persistentKeyValueStore("counter");