1 package de.juplo.kafka;
3 import org.apache.kafka.clients.consumer.KafkaConsumer;
4 import org.apache.kafka.common.serialization.StringDeserializer;
5 import org.springframework.boot.context.properties.EnableConfigurationProperties;
6 import org.springframework.context.annotation.Bean;
7 import org.springframework.context.annotation.Configuration;
9 import java.time.Clock;
10 import java.util.Optional;
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(
22 AdderResults adderResults,
23 ApplicationProperties properties)
25 return new ApplicationRecordHandler(
27 Optional.ofNullable(properties.getThrottle()));
31 public AdderResults adderResults()
33 return new AdderResults();
37 public ApplicationRebalanceListener rebalanceListener(
38 KafkaConsumer<String, String> kafkaConsumer,
39 ApplicationRecordHandler recordHandler,
40 AdderResults adderResults,
41 StateRepository stateRepository,
42 ApplicationProperties properties)
44 return new ApplicationRebalanceListener(
49 properties.getClientId());
53 public EndlessConsumer<String, String> endlessConsumer(
54 KafkaConsumer<String, String> kafkaConsumer,
55 ExecutorService executor,
56 ApplicationRebalanceListener rebalanceListener,
57 ApplicationRecordHandler recordHandler,
58 ApplicationProperties properties)
61 new EndlessConsumer<>(
63 properties.getClientId(),
64 properties.getTopic(),
71 public ExecutorService executor()
73 return Executors.newSingleThreadExecutor();
76 @Bean(destroyMethod = "close")
77 public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
79 Properties props = new Properties();
81 props.put("bootstrap.servers", properties.getBootstrapServer());
82 props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor");
83 props.put("group.id", properties.getGroupId());
84 props.put("client.id", properties.getClientId());
85 props.put("auto.offset.reset", properties.getAutoOffsetReset());
86 props.put("auto.commit.interval.ms", (int)properties.getCommitInterval().toMillis());
87 props.put("metadata.max.age.ms", "1000");
88 props.put("key.deserializer", StringDeserializer.class.getName());
89 props.put("value.deserializer", StringDeserializer.class.getName());
91 return new KafkaConsumer<>(props);