X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FEndlessConsumer.java;h=b152310b3441316032b59ba8ff7948d3291c8c84;hb=3c1d3fa68df685146bdef7cc2e396e55fa0933dc;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..b152310 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)); }); } }); @@ -125,6 +135,8 @@ public class EndlessConsumer implements Runnable String key = record.key() == null ? "NULL" : record.key(); seen.get(partition).increment(key); } + + seen.forEach((tp, statistics) -> repository.save(new StatisticsDocument(statistics, consumer.position(tp)))); } } catch(WakeupException e)