Refaktorisierung für Tests - EndlessConsumer typisiert
authorKai Moritz <kai@juplo.de>
Sat, 9 Apr 2022 14:04:02 +0000 (16:04 +0200)
committerKai Moritz <kai@juplo.de>
Mon, 11 Apr 2022 13:07:20 +0000 (15:07 +0200)
src/main/java/de/juplo/kafka/ApplicationConfiguration.java
src/main/java/de/juplo/kafka/ApplicationHealthIndicator.java
src/main/java/de/juplo/kafka/EndlessConsumer.java

index f228d85..c5fca91 100644 (file)
@@ -28,14 +28,14 @@ public class ApplicationConfiguration
   }
 
   @Bean
-  public EndlessConsumer endlessConsumer(
+  public EndlessConsumer<String, String> endlessConsumer(
       KafkaConsumer<String, String> kafkaConsumer,
       ExecutorService executor,
       Consumer<ConsumerRecord<String, String>> handler,
       ApplicationProperties properties)
   {
     return
-        new EndlessConsumer(
+        new EndlessConsumer<>(
             executor,
             properties.getClientId(),
             properties.getTopic(),
index ab9782c..dc3a26e 100644 (file)
@@ -10,7 +10,7 @@ import org.springframework.stereotype.Component;
 @RequiredArgsConstructor
 public class ApplicationHealthIndicator implements HealthIndicator
 {
-  private final EndlessConsumer consumer;
+  private final EndlessConsumer<String, Long> consumer;
 
 
   @Override
index 38dd360..0a95b2c 100644 (file)
@@ -18,13 +18,13 @@ import java.util.concurrent.locks.ReentrantLock;
 
 @Slf4j
 @RequiredArgsConstructor
-public class EndlessConsumer implements Runnable
+public class EndlessConsumer<K, V> implements Runnable
 {
   private final ExecutorService executor;
   private final String id;
   private final String topic;
-  private final Consumer<String, String> consumer;
-  private final java.util.function.Consumer<ConsumerRecord<String, String>> handler;
+  private final Consumer<K, V> consumer;
+  private final java.util.function.Consumer<ConsumerRecord<K, V>> 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<String, String> records =
+        ConsumerRecords<K, V> records =
             consumer.poll(Duration.ofSeconds(1));
 
         // Do something with the data...
         log.info("{} - Received {} messages", id, records.count());
-        for (ConsumerRecord<String, String> record : records)
+        for (ConsumerRecord<K, V> 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<String, Long> byKey = seen.get(partition);
 
           if (!byKey.containsKey(key))