Skip to content

Commit

Permalink
pubsub snippet updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jabubake committed Feb 27, 2017
1 parent 5da936a commit e053154
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,25 @@ public PublisherClientSnippets(String projectId) {
}

/** Example of creating a topic. */
public Topic createTopic(String topicName) throws Exception {
public Topic createTopic(String myTopic) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
// [START createTopic]
TopicName formattedTopicName = TopicName.create(projectId, topicName);
Topic topic = publisherClient.createTopic(formattedTopicName);
TopicName topicName = TopicName.create(projectId, myTopic);
Topic topic = publisherClient.createTopic(topicName);
// [END createTopic]
return topic;
}
}

/** Example of listing topics, specifying the page size. */
/** Example of listing topics. */
public ListTopicsPagedResponse listTopics() throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
// [START listTopics]
ListTopicsRequest listTopicsRequest =
ListTopicsRequest.newBuilder()
.setProjectWithProjectName(ProjectName.create(projectId))
.setPageSize(100)
.build();
ListTopicsPagedResponse response =
publisherClient.listTopics(listTopicsRequest);
ListTopicsPagedResponse response = publisherClient.listTopics(listTopicsRequest);
Iterable<Topic> topics = response.iterateAllElements();
for (Topic topic : topics) {
// do something with the topic
Expand All @@ -72,87 +70,43 @@ public ListTopicsPagedResponse listTopics() throws Exception {
}
}

/** Example of listing topics, specifying the page size and a page token. */
public ListTopicsPagedResponse listTopicsWithPageToken(String pageToken) throws Exception {
/** Example of listing topics for a subscription. */
public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String myTopic)
throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
// [START listTopicsWithPageToken]
ListTopicsRequest listTopicsRequest =
ListTopicsRequest.newBuilder()
.setProjectWithProjectName(ProjectName.create(projectId))
.setPageSize(100)
.setPageToken(pageToken)
.build();
ListTopicsPagedResponse response =
publisherClient.listTopics(listTopicsRequest);
Iterable<Topic> topics = response.iterateAllElements();
for (Topic topic : topics) {
// do something with the topic
}
// [END listTopicsWithPageToken]
return response;
}
}

/** Example of listing topics for a subscription, specifying the page size. */
public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicName) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
TopicName formattedTopicName = TopicName.create(projectId, topicName);
// [START listTopicSubscriptions]
TopicName topicName = TopicName.create(projectId, myTopic);
ListTopicSubscriptionsRequest request =
ListTopicSubscriptionsRequest.newBuilder()
.setTopicWithTopicName(formattedTopicName)
.setPageSize(100)
.setTopicWithTopicName(topicName)
.build();
ListTopicSubscriptionsPagedResponse response =
publisherClient.listTopicSubscriptions(request);
Iterable<String> subscriptions = response.iterateAllElements();
for (String subscription : subscriptions) {
Iterable<String> subscriptionNames = response.iterateAllElements();
for (String subscriptionName : subscriptionNames) {
// do something with the subscription name
}
// [END listTopicSubscriptions]
return response;
}
}

/** Example of listing topics for a subscription, specifying the page size and page token */
public ListTopicSubscriptionsPagedResponse listTopicSubscriptionsWithPageToken(
String topicName, String pageToken) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
TopicName formattedTopicName = TopicName.create(projectId, topicName);
// [START listTopicSubscriptionsWithPageToken]
ListTopicSubscriptionsRequest request =
ListTopicSubscriptionsRequest.newBuilder()
.setTopicWithTopicName(formattedTopicName)
.setPageSize(100)
.setPageToken(pageToken)
.build();
ListTopicSubscriptionsPagedResponse response =
publisherClient.listTopicSubscriptions(request);
Iterable<String> subscriptions = response.iterateAllElements();
for (String subscription : subscriptions) {
// do something with the subscription name
}
// [END listTopicSubscriptionsWithPageToken]
return response;
}
}

