X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FEndlessConsumer.java;h=e3a60b5b796fea7dbeb4c2434c3daeef63976f84;hb=32a052b7c494009c59190857984ef3563f4f2b14;hp=063a09ebcc3e9ced137fda80af7d962c6f849580;hpb=808bd074aaae940e5f81bc9f09c42d48d1fd2670;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 063a09e..e3a60b5 100644 --- a/src/main/java/de/juplo/kafka/EndlessConsumer.java +++ b/src/main/java/de/juplo/kafka/EndlessConsumer.java @@ -1,18 +1,17 @@ package de.juplo.kafka; import lombok.extern.slf4j.Slf4j; +import org.apache.kafka.clients.consumer.ConsumerRebalanceListener; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; import org.apache.kafka.clients.consumer.KafkaConsumer; +import org.apache.kafka.common.TopicPartition; import org.apache.kafka.common.errors.WakeupException; import org.apache.kafka.common.serialization.StringDeserializer; import javax.annotation.PreDestroy; import java.time.Duration; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; +import java.util.*; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; @@ -34,7 +33,7 @@ public class EndlessConsumer implements Runnable private KafkaConsumer consumer = null; private Future future = null; - private Map> seen; + private final Map seen = new HashMap<>(); public EndlessConsumer( @@ -63,15 +62,44 @@ public class EndlessConsumer implements Runnable props.put("group.id", groupId); props.put("client.id", id); props.put("auto.offset.reset", autoOffsetReset); + props.put("metadata.max.age.ms", "1000"); props.put("key.deserializer", StringDeserializer.class.getName()); props.put("value.deserializer", StringDeserializer.class.getName()); this.consumer = new KafkaConsumer<>(props); log.info("{} - Subscribing to topic {}", id, topic); - consumer.subscribe(Arrays.asList(topic)); + consumer.subscribe(Arrays.asList(topic), new ConsumerRebalanceListener() + { + @Override + public void onPartitionsRevoked(Collection partitions) + { + partitions.forEach(tp -> + { + log.info("{} - removing partition: {}", id, tp); + PartitionStatistics removed = seen.remove(tp); + for (KeyCounter counter : removed.getStatistics()) + { + log.info( + "{} - Seen {} messages for partition={}|key={}", + id, + counter.getCounter(), + removed.getPartition(), + counter.getKey()); + } + }); + } - seen = new HashMap<>(); + @Override + public void onPartitionsAssigned(Collection partitions) + { + partitions.forEach(tp -> + { + log.info("{} - adding partition: {}", id, tp); + seen.put(tp, new PartitionStatistics(tp)); + }); + } + }); while (true) { @@ -93,20 +121,9 @@ public class EndlessConsumer implements Runnable record.value() ); - Integer partition = record.partition(); - String key = record.key(); - - if (!seen.containsKey(partition)) - seen.put(partition, new HashMap<>()); - - Map byKey = seen.get(partition); - - if (!byKey.containsKey(key)) - byKey.put(key, 0); - - int seenByKey = byKey.get(key); - seenByKey++; - byKey.put(key, seenByKey); + TopicPartition partition = new TopicPartition(record.topic(), record.partition()); + String key = record.key() == null ? "NULL" : record.key(); + seen.get(partition).increment(key); } } } @@ -123,27 +140,11 @@ public class EndlessConsumer implements Runnable { log.info("{} - Closing the KafkaConsumer", id); consumer.close(); - - for (Integer partition : seen.keySet()) - { - Map byKey = seen.get(partition); - for (String key : byKey.keySet()) - { - log.info( - "{} - Seen {} messages for partition={}|key={}", - id, - byKey.get(key), - partition, - key); - } - } - seen = null; - log.info("{} - Consumer-Thread exiting", id); } } - public Map> getSeen() + public Map getSeen() { return seen; }