X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FApplicationRecordHandler.java;h=bc18d59b6596507d9ebc03678ceb2b3f487512e4;hb=refs%2Ftags%2Fsumup-adder--drop-duplicates---lvm-2-tage;hp=d0d385ca9f3ed0f94da4a5af058f1da0adc8a19e;hpb=a2e8fc924e5b472d6b90c42d311514f91ea452f1;p=demos%2Fkafka%2Ftraining diff --git a/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java b/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java index d0d385c..bc18d59 100644 --- a/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java +++ b/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java @@ -1,16 +1,25 @@ package de.juplo.kafka; +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 @Slf4j public class ApplicationRecordHandler implements RecordHandler { + private final AdderResults results; + private final Optional throttle; + private final String id; + private final Map state = new HashMap<>(); + private final Map next = new HashMap<>(); @Override @@ -19,31 +28,55 @@ public class ApplicationRecordHandler implements RecordHandler Integer partition = record.partition(); String user = record.key(); String message = record.value(); - switch (message) + + if (record.offset() < next.get(partition)) + { + log.warn( + "{}- Dropping duplicate message: offset={} < next={}", + id, + record.offset(), + next.get(partition)); + return; + } + + 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 { - case "START": - state.get(partition).startSum(user); - break; - - case "END": - Long result = state.get(partition).endSum(user); - log.info("New result for {}: {}", user, result); - break; - - default: - state.get(partition).addToSum(user, Integer.parseInt(message)); - break; + state.get(partition).addToSum(user, Integer.parseInt(message)); + } + + next.put(partition, record.offset() + 1); + + 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 state) + protected void addPartition(Integer partition, Map state, Long offset) { this.state.put(partition, new AdderBusinessLogic(state)); + this.next.put(partition, offset); } - protected Map removePartition(Integer partition) + protected ApplicationState removePartition(Integer partition) { - return this.state.remove(partition).getState(); + ApplicationState state = getState(partition); + this.next.remove(partition); + this.state.remove(partition); + return state; } @@ -51,4 +84,12 @@ public class ApplicationRecordHandler implements RecordHandler { return state; } + + public ApplicationState getState(Integer partition) + { + return + new ApplicationState( + this.next.get(partition), + this.state.get(partition).getState()); + } }