X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FEndlessConsumer.java;h=69c82943522b50e30d228576d186d8bc86d37142;hb=cb8505576575efa6957214c4d6cc4be777fd3b21;hp=c7bc852ccf53e0a3e12c9216dae13e0b257b5f49;hpb=149fa27c8d1fd65c4b9510ff34ae44b11529e549;p=demos%2Fkafka%2Ftraining diff --git a/src/main/java/de/juplo/kafka/EndlessConsumer.java b/src/main/java/de/juplo/kafka/EndlessConsumer.java index c7bc852..69c8294 100644 --- a/src/main/java/de/juplo/kafka/EndlessConsumer.java +++ b/src/main/java/de/juplo/kafka/EndlessConsumer.java @@ -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; @@ -61,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()); @@ -87,6 +91,7 @@ public class EndlessConsumer implements Runnable removed.getPartition(), counter.getKey()); } + repository.save(new StatisticsDocument(removed, consumer.position(tp))); }); } @@ -96,7 +101,12 @@ public class EndlessConsumer implements Runnable partitions.forEach(tp -> { log.info("{} - adding partition: {}", id, tp); - seen.put(tp, new PartitionStatistics(tp)); + StatisticsDocument document = + repository + .findById(tp.toString()) + .orElse(new StatisticsDocument(tp)); + consumer.seek(tp, document.offset); + seen.put(tp, new PartitionStatistics(document)); }); } });