X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FEndlessConsumer.java;h=0c107f3b261965f77cf4ac9f327ca510d39a3458;hb=2d84eda74475aaffff11ddfebe56d309b9aff2e9;hp=7e243a9837f4b13d023a7f20c976fdc3a23c9817;hpb=34d37c55d7cf830c6d2bdaf747f0938eb557bef3;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 7e243a9..0c107f3 100644 --- a/src/main/java/de/juplo/kafka/EndlessConsumer.java +++ b/src/main/java/de/juplo/kafka/EndlessConsumer.java @@ -22,11 +22,10 @@ import java.util.concurrent.locks.ReentrantLock; public class EndlessConsumer implements ConsumerRebalanceListener, Runnable { private final ExecutorService executor; - private final PartitionStatisticsRepository repository; private final String id; private final String topic; private final Consumer consumer; - private final java.util.function.Consumer> handler; + private final RecordHandler handler; private final Lock lock = new ReentrantLock(); private final Condition condition = lock.newCondition(); @@ -34,50 +33,17 @@ public class EndlessConsumer implements ConsumerRebalanceListener, Runnabl private Exception exception; private long consumed = 0; - private final Map> seen = new HashMap<>(); - @Override public void onPartitionsRevoked(Collection partitions) { - partitions.forEach(tp -> - { - Integer partition = tp.partition(); - Long newOffset = consumer.position(tp); - log.info( - "{} - removing partition: {}, offset of next message {})", - id, - partition, - newOffset); - Map removed = seen.remove(partition); - for (String key : removed.keySet()) - { - log.info( - "{} - Seen {} messages for partition={}|key={}", - id, - removed.get(key), - partition, - key); - } - repository.save(new StatisticsDocument(partition, removed, consumer.position(tp))); - }); + partitions.forEach(tp -> handler.onPartitionRevoked(tp)); } @Override public void onPartitionsAssigned(Collection partitions) { - partitions.forEach(tp -> - { - Integer partition = tp.partition(); - Long offset = consumer.position(tp); - log.info("{} - adding partition: {}, offset={}", id, partition, offset); - StatisticsDocument document = - repository - .findById(Integer.toString(partition)) - .orElse(new StatisticsDocument(partition)); - consumer.seek(tp, document.offset); - seen.put(partition, document.statistics); - }); + partitions.forEach(tp -> handler.onPartitionAssigned(tp)); } @@ -111,24 +77,9 @@ public class EndlessConsumer implements ConsumerRebalanceListener, Runnabl handler.accept(record); consumed++; - - Integer partition = record.partition(); - String key = record.key() == null ? "NULL" : record.key().toString(); - Map byKey = seen.get(partition); - - if (!byKey.containsKey(key)) - byKey.put(key, 0l); - - long seenByKey = byKey.get(key); - seenByKey++; - byKey.put(key, seenByKey); } - seen.forEach((partiton, statistics) -> repository.save( - new StatisticsDocument( - partiton, - statistics, - consumer.position(new TopicPartition(topic, partiton))))); + handler.beforeNextPoll(); } } catch(WakeupException e) @@ -196,11 +147,6 @@ public class EndlessConsumer implements ConsumerRebalanceListener, Runnabl } } - public Map> getSeen() - { - return seen; - } - public void start() { lock.lock(); @@ -220,7 +166,7 @@ public class EndlessConsumer implements ConsumerRebalanceListener, Runnabl } } - public synchronized void stop() throws ExecutionException, InterruptedException + public synchronized void stop() throws InterruptedException { lock.lock(); try @@ -243,22 +189,7 @@ public class EndlessConsumer implements ConsumerRebalanceListener, Runnabl public void destroy() throws ExecutionException, InterruptedException { log.info("{} - Destroy!", id); - try - { - stop(); - } - catch (IllegalStateException e) - { - log.info("{} - Was already stopped", id); - } - catch (Exception e) - { - log.error("{} - Unexpected exception while trying to stop the consumer", id, e); - } - finally - { - log.info("{}: Consumed {} messages in total, exiting!", id, consumed); - } + log.info("{}: Consumed {} messages in total, exiting!", id, consumed); } public boolean running()