All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.streamnative.pulsar.handlers.kop.utils.TopicUtils Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2019 - 2024 StreamNative, Inc.. All Rights Reserved.
 */
package io.streamnative.pulsar.handlers.kop.utils;

import java.util.concurrent.CompletableFuture;
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.broker.PulsarService;
import org.apache.pulsar.common.naming.TopicName;

@Slf4j
public class TopicUtils {

    public static CompletableFuture isTopicExists(PulsarService pulsar, String topic) {
        CompletableFuture future = new CompletableFuture<>();
        TopicName topicName = TopicName.get(topic);
        pulsar.getBrokerService().fetchPartitionedTopicMetadataAsync(TopicName.get(topic))
            .whenComplete((metadata, ex) -> {
                if (ex != null) {
                    log.error("Fetch partitioned topic metadata has exception.", ex);
                    future.complete(true);
                    return;
                }
                if (metadata.partitions == 0) {
                    pulsar.getNamespaceService().checkTopicExists(topicName)
                        .thenAccept(topicExistsInfo -> {
                            try {
                                future.complete(topicExistsInfo.isExists());
                            } finally {
                                topicExistsInfo.recycle();
                            }
                        })
                        .exceptionally(throwable -> {
                            log.error("Check topic exists has exception.", throwable);
                            future.complete(true);
                            return null;
                        });
                    return;
                }
                future.complete(true);
            });
        return future;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy