X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FApplicationConfiguration.java;h=3925fcba5fcd5a6a5f9bf934e93546493440e90c;hb=2da45caa1f9d32e3a5506d71cce7f06fa2e36523;hp=58f44faba9bc133788592e2c35765adb27fafde2;hpb=b9ca34cf63146e9ae5e72a8722a92f17615586d2;p=demos%2Fkafka%2Ftraining diff --git a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java index 58f44fa..3925fcb 100644 --- a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java +++ b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java @@ -1,5 +1,6 @@ package de.juplo.kafka; +import org.apache.kafka.clients.consumer.Consumer; import org.apache.kafka.clients.consumer.KafkaConsumer; import org.apache.kafka.common.serialization.LongDeserializer; import org.apache.kafka.common.serialization.StringDeserializer; @@ -7,7 +8,9 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.time.Clock; import java.util.Properties; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -16,27 +19,64 @@ import java.util.concurrent.Executors; public class ApplicationConfiguration { @Bean - public EndlessConsumer endlessConsumer( - KafkaConsumer kafkaConsumer, + public KeyCountingRecordHandler messageCountingRecordHandler() + { + return new KeyCountingRecordHandler(); + } + + @Bean + public KeyCountingRebalanceListener wordcountRebalanceListener( + KeyCountingRecordHandler keyCountingRecordHandler, + PartitionStatisticsRepository repository, + Consumer consumer, + ApplicationProperties properties) + { + return new KeyCountingRebalanceListener( + keyCountingRecordHandler, + repository, + properties.getClientId(), + properties.getTopic(), + Clock.systemDefaultZone(), + properties.getCommitInterval(), + consumer); + } + + @Bean + public EndlessConsumer endlessConsumer( + KafkaConsumer kafkaConsumer, + ExecutorService executor, + KeyCountingRebalanceListener keyCountingRebalanceListener, + KeyCountingRecordHandler keyCountingRecordHandler, ApplicationProperties properties) { return - new EndlessConsumer( - Executors.newFixedThreadPool(1), + new EndlessConsumer<>( + executor, properties.getClientId(), properties.getTopic(), - kafkaConsumer); + kafkaConsumer, + keyCountingRebalanceListener, + keyCountingRecordHandler); + } + + @Bean + public ExecutorService executor() + { + return Executors.newSingleThreadExecutor(); } @Bean(destroyMethod = "close") - public KafkaConsumer kafkaConsumer(ApplicationProperties properties) + public KafkaConsumer kafkaConsumer(ApplicationProperties properties) { Properties props = new Properties(); props.put("bootstrap.servers", properties.getBootstrapServer()); + props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor"); props.put("group.id", properties.getGroupId()); props.put("client.id", properties.getClientId()); + props.put("enable.auto.commit", false); props.put("auto.offset.reset", properties.getAutoOffsetReset()); + props.put("auto.commit.interval.ms", (int)properties.getCommitInterval().toMillis()); props.put("metadata.max.age.ms", "1000"); props.put("key.deserializer", StringDeserializer.class.getName()); props.put("value.deserializer", LongDeserializer.class.getName());