X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FApplicationConfiguration.java;h=3c526dfa0efb8a55c683028bae34bd98d7da71a5;hb=d8f4e839b57b8405ca909b5472a8d4639937a101;hp=76d0c8aa209835aa332a9d9dc402b1c598d54220;hpb=31dc17e7f5ac937f21882ec28b3234c5c398e840;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 76d0c8a..3c526df 100644 --- a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java +++ b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java @@ -1,8 +1,10 @@ package de.juplo.kafka; +import org.apache.kafka.clients.consumer.ConsumerRecord; 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.autoconfigure.kafka.KafkaProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -10,24 +12,37 @@ import org.springframework.context.annotation.Configuration; import java.util.Properties; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.function.Consumer; @Configuration -@EnableConfigurationProperties(ApplicationProperties.class) +@EnableConfigurationProperties({ KafkaProperties.class, ApplicationProperties.class }) public class ApplicationConfiguration { @Bean - public EndlessConsumer endlessConsumer( - KafkaConsumer kafkaConsumer, + public Consumer> consumer() + { + return (record) -> + { + // Handle record + }; + } + + @Bean + public EndlessConsumer endlessConsumer( + KafkaConsumer kafkaConsumer, ExecutorService executor, - ApplicationProperties properties) + Consumer> handler, + KafkaProperties kafkaProperties, + ApplicationProperties applicationProperties) { return - new EndlessConsumer( + new EndlessConsumer<>( executor, - properties.getClientId(), - properties.getTopic(), - kafkaConsumer); + kafkaProperties.getConsumer().getClientId(), + applicationProperties.getTopic(), + kafkaConsumer, + handler); } @Bean @@ -37,14 +52,14 @@ public class ApplicationConfiguration } @Bean(destroyMethod = "close") - public KafkaConsumer kafkaConsumer(ApplicationProperties properties) + public KafkaConsumer kafkaConsumer(KafkaProperties properties) { Properties props = new Properties(); - props.put("bootstrap.servers", properties.getBootstrapServer()); - props.put("group.id", properties.getGroupId()); - props.put("client.id", properties.getClientId()); - props.put("auto.offset.reset", properties.getAutoOffsetReset()); + props.put("bootstrap.servers", properties.getConsumer().getBootstrapServers()); + props.put("group.id", properties.getConsumer().getGroupId()); + props.put("client.id", properties.getConsumer().getClientId()); + props.put("auto.offset.reset", properties.getConsumer().getAutoOffsetReset()); props.put("metadata.max.age.ms", "1000"); props.put("key.deserializer", StringDeserializer.class.getName()); props.put("value.deserializer", LongDeserializer.class.getName());