From: Kai Moritz Date: Sat, 9 Apr 2022 14:04:02 +0000 (+0200) Subject: Refaktorisierung für Tests - EndlessConsumer typisiert X-Git-Tag: deserialization-synchroner-test~7 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=6a02ed79e96cdfe01e025b8c1ee54e5c649cf058;p=demos%2Fkafka%2Ftraining Refaktorisierung für Tests - EndlessConsumer typisiert --- diff --git a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java index f228d85..c5fca91 100644 --- a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java +++ b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java @@ -28,14 +28,14 @@ public class ApplicationConfiguration } @Bean - public EndlessConsumer endlessConsumer( + public EndlessConsumer endlessConsumer( KafkaConsumer kafkaConsumer, ExecutorService executor, Consumer> handler, ApplicationProperties properties) { return - new EndlessConsumer( + new EndlessConsumer<>( executor, properties.getClientId(), properties.getTopic(), diff --git a/src/main/java/de/juplo/kafka/ApplicationHealthIndicator.java b/src/main/java/de/juplo/kafka/ApplicationHealthIndicator.java index ab9782c..dc3a26e 100644 --- a/src/main/java/de/juplo/kafka/ApplicationHealthIndicator.java +++ b/src/main/java/de/juplo/kafka/ApplicationHealthIndicator.java @@ -10,7 +10,7 @@ import org.springframework.stereotype.Component; @RequiredArgsConstructor public class ApplicationHealthIndicator implements HealthIndicator { - private final EndlessConsumer consumer; + private final EndlessConsumer consumer; @Override diff --git a/src/main/java/de/juplo/kafka/EndlessConsumer.java b/src/main/java/de/juplo/kafka/EndlessConsumer.java index 38dd360..0a95b2c 100644 --- a/src/main/java/de/juplo/kafka/EndlessConsumer.java +++ b/src/main/java/de/juplo/kafka/EndlessConsumer.java @@ -18,13 +18,13 @@ import java.util.concurrent.locks.ReentrantLock; @Slf4j @RequiredArgsConstructor -public class EndlessConsumer implements Runnable +public class EndlessConsumer implements Runnable { private final ExecutorService executor; private final String id; private final String topic; - private final Consumer consumer; - private final java.util.function.Consumer> handler; + private final Consumer consumer; + private final java.util.function.Consumer> handler; private final Lock lock = new ReentrantLock(); private final Condition condition = lock.newCondition(); @@ -88,12 +88,12 @@ public class EndlessConsumer implements Runnable while (true) { - ConsumerRecords records = + ConsumerRecords records = consumer.poll(Duration.ofSeconds(1)); // Do something with the data... log.info("{} - Received {} messages", id, records.count()); - for (ConsumerRecord record : records) + for (ConsumerRecord record : records) { log.info( "{} - {}: {}/{} - {}={}", @@ -110,7 +110,7 @@ public class EndlessConsumer implements Runnable consumed++; Integer partition = record.partition(); - String key = record.key() == null ? "NULL" : record.key(); + String key = record.key() == null ? "NULL" : record.key().toString(); Map byKey = seen.get(partition); if (!byKey.containsKey(key))