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.springframework.boot.SpringApplication;
9 import org.springframework.boot.autoconfigure.SpringBootApplication;
10 import org.springframework.boot.context.properties.EnableConfigurationProperties;
11 import org.springframework.context.ConfigurableApplicationContext;
12 import org.springframework.context.annotation.Bean;
14 import java.util.Properties;
15 import java.util.concurrent.CompletableFuture;
17 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
20 @SpringBootApplication
21 @EnableConfigurationProperties(CounterApplicationProperties.class)
23 public class CounterApplication
25 @Bean(initMethod = "start", destroyMethod = "stop")
26 public CounterStreamProcessor streamProcessor(
27 CounterApplicationProperties properties,
28 ObjectMapper objectMapper,
29 ConfigurableApplicationContext context)
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, Serdes.StringSerde.class.getName());
36 propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.StringSerde.class.getName());
37 propertyMap.put(StreamsConfig.STATE_DIR_CONFIG, "target");
38 propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 0);
39 propertyMap.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, 0);
40 propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
42 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
43 properties.getInputTopic(),
44 properties.getOutputTopic(),
48 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
50 log.error("Unexpected error!", e);
51 CompletableFuture.runAsync(() ->
53 log.info("Stopping application...");
54 SpringApplication.exit(context, () -> 1);
56 return SHUTDOWN_CLIENT;
60 return streamProcessor;
63 public static void main(String[] args)
65 SpringApplication.run(CounterApplication.class, args);