X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FApplicationConfiguration.java;h=766740b3d5549c4742dea5dccc24b963258594eb;hb=657bf71b6c1c99065f26cccf0c3d2a1f30bc9407;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..766740b 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.ConsumerRecord; import org.apache.kafka.clients.consumer.KafkaConsumer; import org.apache.kafka.common.serialization.LongDeserializer; import org.apache.kafka.common.serialization.StringDeserializer; @@ -8,7 +9,9 @@ import org.springframework.context.annotation.Bean; 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 @@ -16,20 +19,38 @@ import java.util.concurrent.Executors; public class ApplicationConfiguration { @Bean - public EndlessConsumer endlessConsumer( - KafkaConsumer kafkaConsumer, + public Consumer> consumer() + { + return (record) -> + { + // Handle record + }; + } + + @Bean + public EndlessConsumer endlessConsumer( + KafkaConsumer kafkaConsumer, + ExecutorService executor, + Consumer> handler, ApplicationProperties properties) { return - new EndlessConsumer( - Executors.newFixedThreadPool(1), + new EndlessConsumer<>( + executor, properties.getClientId(), properties.getTopic(), - kafkaConsumer); + kafkaConsumer, + handler); + } + + @Bean + public ExecutorService executor() + { + return Executors.newSingleThreadExecutor(); } @Bean(destroyMethod = "close") - public KafkaConsumer kafkaConsumer(ApplicationProperties properties) + public KafkaConsumer kafkaConsumer(ApplicationProperties properties) { Properties props = new Properties(); @@ -37,6 +58,7 @@ public class ApplicationConfiguration props.put("group.id", properties.getGroupId()); props.put("client.id", properties.getClientId()); 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());