Vereinfachte Version der auf Spring Kafka basierenden Implementierung
[demos/kafka/training] / src / main / java / de / juplo / kafka / ApplicationRecordHandler.java
index 51d524f..2075781 100644 (file)
@@ -2,7 +2,11 @@ package de.juplo.kafka;
 
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.springframework.kafka.annotation.KafkaHandler;
+import org.springframework.kafka.annotation.KafkaListener;
+import org.springframework.kafka.support.KafkaHeaders;
+import org.springframework.messaging.handler.annotation.Header;
+import org.springframework.messaging.handler.annotation.Payload;
 
 import java.time.Duration;
 import java.util.HashMap;
@@ -12,7 +16,10 @@ import java.util.Optional;
 
 @RequiredArgsConstructor
 @Slf4j
-public class ApplicationRecordHandler implements RecordHandler<String, String>
+@KafkaListener(
+    id = "${spring.kafka.consumer.group-id}",
+    topics = "${sumup.adder.topic}")
+public class ApplicationRecordHandler
 {
   private final AdderResults results;
   private final Optional<Duration> throttle;
@@ -21,24 +28,37 @@ public class ApplicationRecordHandler implements RecordHandler<String, String>
   private final Map<Integer, AdderBusinessLogic> state = new HashMap<>();
 
 
-  @Override
-  public void accept(ConsumerRecord<String, String> record)
+  @KafkaHandler
+  public void addNumber(
+      @Header(KafkaHeaders.RECEIVED_PARTITION_ID)
+      Integer partition,
+      @Header(KafkaHeaders.RECEIVED_MESSAGE_KEY)
+      String user,
+      @Payload
+      MessageAddNumber message)
   {
-    Integer partition = record.partition();
-    String user = record.key();
-    String message = record.value();
+    log.debug("{} - Received {} for {} on {}", id, message, user, partition);
+    state.get(partition).addToSum(user, message.getNext());
+    throttle();
+  }
 
-    if (message.equals("CALCULATE"))
-    {
-      AdderResult result = state.get(partition).calculate(user);
-      log.info("{} - New result for {}: {}", id, user, result);
-      results.addResults(partition, user, result);
-    }
-    else
-    {
-      state.get(partition).addToSum(user, Integer.parseInt(message));
-    }
+  @KafkaHandler
+  public void calculateSum(
+      @Header(KafkaHeaders.RECEIVED_PARTITION_ID)
+      Integer partition,
+      @Header(KafkaHeaders.RECEIVED_MESSAGE_KEY)
+      String user,
+      @Payload
+      MessageCalculateSum message)
+  {
+    AdderResult result = state.get(partition).calculate(user);
+    log.info("{} - New result for {}: {}", id, user, result);
+    results.addResults(partition, user, result);
+    throttle();
+  }
 
+  private void throttle()
+  {
     if (throttle.isPresent())
     {
       try
@@ -61,15 +81,4 @@ public class ApplicationRecordHandler implements RecordHandler<String, String>
   {
     return this.state.remove(partition).getState();
   }
-
-
-  public Map<Integer, AdderBusinessLogic> getState()
-  {
-    return state;
-  }
-
-  public AdderBusinessLogic getState(Integer partition)
-  {
-    return state.get(partition);
-  }
 }