Merge branch 'rebalance-listener' into stored-state
[demos/kafka/training] / src / main / java / de / juplo / kafka / EndlessConsumer.java
index 9aa8152..7cb77aa 100644 (file)
@@ -22,6 +22,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 public class EndlessConsumer implements Runnable
 {
   private final ExecutorService executor;
+  private final PartitionStatisticsRepository repository;
   private final String bootstrapServer;
   private final String groupId;
   private final String id;
@@ -38,6 +39,7 @@ public class EndlessConsumer implements Runnable
 
   public EndlessConsumer(
       ExecutorService executor,
+      PartitionStatisticsRepository repository,
       String bootstrapServer,
       String groupId,
       String clientId,
@@ -45,6 +47,7 @@ public class EndlessConsumer implements Runnable
       String autoOffsetReset)
   {
     this.executor = executor;
+    this.repository = repository;
     this.bootstrapServer = bootstrapServer;
     this.groupId = groupId;
     this.id = clientId;
@@ -74,13 +77,36 @@ public class EndlessConsumer implements Runnable
         @Override
         public void onPartitionsRevoked(Collection<TopicPartition> partitions)
         {
-          partitions.forEach(tp -> seen.remove(tp.partition()));
+          partitions.forEach(tp ->
+          {
+            log.info("{} - removing partition: {}", id, tp);
+            Map<String, Integer> removed = seen.remove(tp.partition());
+            for (String key : removed.keySet())
+            {
+              log.info(
+                  "{} - Seen {} messages for partition={}|key={}",
+                  id,
+                  removed.get(key),
+                  tp.partition(),
+                  key);
+            }
+            repository.save(new StatisticsDocument(tp.partition(), removed));
+          });
         }
 
         @Override
         public void onPartitionsAssigned(Collection<TopicPartition> partitions)
         {
-          partitions.forEach(tp -> seen.put(tp.partition(), new HashMap<>()));
+          partitions.forEach(tp ->
+          {
+            log.info("{} - adding partition: {}", id, tp);
+            seen.put(
+                tp.partition(),
+                repository
+                    .findById(Integer.toString(tp.partition()))
+                    .map(document -> document.statistics)
+                    .orElse(new HashMap<>()));
+          });
         }
       });
 
@@ -130,21 +156,6 @@ public class EndlessConsumer implements Runnable
     {
       log.info("{} - Closing the KafkaConsumer", id);
       consumer.close();
-
-      for (Integer partition : seen.keySet())
-      {
-        Map<String, Integer> byKey = seen.get(partition);
-        for (String key : byKey.keySet())
-        {
-          log.info(
-              "{} - Seen {} messages for partition={}|key={}",
-              id,
-              byKey.get(key),
-              partition,
-              key);
-        }
-      }
-
       log.info("{} - Consumer-Thread exiting", id);
     }
   }