1 package de.juplo.kafka;
3 import org.apache.kafka.clients.consumer.Consumer;
4 import org.apache.kafka.clients.consumer.KafkaConsumer;
5 import org.apache.kafka.common.serialization.StringDeserializer;
6 import org.springframework.boot.context.properties.EnableConfigurationProperties;
7 import org.springframework.context.annotation.Bean;
8 import org.springframework.context.annotation.Configuration;
10 import java.time.Clock;
11 import java.util.Properties;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
17 @EnableConfigurationProperties(ApplicationProperties.class)
18 public class ApplicationConfiguration
21 public ApplicationRecordHandler recordHandler(AdderResults adderResults)
23 return new ApplicationRecordHandler(adderResults);
27 public AdderResults adderResults()
29 return new AdderResults();
33 public ApplicationRebalanceListener rebalanceListener(
34 ApplicationRecordHandler recordHandler,
35 AdderResults adderResults,
36 StateRepository stateRepository,
37 Consumer<String, String> consumer,
38 ApplicationProperties properties)
40 return new ApplicationRebalanceListener(
44 properties.getClientId(),
45 properties.getTopic(),
46 Clock.systemDefaultZone(),
47 properties.getCommitInterval(),
52 public EndlessConsumer<String, String> endlessConsumer(
53 KafkaConsumer<String, String> kafkaConsumer,
54 ExecutorService executor,
55 ApplicationRebalanceListener rebalanceListener,
56 ApplicationRecordHandler recordHandler,
57 ApplicationProperties properties)
60 new EndlessConsumer<>(
62 properties.getClientId(),
63 properties.getTopic(),
70 public ExecutorService executor()
72 return Executors.newSingleThreadExecutor();
75 @Bean(destroyMethod = "close")
76 public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
78 Properties props = new Properties();
80 props.put("bootstrap.servers", properties.getBootstrapServer());
81 props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor");
82 props.put("group.id", properties.getGroupId());
83 props.put("client.id", properties.getClientId());
84 props.put("enable.auto.commit", false);
85 props.put("auto.offset.reset", properties.getAutoOffsetReset());
86 props.put("metadata.max.age.ms", "1000");
87 props.put("key.deserializer", StringDeserializer.class.getName());
88 props.put("value.deserializer", StringDeserializer.class.getName());
90 return new KafkaConsumer<>(props);