Search in sources :

Example 1 with Helper

use of io.vertx.kafka.client.common.impl.Helper in project hono by eclipse.

the class HonoKafkaConsumerHelper method partitionsFor.

/**
 * Get metadata about the partitions for a given topic.
 * <p>
 * This method is adapted from {@code io.vertx.kafka.client.consumer.impl.KafkaConsumerImpl#partitionsFor(String, Handler)}
 * and fixes an NPE in case {@code KafkaConsumer#partitionsFor(String)} returns {@code null}
 * (happens if "auto.create.topics.enable" is false).
 * <p>
 * This method will become obsolete when updating to a Kafka client in which https://issues.apache.org/jira/browse/KAFKA-12260
 * ("PartitionsFor should not return null value") is solved.
 * TODO remove this method once updated Kafka client is used
 *
 * @param kafkaConsumer The KafkaConsumer to use.
 * @param topic The topic to get partitions info for.
 * @return The result Future.
 * @throws NullPointerException if any of the parameters is {@code null}.
 */
public static Future<List<PartitionInfo>> partitionsFor(final KafkaConsumer<?, ?> kafkaConsumer, final String topic) {
    Objects.requireNonNull(kafkaConsumer);
    Objects.requireNonNull(topic);
    final Promise<List<PartitionInfo>> handler = Promise.promise();
    kafkaConsumer.asStream().partitionsFor(topic, done -> {
        if (done.succeeded()) {
            if (done.result() == null) {
                handler.handle(Future.succeededFuture(List.of()));
            } else {
                final List<PartitionInfo> partitions = new ArrayList<>();
                for (final org.apache.kafka.common.PartitionInfo kafkaPartitionInfo : done.result()) {
                    final PartitionInfo partitionInfo = new PartitionInfo();
                    partitionInfo.setInSyncReplicas(Stream.of(kafkaPartitionInfo.inSyncReplicas()).map(Helper::from).collect(Collectors.toList())).setLeader(Helper.from(kafkaPartitionInfo.leader())).setPartition(kafkaPartitionInfo.partition()).setReplicas(Stream.of(kafkaPartitionInfo.replicas()).map(Helper::from).collect(Collectors.toList())).setTopic(kafkaPartitionInfo.topic());
                    partitions.add(partitionInfo);
                }
                handler.handle(Future.succeededFuture(partitions));
            }
        } else {
            handler.handle(Future.failedFuture(done.cause()));
        }
    });
    return handler.future();
}
Also used : Helper(io.vertx.kafka.client.common.impl.Helper) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PartitionInfo(io.vertx.kafka.client.common.PartitionInfo)

Aggregations

PartitionInfo (io.vertx.kafka.client.common.PartitionInfo)1 Helper (io.vertx.kafka.client.common.impl.Helper)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1