io.quarkus.kafka.streams.runtime.KafkaStreamsTopologyManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-kafka-streams Show documentation
Show all versions of quarkus-kafka-streams Show documentation
Implement stream processing applications based on Apache Kafka
package io.quarkus.kafka.streams.runtime;
import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.clients.admin.ListTopicsResult;
import org.jboss.logging.Logger;
public class KafkaStreamsTopologyManager {
private static final Logger LOGGER = Logger.getLogger(KafkaStreamsTopologyManager.class.getName());
private final Admin adminClient;
public KafkaStreamsTopologyManager(Admin adminClient) {
this.adminClient = adminClient;
}
public Set getMissingTopics(Collection topicsToCheck) throws InterruptedException {
return getMissingTopics(topicsToCheck, Duration.ofSeconds(10)); // keep defaults
}
public Set getMissingTopics(Collection topicsToCheck, Duration timeout) throws InterruptedException {
Set missing = new LinkedHashSet<>(topicsToCheck);
try {
ListTopicsResult topics = adminClient.listTopics();
Set topicNames = topics.names().get(timeout.toMillis(), TimeUnit.MILLISECONDS);
if (topicNames.containsAll(topicsToCheck)) {
return Collections.emptySet();
} else {
missing.removeAll(topicNames);
}
} catch (ExecutionException | TimeoutException e) {
LOGGER.error("Failed to get topic names from broker", e);
}
return missing;
}
}