io.streamnative.pulsar.handlers.kop.utils.TopicUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pulsar-protocol-handler-kafka Show documentation
Show all versions of pulsar-protocol-handler-kafka Show documentation
Kafka on Pulsar implemented using Pulsar Protocol Handler
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;
}
}