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.common.serialization.Serdes;
7 import org.apache.kafka.streams.StreamsConfig;
8 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
9 import org.apache.kafka.streams.state.Stores;
10 import org.springframework.boot.SpringApplication;
11 import org.springframework.boot.context.properties.EnableConfigurationProperties;
12 import org.springframework.context.ConfigurableApplicationContext;
13 import org.springframework.context.annotation.Bean;
14 import org.springframework.context.annotation.Configuration;
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 propertyMap(CounterApplicationProperties properties)
30 Properties propertyMap = new Properties();
32 propertyMap.put(StreamsConfig.APPLICATION_ID_CONFIG, properties.getApplicationId());
33 propertyMap.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServer());
34 propertyMap.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.StringSerde.class.getName());
35 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.StringSerde.class.getName());
36 propertyMap.put(StreamsConfig.STATE_DIR_CONFIG, "target");
37 if (properties.getCommitInterval() != null)
38 propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, properties.getCommitInterval());
39 if (properties.getCacheMaxBytes() != null)
40 propertyMap.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, properties.getCacheMaxBytes());
41 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
46 @Bean(initMethod = "start", destroyMethod = "stop")
47 public CounterStreamProcessor streamProcessor(
48 CounterApplicationProperties properties,
49 Properties propertyMap,
50 KeyValueBytesStoreSupplier storeSupplier,
51 ObjectMapper objectMapper,
52 ConfigurableApplicationContext context)
54 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
55 properties.getInputTopic(),
56 properties.getOutputTopic(),
61 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
63 log.error("Unexpected error!", e);
64 CompletableFuture.runAsync(() ->
66 log.info("Stopping application...");
67 SpringApplication.exit(context, () -> 1);
69 return SHUTDOWN_CLIENT;
73 return streamProcessor;
77 public KeyValueBytesStoreSupplier storeSupplier()
79 return Stores.persistentKeyValueStore("counter");