X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FApplicationConfiguration.java;h=175574783b5673e72e10ecf587c30f782858e84e;hb=0c9a0c1cf9a0065012743efcd940d8721bc33c20;hp=4473c6967840a8fe184aab238620778fccfcf036;hpb=de4f94c45fd0678777fecba4dbcb63f89e0ebafa;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 4473c69..1755747 100644 --- a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java +++ b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java @@ -1,79 +1,69 @@ package de.juplo.kafka; -import org.apache.kafka.clients.consumer.KafkaConsumer; -import org.apache.kafka.common.serialization.StringDeserializer; +import org.springframework.boot.autoconfigure.kafka.KafkaProperties; 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; +import java.util.Optional; + +import org.springframework.kafka.config.KafkaListenerEndpointRegistry; @Configuration -@EnableConfigurationProperties(ApplicationProperties.class) +@EnableConfigurationProperties({ KafkaProperties.class, ApplicationProperties.class }) public class ApplicationConfiguration { @Bean - public ApplicationRecordHandler recordHandler() + public ApplicationRecordHandler applicationRecordHandler( + AdderResults adderResults, + KafkaProperties kafkaProperties, + ApplicationProperties applicationProperties) + { + return new ApplicationRecordHandler( + adderResults, + Optional.ofNullable(applicationProperties.getThrottle()), + kafkaProperties.getClientId()); + } + + @Bean + public AdderResults adderResults() { - return new ApplicationRecordHandler(); + return new AdderResults(); } @Bean public ApplicationRebalanceListener rebalanceListener( ApplicationRecordHandler recordHandler, + AdderResults adderResults, StateRepository stateRepository, - ApplicationProperties properties) + KafkaProperties kafkaProperties) { return new ApplicationRebalanceListener( recordHandler, + adderResults, stateRepository, - properties.getClientId(), - Clock.systemDefaultZone(), - properties.getCommitInterval()); + kafkaProperties.getClientId()); } @Bean - public EndlessConsumer endlessConsumer( - KafkaConsumer kafkaConsumer, - ExecutorService executor, - ApplicationRebalanceListener rebalanceListener, - ApplicationRecordHandler recordHandler, - ApplicationProperties properties) + public ApplicationErrorHandler applicationErrorHandler() { - return - new EndlessConsumer<>( - executor, - properties.getClientId(), - properties.getTopic(), - kafkaConsumer, - rebalanceListener, - recordHandler); + return new ApplicationErrorHandler(); } @Bean - public ExecutorService executor() + public EndlessConsumer endlessConsumer( + RecordHandler recordHandler, + ApplicationErrorHandler errorHandler, + KafkaProperties kafkaProperties, + KafkaListenerEndpointRegistry endpointRegistry) { - return Executors.newSingleThreadExecutor(); - } - - @Bean(destroyMethod = "close") - 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("auto.offset.reset", properties.getAutoOffsetReset()); - props.put("metadata.max.age.ms", "1000"); - props.put("key.deserializer", StringDeserializer.class.getName()); - props.put("value.deserializer", StringDeserializer.class.getName()); - - return new KafkaConsumer<>(props); + return + new EndlessConsumer( + kafkaProperties.getClientId(), + endpointRegistry, + errorHandler, + recordHandler); } }