X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FApplicationConfiguration.java;h=da1605bee122fe7f83229a14410de53abd3e997a;hb=818c1eb862247e25abf9f7d91d5a73e3e3789a39;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..da1605b 100644 --- a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java +++ b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java @@ -1,13 +1,15 @@ 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; 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,16 +18,49 @@ import java.util.concurrent.Executors; public class ApplicationConfiguration { @Bean - public EndlessConsumer endlessConsumer( + public WordcountRecordHandler wordcountRecordHandler( + PartitionStatisticsRepository repository, + Consumer consumer, + ApplicationProperties properties) + { + return new WordcountRecordHandler( + repository, + properties.getClientId(), + properties.getTopic(), + Clock.systemDefaultZone(), + properties.getCommitInterval(), + consumer); + } + + @Bean + public WordcountRebalanceListener wordcountRebalanceListener( + WordcountRecordHandler wordcountRecordHandler) + { + return new WordcountRebalanceListener(wordcountRecordHandler); + } + + @Bean + public EndlessConsumer endlessConsumer( KafkaConsumer kafkaConsumer, + ExecutorService executor, + WordcountRebalanceListener wordcountRebalanceListener, + WordcountRecordHandler wordcountRecordHandler, ApplicationProperties properties) { return - new EndlessConsumer( - Executors.newFixedThreadPool(1), + new EndlessConsumer<>( + executor, properties.getClientId(), properties.getTopic(), - kafkaConsumer); + kafkaConsumer, + wordcountRebalanceListener, + wordcountRecordHandler); + } + + @Bean + public ExecutorService executor() + { + return Executors.newSingleThreadExecutor(); } @Bean(destroyMethod = "close") @@ -34,12 +69,14 @@ public class ApplicationConfiguration 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("metadata.max.age.ms", "1000"); props.put("key.deserializer", StringDeserializer.class.getName()); - props.put("value.deserializer", LongDeserializer.class.getName()); + props.put("value.deserializer", StringDeserializer.class.getName()); return new KafkaConsumer<>(props); }