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 AdderRecordHandler sumRecordHandler()
23 return new AdderRecordHandler();
27 public AdderRebalanceListener sumRebalanceListener(
28 AdderRecordHandler adderRecordHandler,
29 PartitionStatisticsRepository repository,
30 Consumer<String, String> consumer,
31 ApplicationProperties properties)
33 return new AdderRebalanceListener(
36 properties.getClientId(),
37 properties.getTopic(),
38 Clock.systemDefaultZone(),
39 properties.getCommitInterval(),
44 public EndlessConsumer<String, String> endlessConsumer(
45 KafkaConsumer<String, String> kafkaConsumer,
46 ExecutorService executor,
47 AdderRebalanceListener adderRebalanceListener,
48 AdderRecordHandler adderRecordHandler,
49 ApplicationProperties properties)
52 new EndlessConsumer<>(
54 properties.getClientId(),
55 properties.getTopic(),
57 adderRebalanceListener,
62 public ExecutorService executor()
64 return Executors.newSingleThreadExecutor();
67 @Bean(destroyMethod = "close")
68 public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
70 Properties props = new Properties();
72 props.put("bootstrap.servers", properties.getBootstrapServer());
73 props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor");
74 props.put("group.id", properties.getGroupId());
75 props.put("client.id", properties.getClientId());
76 props.put("enable.auto.commit", false);
77 props.put("auto.offset.reset", properties.getAutoOffsetReset());
78 props.put("metadata.max.age.ms", "1000");
79 props.put("key.deserializer", StringDeserializer.class.getName());
80 props.put("value.deserializer", StringDeserializer.class.getName());
82 return new KafkaConsumer<>(props);