X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FEndlessConsumer.java;h=b152310b3441316032b59ba8ff7948d3291c8c84;hb=3c1d3fa68df685146bdef7cc2e396e55fa0933dc;hp=e67bf412a1bae6762c7270935079e47f57716a1f;hpb=915674ec49ba38b3716cc4ef53272e963f139677;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 e67bf41..b152310 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)); }); } }); @@ -134,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)