public Set<String> listTopics() throws IOException { ListTopicsResponse listTopicsResponse = client.projects().topics().list(getProjectPath()).execute(); List<Topic> topics = listTopicsResponse.getTopics(); Set<String> topicsName = new HashSet<>(); for (Topic topic : topics) { String topicName = topic.getName(); String[] split = topicName.split("/"); topicsName.add(split[3]); } return topicsName; }
@Override public List<TopicPath> listTopics(ProjectPath project) throws IOException { Topics.List request = pubsub.projects().topics().list(project.getPath()); ListTopicsResponse response = request.execute(); if (response.getTopics() == null || response.getTopics().isEmpty()) { return ImmutableList.of(); } List<TopicPath> topics = new ArrayList<>(response.getTopics().size()); while (true) { for (Topic topic : response.getTopics()) { topics.add(topicPathFromPath(topic.getName())); } if (Strings.isNullOrEmpty(response.getNextPageToken())) { break; } request.setPageToken(response.getNextPageToken()); response = request.execute(); } return topics; }
/** Create a topic if it doesn't exist. */ public static void createTopic(Pubsub client, String fullTopicName) throws IOException { System.out.println("fullTopicName " + fullTopicName); try { client.projects().topics().get(fullTopicName).execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) { Topic topic = client.projects().topics().create(fullTopicName, new Topic()).execute(); System.out.printf("Topic %s was created.%n", topic.getName()); } } } }
/** * Creates a new topic with a given name. * * @param client Cloud Pub/Sub client. * @param args Command line arguments. * @throws IOException when Cloud Pub/Sub API calls fail. */ public static void createTopic(final Pubsub client, final String[] args) throws IOException { Main.checkArgsLength(args, 3); String topicName = PubsubUtils.getFullyQualifiedResourceName( PubsubUtils.ResourceType.TOPIC, args[0], args[2]); Topic topic = client.projects().topics() .create(topicName, new Topic()) .execute(); System.out.printf("Topic %s was created.\n", topic.getName()); }
for (Topic topic : response.getTopics()) { hasTopics = true; System.out.println(topic.getName());