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(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
39 CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
40 properties.getInputTopic(),
41 properties.getOutputTopic(),
45 streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
47 log.error("Unexpected error!", e);
48 CompletableFuture.runAsync(() ->
50 log.info("Stopping application...");
51 SpringApplication.exit(context, () -> 1);
53 return SHUTDOWN_CLIENT;
57 return streamProcessor;
60 public static void main(String[] args)
62 SpringApplication.run(CounterApplication.class, args);