/** * 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()); }
@Override public void createTopic(TopicPath topic) throws IOException { pubsub .projects() .topics() .create(topic.getPath(), new Topic()) .execute(); // ignore Topic result. }
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; }
try { pubsub.projects().topics() .create("projects/" + projectName + "/topics/" + topic, new Topic()).execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() != ALREADY_EXISTS) {
@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()); } } } }
private void setupPubsubTopic(String topic) throws IOException { if (pubsubClient == null) { pubsubClient = newPubsubClient(options.as(PubsubOptions.class)).build(); } if (executeNullIfNotFound(pubsubClient.projects().topics().get(topic)) == null) { pubsubClient.projects().topics().create(topic, new Topic().setName(topic)).execute(); } }
public void createTopic(String topic) throws IOException { client.projects().topics().create(getTopicPath(topic), new Topic()).execute(); }
for (Topic topic : response.getTopics()) { hasTopics = true; System.out.println(topic.getName());