Rückbau der Deaktivierung der Commits, um den Code anzugleichen
[demos/kafka/training] / src / main / java / de / juplo / kafka / ApplicationRecordHandler.java
index 596f3da..51d524f 100644 (file)
@@ -4,8 +4,10 @@ import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.clients.consumer.ConsumerRecord;
 
+import java.time.Duration;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Optional;
 
 
 @RequiredArgsConstructor
@@ -13,6 +15,8 @@ import java.util.Map;
 public class ApplicationRecordHandler implements RecordHandler<String, String>
 {
   private final AdderResults results;
+  private final Optional<Duration> throttle;
+  private final String id;
 
   private final Map<Integer, AdderBusinessLogic> state = new HashMap<>();
 
@@ -27,12 +31,25 @@ public class ApplicationRecordHandler implements RecordHandler<String, String>
     if (message.equals("CALCULATE"))
     {
       AdderResult result = state.get(partition).calculate(user);
-      log.info("New result for {}: {}", user, result);
+      log.info("{} - New result for {}: {}", id, user, result);
       results.addResults(partition, user, result);
-      return;
+    }
+    else
+    {
+      state.get(partition).addToSum(user, Integer.parseInt(message));
     }
 
-    state.get(partition).addToSum(user, Integer.parseInt(message));
+    if (throttle.isPresent())
+    {
+      try
+      {
+        Thread.sleep(throttle.get().toMillis());
+      }
+      catch (InterruptedException e)
+      {
+        log.warn("{} - Intrerrupted while throttling: {}", id, e);
+      }
+    }
   }
 
   protected void addPartition(Integer partition, Map<String, AdderResult> state)