From cb8505576575efa6957214c4d6cc4be777fd3b21 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Wed, 6 Apr 2022 23:35:27 +0200 Subject: [PATCH] Offset-Position wird in der MongoDB gespeichert --- src/main/java/de/juplo/kafka/EndlessConsumer.java | 11 ++++++----- src/main/java/de/juplo/kafka/StatisticsDocument.java | 12 +++++++++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/juplo/kafka/EndlessConsumer.java b/src/main/java/de/juplo/kafka/EndlessConsumer.java index e67bf41..69c8294 100644 --- a/src/main/java/de/juplo/kafka/EndlessConsumer.java +++ b/src/main/java/de/juplo/kafka/EndlessConsumer.java @@ -64,6 +64,7 @@ public class EndlessConsumer implements Runnable props.put("bootstrap.servers", bootstrapServer); props.put("group.id", groupId); props.put("client.id", id); + props.put("enable.auto.commit", false); props.put("auto.offset.reset", autoOffsetReset); props.put("metadata.max.age.ms", "1000"); props.put("key.deserializer", StringDeserializer.class.getName()); @@ -90,7 +91,7 @@ public class EndlessConsumer implements Runnable removed.getPartition(), counter.getKey()); } - repository.save(new StatisticsDocument(removed)); + repository.save(new StatisticsDocument(removed, consumer.position(tp))); }); } @@ -100,12 +101,12 @@ public class EndlessConsumer implements Runnable partitions.forEach(tp -> { log.info("{} - adding partition: {}", id, tp); - seen.put( - tp, + StatisticsDocument document = repository .findById(tp.toString()) - .map(PartitionStatistics::new) - .orElse(new PartitionStatistics(tp))); + .orElse(new StatisticsDocument(tp)); + consumer.seek(tp, document.offset); + seen.put(tp, new PartitionStatistics(document)); }); } }); diff --git a/src/main/java/de/juplo/kafka/StatisticsDocument.java b/src/main/java/de/juplo/kafka/StatisticsDocument.java index 9318c4c..e8c2e9b 100644 --- a/src/main/java/de/juplo/kafka/StatisticsDocument.java +++ b/src/main/java/de/juplo/kafka/StatisticsDocument.java @@ -1,6 +1,7 @@ package de.juplo.kafka; import lombok.ToString; +import org.apache.kafka.common.TopicPartition; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @@ -16,23 +17,32 @@ public class StatisticsDocument public String id; public String topic; public Integer partition; + public long offset; public Map statistics; public StatisticsDocument() { } + public StatisticsDocument(TopicPartition tp) + { + this.topic = tp.topic(); + this.partition = tp.partition(); + this.offset = 0; + } + public StatisticsDocument(String topic, Integer partition, Map statistics) { this.partition = partition; this.statistics = statistics; } - public StatisticsDocument(PartitionStatistics statistics) + public StatisticsDocument(PartitionStatistics statistics, long offset) { this.topic = statistics.getPartition().topic(); this.id = statistics.toString(); this.partition = statistics.getPartition().partition(); + this.offset = offset; this.statistics = new HashMap<>(); statistics.getStatistics().forEach(counter -> this.statistics.put(counter.getKey(), counter.getResult())); } -- 2.20.1