Skip to content

Commit d21491b

Browse files
authored
PublisherClient/SubscriberClient snippets (#1663)
adding snippets for PublisherClient, SubscriberClient
1 parent 38bf852 commit d21491b

File tree

8 files changed

+830
-17
lines changed

8 files changed

+830
-17
lines changed

google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/MessageReceiverSnippets.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
/*
1818
* EDITING INSTRUCTIONS
19-
* This file is referenced in Subscriber's javadoc. Any change to this file should be reflected in
20-
* PubSub's javadoc.
19+
* This file is referenced in MessageReceiver's javadoc.
20+
* Any change to this file should be reflected in MessageReceiver's javadoc.
2121
*/
2222

2323
package com.google.cloud.examples.pubsub.snippets;
@@ -28,6 +28,8 @@
2828
import com.google.pubsub.v1.PubsubMessage;
2929
import java.util.concurrent.BlockingQueue;
3030

31+
/** This class contains snippets for the {@link MessageReceiver} interface. */
32+
3133
public class MessageReceiverSnippets {
3234
private final BlockingQueue<PubsubMessage> blockingQueue;
3335

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Copyright 2017 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.examples.pubsub.snippets;
18+
19+
import com.google.cloud.Identity;
20+
import com.google.cloud.Role;
21+
import com.google.cloud.ServiceOptions;
22+
import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse;
23+
import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse;
24+
import com.google.cloud.pubsub.spi.v1.PublisherClient;
25+
import com.google.iam.v1.Binding;
26+
import com.google.iam.v1.Policy;
27+
import com.google.iam.v1.TestIamPermissionsResponse;
28+
import com.google.pubsub.v1.ListTopicSubscriptionsRequest;
29+
import com.google.pubsub.v1.ListTopicsRequest;
30+
import com.google.pubsub.v1.ProjectName;
31+
import com.google.pubsub.v1.Topic;
32+
import com.google.pubsub.v1.TopicName;
33+
import java.util.LinkedList;
34+
import java.util.List;
35+
36+
/** This class contains a number of snippets for the {@link PublisherClient} interface. */
37+
public class PublisherClientSnippets {
38+
39+
private final String projectId;
40+
41+
public PublisherClientSnippets() {
42+
this.projectId = ServiceOptions.getDefaultProjectId();
43+
}
44+
45+
public String getProjectId() {
46+
return projectId;
47+
}
48+
49+
/** Example of creating a topic. */
50+
public Topic createTopic(String topicId) throws Exception {
51+
try (PublisherClient publisherClient = PublisherClient.create()) {
52+
// [START createTopic]
53+
TopicName topicName = TopicName.create(projectId, topicId);
54+
Topic topic = publisherClient.createTopic(topicName);
55+
// [END createTopic]
56+
return topic;
57+
}
58+
}
59+
60+
/** Example of listing topics. */
61+
public ListTopicsPagedResponse listTopics() throws Exception {
62+
try (PublisherClient publisherClient = PublisherClient.create()) {
63+
// [START listTopics]
64+
ListTopicsRequest listTopicsRequest =
65+
ListTopicsRequest.newBuilder()
66+
.setProjectWithProjectName(ProjectName.create(projectId))
67+
.build();
68+
ListTopicsPagedResponse response = publisherClient.listTopics(listTopicsRequest);
69+
Iterable<Topic> topics = response.iterateAllElements();
70+
for (Topic topic : topics) {
71+
// do something with the topic
72+
}
73+
// [END listTopics]
74+
return response;
75+
}
76+
}
77+
78+
/** Example of listing topics for a subscription. */
79+
public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicId)
80+
throws Exception {
81+
try (PublisherClient publisherClient = PublisherClient.create()) {
82+
// [START listTopicSubscriptions]
83+
TopicName topicName = TopicName.create(projectId, topicId);
84+
ListTopicSubscriptionsRequest request =
85+
ListTopicSubscriptionsRequest.newBuilder()
86+
.setTopicWithTopicName(topicName)
87+
.build();
88+
ListTopicSubscriptionsPagedResponse response =
89+
publisherClient.listTopicSubscriptions(request);
90+
Iterable<String> subscriptionNames = response.iterateAllElements();
91+
for (String subscriptionName : subscriptionNames) {
92+
// do something with the subscription name
93+
}
94+
// [END listTopicSubscriptions]
95+
return response;
96+
}
97+
}
98+
99+
/** Example of deleting a topic. */
100+
public TopicName deleteTopic(String topicId) throws Exception {
101+
try (PublisherClient publisherClient = PublisherClient.create()) {
102+
// [START deleteTopic]
103+
TopicName topicName = TopicName.create(projectId, topicId);
104+
publisherClient.deleteTopic(topicName);
105+
// [END deleteTopic]
106+
return topicName;
107+
}
108+
}
109+
110+
/** Example of getting a topic policy. */
111+
public Policy getTopicPolicy(String topicId) throws Exception {
112+
try (PublisherClient publisherClient = PublisherClient.create()) {
113+
// [START getTopicPolicy]
114+
TopicName topicName = TopicName.create(projectId, topicId);
115+
Policy policy = publisherClient.getIamPolicy(topicName.toString());
116+
if (policy == null) {
117+
// topic iam policy was not found
118+
}
119+
// [END getTopicPolicy]
120+
return policy;
121+
}
122+
}
123+
124+
/** Example of replacing a topic policy. */
125+
public Policy replaceTopicPolicy(String topicId) throws Exception {
126+
try (PublisherClient publisherClient = PublisherClient.create()) {
127+
// [START replaceTopicPolicy]
128+
String topicName = TopicName.create(projectId, topicId).toString();
129+
Policy policy = publisherClient.getIamPolicy(topicName);
130+
// add role -> members binding
131+
Binding binding =
132+
Binding.newBuilder()
133+
.setRole(Role.viewer().toString())
134+
.addMembers(Identity.allAuthenticatedUsers().toString())
135+
.build();
136+
// create updated policy
137+
Policy updatedPolicy = Policy.newBuilder(policy).addBindings(binding).build();
138+
updatedPolicy = publisherClient.setIamPolicy(topicName, updatedPolicy);
139+
// [END replaceTopicPolicy]
140+
return updatedPolicy;
141+
}
142+
}
143+
144+
/** Example of testing whether the caller has the provided permissions on a topic.
145+
* Only viewer, editor or admin/owner can view results of pubsub.topics.get */
146+
public TestIamPermissionsResponse testTopicPermissions(String topicId) throws Exception {
147+
try (PublisherClient publisherClient = PublisherClient.create()) {
148+
// [START testTopicPermissions]
149+
List<String> permissions = new LinkedList<>();
150+
permissions.add("pubsub.topics.get");
151+
TopicName topicName = TopicName.create(projectId, topicId);
152+
TestIamPermissionsResponse testedPermissions =
153+
publisherClient.testIamPermissions(topicName.toString(), permissions);
154+
// [END testTopicPermissions]
155+
return testedPermissions;
156+
}
157+
}
158+
159+
/** Example of getting a topic. */
160+
public Topic getTopic(String topicId) throws Exception {
161+
try (PublisherClient publisherClient = PublisherClient.create()) {
162+
// [START getTopic]
163+
TopicName topicName = TopicName.create(projectId, topicId);
164+
Topic topic = publisherClient.getTopic(topicName);
165+
// [END createTopic]
166+
return topic;
167+
}
168+
}
169+
}

google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherSnippets.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17+
/*
18+
* EDITING INSTRUCTIONS
19+
* This file is referenced in Publisher's javadoc. Any change to this file should be reflected in
20+
* Publisher's javadoc.
21+
*/
22+
1723
package com.google.cloud.examples.pubsub.snippets;
1824

1925
import com.google.api.gax.core.RpcFuture;
@@ -23,39 +29,37 @@
2329
import com.google.pubsub.v1.PubsubMessage;
2430
import com.google.pubsub.v1.TopicName;
2531

32+
/** This class contains snippets for the {@link Publisher} interface. */
2633
public class PublisherSnippets {
2734
private final Publisher publisher;
2835

2936
public PublisherSnippets(Publisher publisher) {
3037
this.publisher = publisher;
3138
}
3239

33-
/**
34-
* Example of publishing a message.
35-
*/
40+
/** Example of publishing a message. */
3641
// [TARGET publish(PubsubMessage)]
3742
// [VARIABLE "my_message"]
3843
public RpcFuture<String> publish(String message) {
3944
// [START publish]
4045
ByteString data = ByteString.copyFromUtf8(message);
4146
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
4247
RpcFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
43-
messageIdFuture.addCallback(new RpcFutureCallback<String>() {
44-
public void onSuccess(String messageId) {
45-
System.out.println("published with message id: " + messageId);
46-
}
48+
messageIdFuture.addCallback(
49+
new RpcFutureCallback<String>() {
50+
public void onSuccess(String messageId) {
51+
System.out.println("published with message id: " + messageId);
52+
}
4753

48-
public void onFailure(Throwable t) {
49-
System.out.println("failed to publish: " + t);
50-
}
51-
});
54+
public void onFailure(Throwable t) {
55+
System.out.println("failed to publish: " + t);
56+
}
57+
});
5258
// [END publish]
5359
return messageIdFuture;
5460
}
5561

56-
/**
57-
* Example of creating a {@code Publisher}.
58-
*/
62+
/** Example of creating a {@code Publisher}. */
5963
// [TARGET newBuilder(TopicName)]
6064
// [VARIABLE "my_project"]
6165
// [VARIABLE "my_topic"]

0 commit comments

Comments
 (0)