/** Example of deleting a topic. */
public TopicName deleteTopic(String topicName) throws Exception {
public TopicName deleteTopic(String myTopic) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
TopicName formattedTopicName = TopicName.create(projectId, topicName);
// [START deleteTopic]
publisherClient.deleteTopic(formattedTopicName);
TopicName topicName = TopicName.create(projectId, myTopic);
publisherClient.deleteTopic(topicName);
// [END deleteTopic]
return formattedTopicName;
return topicName;
}
}

/** Example of getting a topic policy. */
public Policy getTopicPolicy(String name) throws Exception {
public Policy getTopicPolicy(String myTopic) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
TopicName topicName = TopicName.create(projectId, name);
// [START getTopicPolicy]
TopicName topicName = TopicName.create(projectId, myTopic);
Policy policy = publisherClient.getIamPolicy(topicName.toString());
if (policy == null) {
// topic iam policy was not found
Expand All @@ -163,10 +117,10 @@ public Policy getTopicPolicy(String name) throws Exception {
}

/** Example of replacing a topic policy. */
public Policy replaceTopicPolicy(String name) throws Exception {
public Policy replaceTopicPolicy(String myTopic) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
String topicName = TopicName.create(projectId, name).toString();
// [START replaceTopicPolicy]
String topicName = TopicName.create(projectId, myTopic).toString();
Policy policy = publisherClient.getIamPolicy(topicName);
// add role -> members binding
Binding binding =
Expand All @@ -184,25 +138,25 @@ public Policy replaceTopicPolicy(String name) throws Exception {

/** Example of testing whether the caller has the provided permissions on a topic.
* Only viewer, editor or admin/owner can view results of pubsub.topics.get */
public TestIamPermissionsResponse testTopicPermissions(String topicName) throws Exception {
public TestIamPermissionsResponse testTopicPermissions(String myTopic) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
TopicName formattedTopicName = TopicName.create(projectId, topicName);
// [START testTopicPermissions]
List<String> permissions = new LinkedList<>();
permissions.add("pubsub.topics.get");
TopicName topicName = TopicName.create(projectId, myTopic);
TestIamPermissionsResponse testedPermissions =
publisherClient.testIamPermissions(formattedTopicName.toString(), permissions);
publisherClient.testIamPermissions(topicName.toString(), permissions);
// [END testTopicPermissions]
return testedPermissions;
}
}

/** Example of getting a topic. */
public Topic getTopic(String topicName) throws Exception {
public Topic getTopic(String myTopic) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
// [START getTopic]
TopicName formattedTopicName = TopicName.create(projectId, topicName);
Topic topic = publisherClient.getTopic(formattedTopicName);
TopicName topicName = TopicName.create(projectId, myTopic);
Topic topic = publisherClient.getTopic(topicName);
// [END createTopic]
return topic;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.google.cloud.Identity;
import com.google.cloud.Role;
import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers;
import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListSubscriptionsPagedResponse;
import com.google.cloud.pubsub.spi.v1.PublisherClient;
import com.google.cloud.pubsub.spi.v1.SubscriberClient;
Expand Down Expand Up @@ -49,60 +48,56 @@ public SubscriberClientSnippets(String projectId) {
}

/** Example of creating a pull subscription for a topic. */
public Subscription createSubscription(String topic, String subscriptionName) throws Exception {
TopicName topicName = TopicName.create(projectId, topic);
SubscriptionName formattedSubscriptionName =
SubscriptionName.create(projectId, subscriptionName);
public Subscription createSubscription(String topic, String mySubscription) throws Exception {
try (SubscriberClient subscriberClient = SubscriberClient.create()) {
// [START createSubscription]
TopicName topicName = TopicName.create(projectId, topic);
SubscriptionName subscriptionName =
SubscriptionName.create(projectId, mySubscription);
Subscription subscription =
subscriberClient.createSubscription(
formattedSubscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
// [END createSubscription]
return subscription;
}
}

/** Example of pulling a maximum number of messages from a subscription. */
public PullResponse pull(String subscriptionName) throws Exception {
public PullResponse pull(String mySubscription) throws Exception {
try (SubscriberClient subscriberClient = SubscriberClient.create()) {
SubscriptionName subcriptionFormattedName =
SubscriptionName.create(projectId, subscriptionName);
// [START pull]
PullResponse response = subscriberClient.pull(subcriptionFormattedName, true, 100);
SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription);
PullResponse response = subscriberClient.pull(subscriptionName, true, 100);
for (ReceivedMessage message : response.getReceivedMessagesList()) {
// do something with message, then ack or nack
subscriberClient.acknowledge(
subcriptionFormattedName, Collections.singletonList(message.getAckId()));
subscriptionName, Collections.singletonList(message.getAckId()));
}
// [END pull]
return response;
}
}

/** Example of replacing the push configuration of a subscription, setting the push endpoint. */
public void replacePushConfig(String subscriptionName, String endpoint) throws Exception {
public void replacePushConfig(String mySubscription, String endpoint) throws Exception {
try (SubscriberClient subscriberClient = SubscriberClient.create()) {
SubscriptionName formattedSubscriptionName =
SubscriptionName.create(projectId, subscriptionName);
// [START replacePushConfig]
SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription);
PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build();
subscriberClient.modifyPushConfig(formattedSubscriptionName, pushConfig);
subscriberClient.modifyPushConfig(subscriptionName, pushConfig);
// [END replacePushConfig]
}
}

/** Example of listing subscriptions, specifying the page size. */
/** Example of listing subscriptions. */
public ListSubscriptionsPagedResponse listSubscriptions() throws Exception {
try (SubscriberClient subscriberClient = SubscriberClient.create()) {
ProjectName projectName = ProjectName.create(projectId);
// [START listSubscriptions]
ListSubscriptionsRequest listSubscriptionsRequest =
ListSubscriptionsRequest.newBuilder()
.setProjectWithProjectName(projectName)
.setPageSize(100)
.setProjectWithProjectName(ProjectName.create(projectId))
.build();
PagedResponseWrappers.ListSubscriptionsPagedResponse response =
ListSubscriptionsPagedResponse response =
subscriberClient.listSubscriptions(listSubscriptionsRequest);
Iterable<Subscription> subscriptions = response.iterateAllElements();
for (Subscription subscription : subscriptions) {
Expand All @@ -113,48 +108,23 @@ public ListSubscriptionsPagedResponse listSubscriptions() throws Exception {
}
}

/** Example of listing subscriptions, specifying the page size and page token. */
public ListSubscriptionsPagedResponse listSubscriptionsWithPageToken(String pageToken)
throws Exception {
try (SubscriberClient subscriberClient = SubscriberClient.create()) {
ProjectName projectName = ProjectName.create(projectId);
// [START listSubscriptionsWithPageToken]
ListSubscriptionsRequest listSubscriptionsRequest =
ListSubscriptionsRequest.newBuilder()
.setProjectWithProjectName(projectName)
.setPageSize(100)
.setPageToken(pageToken)
.build();
PagedResponseWrappers.ListSubscriptionsPagedResponse response =
subscriberClient.listSubscriptions(listSubscriptionsRequest);
Iterable<Subscription> subscriptions = response.iterateAllElements();
for (Subscription subscription : subscriptions) {
// do something with the subscription
}
// [END listSubscriptionsWithPageToken]
return response;
}
}

/** Example of deleting a subscription. */
public SubscriptionName deleteSubscription(String subscriptionName) throws Exception {
public SubscriptionName deleteSubscription(String mySubscription) throws Exception {
try (SubscriberClient subscriberClient = SubscriberClient.create()) {
SubscriptionName formattedSubscriptionName =
SubscriptionName.create(projectId, subscriptionName);
// [START deleteSubscription]
subscriberClient.deleteSubscription(formattedSubscriptionName);
SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription);
subscriberClient.deleteSubscription(subscriptionName);
// [END deleteSubscription]
return formattedSubscriptionName;
return subscriptionName;
}
}

/** Example of getting a subscription policy. */
public Policy getSubscriptionPolicy(String subscriptionName) throws Exception {
public Policy getSubscriptionPolicy(String mySubscription) throws Exception {
try (SubscriberClient subscriberClient = SubscriberClient.create()) {
SubscriptionName formattedSubscriptionName =
SubscriptionName.create(projectId, subscriptionName);
// [START getSubscriptionPolicy]
Policy policy = subscriberClient.getIamPolicy(formattedSubscriptionName.toString());
SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription);
Policy policy = subscriberClient.getIamPolicy(subscriptionName.toString());
if (policy == null) {
// subscription was not found
}
Expand All @@ -164,12 +134,11 @@ public Policy getSubscriptionPolicy(String subscriptionName) throws Exception {
}

/** Example of replacing a subscription policy. */
public Policy replaceSubscriptionPolicy(String subscriptionName) throws Exception {
public Policy replaceSubscriptionPolicy(String mySubscription) throws Exception {
try (SubscriberClient subscriberClient = SubscriberClient.create()) {
SubscriptionName formattedSubscriptionName =
SubscriptionName.create(projectId, subscriptionName);
// [START replaceSubscriptionPolicy]
Policy policy = subscriberClient.getIamPolicy(formattedSubscriptionName.toString());
SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription);
Policy policy = subscriberClient.getIamPolicy(subscriptionName.toString());
// Create a role => members binding
Binding binding =
Binding.newBuilder()
Expand All @@ -179,36 +148,33 @@ public Policy replaceSubscriptionPolicy(String subscriptionName) throws Exceptio
//Update policy
Policy updatedPolicy = policy.toBuilder().addBindings(binding).build();

updatedPolicy = subscriberClient.setIamPolicy(
formattedSubscriptionName.toString(), updatedPolicy);
updatedPolicy = subscriberClient.setIamPolicy(subscriptionName.toString(), updatedPolicy);
// [END replaceSubscriptionPolicy]
return updatedPolicy;
}
}

/** Example of testing whether the caller has the provided permissions on a subscription. */
public TestIamPermissionsResponse testSubscriptionPermissions(String subscriptionName)
public TestIamPermissionsResponse testSubscriptionPermissions(String mySubscription)
throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
SubscriptionName formattedSubscriptionName =
SubscriptionName.create(projectId, subscriptionName);
// [START testSubscriptionPermissions]
List<String> permissions = new LinkedList<>();
permissions.add("pubsub.subscriptions.get");
SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription);
TestIamPermissionsResponse testedPermissions =
publisherClient.testIamPermissions(formattedSubscriptionName.toString(), permissions);
publisherClient.testIamPermissions(subscriptionName.toString(), permissions);
// [END testSubscriptionPermissions]
return testedPermissions;
}
}

/** Example of getting a subscription. */
public Subscription getSubscription(String subscriptionName) throws Exception {
SubscriptionName formattedSubscriptionName =
SubscriptionName.create(projectId, subscriptionName);
public Subscription getSubscription(String mySubscription) throws Exception {
try (SubscriberClient subscriberClient = SubscriberClient.create()) {
// [START getSubscription]
Subscription subscription = subscriberClient.getSubscription(formattedSubscriptionName);
SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription);
Subscription subscription = subscriberClient.getSubscription(subscriptionName);
// [END getSubscription]
return subscription;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ protected static void deleteTestTopicsAndSubscriptions(

private static void deleteTestTopics(String projectId, String[] testTopics) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
for (String testTopic : testTopics) {
for (String name : testTopics) {
try {
publisherClient.deleteTopic(TopicName.create(projectId, testTopic));
publisherClient.deleteTopic(TopicName.create(projectId, name));
System.out.println("Topic deleted : " + name);
} catch (Exception e) {
//do nothing catch clause
}
Expand All @@ -48,6 +49,7 @@ private static void deleteTestSubscriptions(String projectId, String[] subscript
try {
subscriberClient.deleteSubscription(
SubscriptionName.create(projectId, name));
System.out.println("Subscription deleted : " + name);
} catch (Exception e) {
//do nothing catch clause
}
Expand Down
Loading

0 comments on commit e053154

Please sign in to comment.