Die Ergebnisse werden gespeichert und sind via REST abrufbar
[demos/kafka/training] / src / main / java / de / juplo / kafka / ApplicationRebalanceListener.java
index 5a01393..cd9da64 100644 (file)
@@ -8,8 +8,7 @@ import org.apache.kafka.common.TopicPartition;
 import java.time.Clock;
 import java.time.Duration;
 import java.time.Instant;
-import java.util.Collection;
-import java.util.Map;
+import java.util.*;
 
 
 @RequiredArgsConstructor
@@ -17,6 +16,7 @@ import java.util.Map;
 public class ApplicationRebalanceListener implements PollIntervalAwareConsumerRebalanceListener
 {
   private final ApplicationRecordHandler recordHandler;
+  private final AdderResults adderResults;
   private final StateRepository stateRepository;
   private final String id;
   private final String topic;
@@ -24,6 +24,8 @@ public class ApplicationRebalanceListener implements PollIntervalAwareConsumerRe
   private final Duration commitInterval;
   private final Consumer<String, String> consumer;
 
+  private final Set<Integer> partitions = new HashSet<>();
+
   private Instant lastCommit = Instant.EPOCH;
   private boolean commitsEnabled = true;
 
@@ -33,6 +35,7 @@ public class ApplicationRebalanceListener implements PollIntervalAwareConsumerRe
     partitions.forEach(tp ->
     {
       Integer partition = tp.partition();
+      this.partitions.add(partition);
       StateDocument document =
           stateRepository
               .findById(Integer.toString(partition))
@@ -45,6 +48,7 @@ public class ApplicationRebalanceListener implements PollIntervalAwareConsumerRe
         consumer.seek(tp, document.offset);
       }
       recordHandler.addPartition(partition, document.state);
+      adderResults.addPartition(partition, document.results);
     });
   }
 
@@ -54,6 +58,7 @@ public class ApplicationRebalanceListener implements PollIntervalAwareConsumerRe
     partitions.forEach(tp ->
     {
       Integer partition = tp.partition();
+      this.partitions.remove(partition);
       Long offset = consumer.position(tp);
       log.info(
           "{} - removing partition: {}, offset of next message {})",
@@ -62,8 +67,9 @@ public class ApplicationRebalanceListener implements PollIntervalAwareConsumerRe
           offset);
       if (commitsEnabled)
       {
-        Map<String, AdderResult> removed = recordHandler.removePartition(partition);
-        stateRepository.save(new StateDocument(partition, removed, offset));
+        Map<String, AdderResult> state = recordHandler.removePartition(partition);
+        Map<String, List<AdderResult>> results = adderResults.removePartition(partition);
+        stateRepository.save(new StateDocument(partition, state, results, offset));
       }
       else
       {
@@ -85,11 +91,12 @@ public class ApplicationRebalanceListener implements PollIntervalAwareConsumerRe
     if (lastCommit.plus(commitInterval).isBefore(clock.instant()))
     {
       log.debug("Storing data and offsets, last commit: {}", lastCommit);
-      recordHandler.getState().forEach((partiton, adder) -> stateRepository.save(
+      partitions.forEach(partition -> stateRepository.save(
           new StateDocument(
-              partiton,
-              adder.getState(),
-              consumer.position(new TopicPartition(topic, partiton)))));
+              partition,
+              recordHandler.getState(partition).getState(),
+              adderResults.getState(partition),
+              consumer.position(new TopicPartition(topic, partition)))));
       lastCommit = clock.instant();
     }
   }