private void tryDelete(AdminClient adminClient, String topic) throws Exception { try { adminClient.deleteTopics(Collections.singleton(topic)).all().get(DELETE_TIMEOUT_SECONDS, TimeUnit.SECONDS); } catch (TimeoutException e) { LOG.info("Did not receive delete topic response within %d seconds. Checking if it succeeded", DELETE_TIMEOUT_SECONDS); if (adminClient.listTopics().names().get(DELETE_TIMEOUT_SECONDS, TimeUnit.SECONDS).contains(topic)) { throw new Exception("Topic still exists after timeout"); } } }
@Override public boolean conditionMet() { return result.listings().isDone(); } }, "Timed out waiting for listTopics to complete");
@Override public ListTopicsResult listTopics(ListTopicsOptions options) { Map<String, TopicListing> topicListings = new HashMap<>(); if (timeoutNextRequests > 0) { KafkaFutureImpl<Map<String, TopicListing>> future = new KafkaFutureImpl<>(); future.completeExceptionally(new TimeoutException()); --timeoutNextRequests; return new ListTopicsResult(future); } for (Map.Entry<String, TopicMetadata> topicDescription : allTopics.entrySet()) { String topicName = topicDescription.getKey(); topicListings.put(topicName, new TopicListing(topicName, topicDescription.getValue().isInternalTopic)); } KafkaFutureImpl<Map<String, TopicListing>> future = new KafkaFutureImpl<>(); future.complete(topicListings); return new ListTopicsResult(future); }
allTopics.addAll(kafkaAdminClient.listTopics().names().get(60, TimeUnit.SECONDS));
TestUtils.assertFutureError(result.listings(), TimeoutException.class); log.info("Verified the error result of AdminClient#listTopics");
@Override public ListTopicsResult listTopics(final ListTopicsOptions options) { final KafkaFutureImpl<Map<String, TopicListing>> topicListingFuture = new KafkaFutureImpl<>(); final long now = time.milliseconds(); runnable.call(new Call("listTopics", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) { @Override AbstractRequest.Builder createRequest(int timeoutMs) { return MetadataRequest.Builder.allTopics(); } @Override void handleResponse(AbstractResponse abstractResponse) { MetadataResponse response = (MetadataResponse) abstractResponse; Map<String, TopicListing> topicListing = new HashMap<>(); for (MetadataResponse.TopicMetadata topicMetadata : response.topicMetadata()) { String topicName = topicMetadata.topic(); boolean isInternal = topicMetadata.isInternal(); if (!topicMetadata.isInternal() || options.shouldListInternal()) topicListing.put(topicName, new TopicListing(topicName, isInternal)); } topicListingFuture.complete(topicListing); } @Override void handleFailure(Throwable throwable) { topicListingFuture.completeExceptionally(throwable); } }, now); return new ListTopicsResult(topicListingFuture); }
allTopics.addAll(kafkaAdminClient.listTopics().names().get(60, TimeUnit.SECONDS));
/** * Get information about all topics in Kafka. * @return Set of topics found in Kafka. */ public List<TopicListing> getTopics() { try (final AdminClient adminClient = getAdminClient()) { return new ArrayList<>(adminClient.listTopics().listings().get()); } catch (final InterruptedException | ExecutionException e) { throw new RuntimeException(e.getMessage(), e); } }
allTopics.addAll(kafkaAdminClient.listTopics().names().get(60, TimeUnit.SECONDS));
/** * Lists non-internal topics in a cluster. * * @param timeoutMillis The timeout to wait for. * @return A set of topic names. * @throws ExecutionException If an unexpected error occurred. * @throws InterruptedException If the thread was interrupted. * @throws TimeoutException If a timeout occurred. */ public Set<String> listTopics(int timeoutMillis) throws ExecutionException, TimeoutException, InterruptedException { return runWithRetry(() -> { final ListTopicsResult result = admin.listTopics(new ListTopicsOptions().timeoutMs(timeoutMillis)); awaitFutures(timeoutMillis, result.listings()); final Collection<TopicListing> listings = result.listings().get(); return listings.stream().map(TopicListing::name).collect(Collectors.toSet()); }); }
private void waitForTopics(String bootstrapServers) throws Exception { while (true) { TimeUnit.SECONDS.sleep(5); Properties properties = new Properties(); properties.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); AdminClient adminClient = AdminClient.create(properties); Set<String> topics = adminClient.listTopics().names().get(); if (topics.contains(Member.class.getSimpleName())) { return; } System.out.println("Waiting for data"); } }
@Override boolean runInternal(StopWatch stopWatch) throws InterruptedException, ExecutionException { stopWatch.start("adminClient.listTopics()"); Collection<String> topicNames = adminClient.listTopics().listings().get() .stream().map(TopicListing::name).filter(this::shouldCollectEvent).collect(Collectors.toList()); topicsMap.removeAll(new RemoveTopicPredicate(topicNames)); DescribeTopicsResult describeTopicsResult = adminClient.describeTopics(topicNames); describeTopicsResult.all().get().forEach( (topic, topicDescription) -> topicsMap.executeOnKey(topic, new SetTopicPartitionsProcessor( topicDescription.partitions().stream().map(TopicPartitionInfo::partition).collect(Collectors.toList())) ) ); metaMap.set(this.getName() + TopicServiceScheduler.LAST_SUCCESS_PREFIX, System.currentTimeMillis()); log.debug("Topics:" + topicsMap.entrySet()); log.debug(stopWatch.prettyPrint()); return true; }
@Override public List<String> getMessageTopics() { List<String> list = new ArrayList<>(); try { list = new ArrayList<>(kafkaClient.listTopics().names().get()); } catch (final InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (final ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } Collections.sort(list); return list; }
/** * Retrieve all available topics within cluster. Includes internal topics. */ public TopicList getAvailableTopics() { final List<org.sourcelab.kafka.webview.ui.manager.kafka.dto.TopicListing> topicListings = new ArrayList<>(); try { // We want to show all topics, including internal topics. final ListTopicsOptions listTopicsOptions = new ListTopicsOptions().listInternal(true); final Collection<TopicListing> results = adminClient .listTopics(listTopicsOptions) .listings() .get(); for (final TopicListing entry: results) { topicListings.add( new org.sourcelab.kafka.webview.ui.manager.kafka.dto.TopicListing(entry.name(), entry.isInternal()) ); } return new TopicList(topicListings); } catch (InterruptedException | ExecutionException e) { // TODO Handle throw new RuntimeException(e.getMessage(), e); } }
private void waitForTopics(String bootstrapServers) throws Exception { while (true) { TimeUnit.SECONDS.sleep(5); Properties properties = new Properties(); properties.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); AdminClient adminClient = AdminClient.create(properties); Set<String> topics = adminClient.listTopics().names().get(); if (topics.contains(RandomProducerAction.class.getSimpleName())) { return; } System.out.println("Waiting for data"); } }
private void waitForTopics(String bootstrapServers) throws Exception { while (true) { TimeUnit.SECONDS.sleep(5); Properties properties = new Properties(); properties.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); AdminClient adminClient = AdminClient.create(properties); if (adminClient.listTopics().names().get().containsAll(TOPICS)) { return; } System.out.println("Waiting for data"); } }
/** * List all names of topics in Kafka. * @return Set of topics found in Kafka. */ public Set<String> getTopicNames() { try (final AdminClient adminClient = getAdminClient()) { return adminClient.listTopics().names().get(); } catch (final InterruptedException | ExecutionException e) { throw new RuntimeException(e.getMessage(), e); } }
@Override public void listTopics(Handler<AsyncResult<Set<String>>> handler) { LOGGER.debug("Listing topics"); ListTopicsResult future = adminClient.listTopics(); queueWork(new UniWork<>("listTopics", future.names(), handler)); }
public Set<String> listTopics() { try { return adminClient.listTopics().names().get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new StreamRuntimeException(e); } catch (ExecutionException e) { throw new StreamRuntimeException(e); } }
public Set<String> topics() { KafkaFuture<Set<String>> names = adminClient.listTopics().names(); try { KafkaFuture.allOf(names).get(10, TimeUnit.SECONDS); Set<String> topics = names.get(); log.debug("Got topics: {}", topics); return topics; } catch (InterruptedException | ExecutionException | TimeoutException e) { throw new IllegalStateException("Exception occured during topic retrieval.", e); } }