Skip to content

Commit 3e5ae85

Browse files
authored
new code snippet for push subscription + cleanup of deprecated snippets (#1875)
* cleaning up PubSubExample adding snippet for creating a subscription with a push endpoint * updating start, end tags for snippets, adding async pull snippet tag
1 parent c2fb49e commit 3e5ae85

File tree

5 files changed

+80
-67
lines changed

5 files changed

+80
-67
lines changed

google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/PubSubExample.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@
4848
* pull sync <subscription> <maxMessages>
4949
* publish <topic> <message>+
5050
* replace-push-config <subscription> <endpoint>?
51-
* ack <subscription> <ackId>+
52-
* nack <subscription> <ackId>+
5351
* create topic <topic>
5452
* create subscription <topic> <subscription> <endpoint>?
5553
* list subscriptions <topic>?
@@ -488,22 +486,6 @@ public String params() {
488486
}
489487
}
490488

491-
private abstract static class MessagesAction extends PubSubAction<Tuple<SubscriptionName, List<String>>> {
492-
@Override
493-
Tuple<SubscriptionName, List<String>> parse(String... args) throws Exception {
494-
if (args.length < 2) {
495-
throw new IllegalArgumentException("Missing required subscription and ack IDs");
496-
}
497-
SubscriptionName subscriptionName = SubscriptionName.create(projectId, args[0]);
498-
return Tuple.of(subscriptionName, Arrays.asList(Arrays.copyOfRange(args, 1, args.length)));
499-
}
500-
501-
@Override
502-
public String params() {
503-
return "<subscription> <ackId>+";
504-
}
505-
}
506-
507489
/**
508490
* This class demonstrates how to asynchronously pull messages from a Pub/Sub pull subscription.
509491
* Messages are pulled until a timeout is reached.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
public class CreateSubscriptionAndPullMessages {
3535

3636
public static void main(String... args) throws Exception {
37+
// [START async_pull_subscription]
3738
TopicName topic = TopicName.create("test-project", "test-topic");
3839
SubscriptionName subscription = SubscriptionName.create("test-project", "test-subscription");
3940

@@ -62,11 +63,13 @@ public void failed(Subscriber.State from, Throwable failure) {
6263
},
6364
MoreExecutors.directExecutor());
6465
subscriber.startAsync().awaitRunning();
66+
6567
Thread.sleep(60000);
6668
} finally {
6769
if (subscriber != null) {
6870
subscriber.stopAsync();
6971
}
7072
}
73+
// [END async_pull_subscription]
7174
}
7275
}

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

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,35 +53,61 @@ public String getProjectId() {
5353
}
5454

5555
/** Example of creating a pull subscription for a topic. */
56-
public Subscription createSubscription(String topic, String subscriptionId) throws Exception {
56+
public Subscription createSubscription(String topicId, String subscriptionId) throws Exception {
57+
// [START createSubscription]
5758
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
58-
// [START createSubscription]
59-
TopicName topicName = TopicName.create(projectId, topic);
59+
// eg. projectId = "my-test-project", topicId = "my-test-topic"
60+
TopicName topicName = TopicName.create(projectId, topicId);
61+
// eg. subscriptionId = "my-test-subscription"
6062
SubscriptionName subscriptionName =
6163
SubscriptionName.create(projectId, subscriptionId);
64+
// create a pull subscription with default acknowledgement deadline
6265
Subscription subscription =
6366
subscriptionAdminClient.createSubscription(
6467
subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
65-
// [END createSubscription]
6668
return subscription;
6769
}
70+
// [END createSubscription]
71+
}
72+
73+
/** Example of creating a subscription with a push endpoint. */
74+
public Subscription createSubscriptionWithPushEndpoint(String topicId, String subscriptionId, String endpoint)
75+
throws Exception {
76+
// [START createSubscriptionWithPushEndpoint]
77+
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
78+
TopicName topicName = TopicName.create(projectId, topicId);
79+
SubscriptionName subscriptionName =
80+
SubscriptionName.create(projectId, subscriptionId);
81+
82+
// eg. endpoint = "https://my-test-project.appspot.com/push"
83+
PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build();
84+
85+
// acknowledgement deadline in seconds for the message received over the push endpoint
86+
int ackDeadlineInSeconds = 10;
87+
88+
Subscription subscription =
89+
subscriptionAdminClient.createSubscription(
90+
subscriptionName, topicName, pushConfig, ackDeadlineInSeconds);
91+
return subscription;
92+
}
93+
// [END createSubscriptionWithPushEndpoint]
6894
}
6995

7096
/** Example of replacing the push configuration of a subscription, setting the push endpoint. */
7197
public void replacePushConfig(String subscriptionId, String endpoint) throws Exception {
98+
// [START replacePushConfig]
7299
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
73-
// [START replacePushConfig]
74100
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
75101
PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build();
76102
subscriptionAdminClient.modifyPushConfig(subscriptionName, pushConfig);
77-
// [END replacePushConfig]
78103
}
104+
// [END replacePushConfig]
79105
}
80106

81107
/** Example of listing subscriptions. */
82108
public ListSubscriptionsPagedResponse listSubscriptions() throws Exception {
109+
// [START listSubscriptions]
83110
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
84-
// [START listSubscriptions]
85111
ListSubscriptionsRequest listSubscriptionsRequest =
86112
ListSubscriptionsRequest.newBuilder()
87113
.setProjectWithProjectName(ProjectName.create(projectId))
@@ -92,40 +118,40 @@ public ListSubscriptionsPagedResponse listSubscriptions() throws Exception {
92118
for (Subscription subscription : subscriptions) {
93119
// do something with the subscription
94120
}
95-
// [END listSubscriptions]
96121
return response;
97122
}
123+
// [END listSubscriptions]
98124
}
99125

100126
/** Example of deleting a subscription. */
101127
public SubscriptionName deleteSubscription(String subscriptionId) throws Exception {
128+
// [START deleteSubscription]
102129
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
103-
// [START deleteSubscription]
104130
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
105131
subscriptionAdminClient.deleteSubscription(subscriptionName);
106-
// [END deleteSubscription]
107132
return subscriptionName;
108133
}
134+
// [END deleteSubscription]
109135
}
110136

111137
/** Example of getting a subscription policy. */
112138
public Policy getSubscriptionPolicy(String subscriptionId) throws Exception {
139+
// [START getSubscriptionPolicy]
113140
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
114-
// [START getSubscriptionPolicy]
115141
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
116142
Policy policy = subscriptionAdminClient.getIamPolicy(subscriptionName.toString());
117143
if (policy == null) {
118144
// subscription was not found
119145
}
120-
// [END getSubscriptionPolicy]
121146
return policy;
122147
}
148+
// [END getSubscriptionPolicy]
123149
}
124150

125151
/** Example of replacing a subscription policy. */
126152
public Policy replaceSubscriptionPolicy(String subscriptionId) throws Exception {
153+
// [START replaceSubscriptionPolicy]
127154
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
128-
// [START replaceSubscriptionPolicy]
129155
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
130156
Policy policy = subscriptionAdminClient.getIamPolicy(subscriptionName.toString());
131157
// Create a role => members binding
@@ -138,34 +164,34 @@ public Policy replaceSubscriptionPolicy(String subscriptionId) throws Exception
138164
Policy updatedPolicy = policy.toBuilder().addBindings(binding).build();
139165

140166
updatedPolicy = subscriptionAdminClient.setIamPolicy(subscriptionName.toString(), updatedPolicy);
141-
// [END replaceSubscriptionPolicy]
142167
return updatedPolicy;
143168
}
169+
// [END replaceSubscriptionPolicy]
144170
}
145171

146172
/** Example of testing whether the caller has the provided permissions on a subscription. */
147173
public TestIamPermissionsResponse testSubscriptionPermissions(String subscriptionId)
148174
throws Exception {
175+
// [START testSubscriptionPermissions]
149176
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
150-
// [START testSubscriptionPermissions]
151177
List<String> permissions = new LinkedList<>();
152178
permissions.add("pubsub.subscriptions.get");
153179
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
154180
TestIamPermissionsResponse testedPermissions =
155181
topicAdminClient.testIamPermissions(subscriptionName.toString(), permissions);
156-
// [END testSubscriptionPermissions]
157182
return testedPermissions;
158183
}
184+
// [END testSubscriptionPermissions]
159185
}
160186

161187
/** Example of getting a subscription. */
162188
public Subscription getSubscription(String subscriptionId) throws Exception {
189+
// [START getSubscription]
163190
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
164-
// [START getSubscription]
165191
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
166192
Subscription subscription = subscriptionAdminClient.getSubscription(subscriptionName);
167-
// [END getSubscription]
168193
return subscription;
169194
}
195+
// [END getSubscription]
170196
}
171197
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,19 @@ public String getProjectId() {
4848

4949
/** Example of creating a topic. */
5050
public Topic createTopic(String topicId) throws Exception {
51+
// [START createTopic]
5152
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
52-
// [START createTopic]
5353
TopicName topicName = TopicName.create(projectId, topicId);
5454
Topic topic = topicAdminClient.createTopic(topicName);
55-
// [END createTopic]
5655
return topic;
5756
}
57+
// [END createTopic]
5858
}
5959

6060
/** Example of listing topics. */
6161
public ListTopicsPagedResponse listTopics() throws Exception {
62+
// [START listTopics]
6263
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
63-
// [START listTopics]
6464
ListTopicsRequest listTopicsRequest =
6565
ListTopicsRequest.newBuilder()
6666
.setProjectWithProjectName(ProjectName.create(projectId))
@@ -70,16 +70,16 @@ public ListTopicsPagedResponse listTopics() throws Exception {
7070
for (Topic topic : topics) {
7171
// do something with the topic
7272
}
73-
// [END listTopics]
7473
return response;
7574
}
75+
// [END listTopics]
7676
}
7777

7878
/** Example of listing topics for a subscription. */
7979
public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicId)
8080
throws Exception {
81+
// [START listTopicSubscriptions]
8182
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
82-
// [START listTopicSubscriptions]
8383
TopicName topicName = TopicName.create(projectId, topicId);
8484
ListTopicSubscriptionsRequest request =
8585
ListTopicSubscriptionsRequest.newBuilder()
@@ -91,40 +91,40 @@ public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicId
9191
for (String subscriptionName : subscriptionNames) {
9292
// do something with the subscription name
9393
}
94-
// [END listTopicSubscriptions]
9594
return response;
9695
}
96+
// [END listTopicSubscriptions]
9797
}
9898

9999
/** Example of deleting a topic. */
100100
public TopicName deleteTopic(String topicId) throws Exception {
101+
// [START deleteTopic]
101102
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
102-
// [START deleteTopic]
103103
TopicName topicName = TopicName.create(projectId, topicId);
104104
topicAdminClient.deleteTopic(topicName);
105-
// [END deleteTopic]
106105
return topicName;
107106
}
107+
// [END deleteTopic]
108108
}
109109

110110
/** Example of getting a topic policy. */
111111
public Policy getTopicPolicy(String topicId) throws Exception {
112+
// [START getTopicPolicy]
112113
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
113-
// [START getTopicPolicy]
114114
TopicName topicName = TopicName.create(projectId, topicId);
115115
Policy policy = topicAdminClient.getIamPolicy(topicName.toString());
116116
if (policy == null) {
117117
// topic iam policy was not found
118118
}
119-
// [END getTopicPolicy]
120119
return policy;
121120
}
121+
// [END getTopicPolicy]
122122
}
123123

124124
/** Example of replacing a topic policy. */
125125
public Policy replaceTopicPolicy(String topicId) throws Exception {
126+
// [START replaceTopicPolicy]
126127
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
127-
// [START replaceTopicPolicy]
128128
String topicName = TopicName.create(projectId, topicId).toString();
129129
Policy policy = topicAdminClient.getIamPolicy(topicName);
130130
// add role -> members binding
@@ -136,34 +136,34 @@ public Policy replaceTopicPolicy(String topicId) throws Exception {
136136
// create updated policy
137137
Policy updatedPolicy = Policy.newBuilder(policy).addBindings(binding).build();
138138
updatedPolicy = topicAdminClient.setIamPolicy(topicName, updatedPolicy);
139-
// [END replaceTopicPolicy]
140139
return updatedPolicy;
141140
}
141+
// [END replaceTopicPolicy]
142142
}
143143

144144
/** Example of testing whether the caller has the provided permissions on a topic.
145145
* Only viewer, editor or admin/owner can view results of pubsub.topics.get */
146146
public TestIamPermissionsResponse testTopicPermissions(String topicId) throws Exception {
147+
// [START testTopicPermissions]
147148
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
148-
// [START testTopicPermissions]
149149
List<String> permissions = new LinkedList<>();
150150
permissions.add("pubsub.topics.get");
151151
TopicName topicName = TopicName.create(projectId, topicId);
152152
TestIamPermissionsResponse testedPermissions =
153153
topicAdminClient.testIamPermissions(topicName.toString(), permissions);
154-
// [END testTopicPermissions]
155154
return testedPermissions;
156155
}
156+
// [END testTopicPermissions]
157157
}
158158

159159
/** Example of getting a topic. */
160160
public Topic getTopic(String topicId) throws Exception {
161+
// [START getTopic]
161162
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
162-
// [START getTopic]
163163
TopicName topicName = TopicName.create(projectId, topicId);
164164
Topic topic = topicAdminClient.getTopic(topicName);
165-
// [END createTopic]
166165
return topic;
167166
}
167+
// [END getTopic]
168168
}
169169
}

google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriptionAdminClientSnippets.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,19 @@ public void setUp() throws Exception {
8282
Cleanup.deleteTestTopicsAndSubscriptions(projectId, topics, subscriptions);
8383
}
8484

85-
private Subscription createSubscription(String topicName, String subscriptionName)
86-
throws Exception {
85+
@Test
86+
public void createSubscriptionWithPushIsSuccessful() throws Exception {
87+
String topicName = topics[0];
88+
String subscriptionName = subscriptions[0];
8789
createTopic(topicName);
90+
String endpoint = "https://" + projectId + ".appspot.com/push";
8891
Subscription subscription =
89-
subscriptionAdminClientSnippets.createSubscription(topicName, subscriptionName);
92+
subscriptionAdminClientSnippets.createSubscriptionWithPushEndpoint(topicName, subscriptionName, endpoint);
9093
assertNotNull(subscription);
9194
Subscription retrievedSubscription = subscriptionAdminClientSnippets.getSubscription(subscriptionName);
9295
assertNotNull(retrievedSubscription);
9396
assertEquals(subscription.getName(), retrievedSubscription.getName());
94-
return subscription;
97+
assertEquals(subscription.getPushConfig().getPushEndpoint(), endpoint);
9598
}
9699

97100
@Test
@@ -168,17 +171,16 @@ private void createTopic(String name) throws Exception {
168171
}
169172
}
170173

171-
private Set<String> publishMessages(String topicName, int numMessages) throws Exception {
172-
Set<String> messages = new HashSet<>();
173-
Publisher publisher = Publisher.defaultBuilder(TopicName.create(projectId, topicName)).build();
174-
for (int i = 1; i<= numMessages; i++) {
175-
String message = formatForTest("message-" + i);
176-
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(
177-
ByteString.copyFromUtf8(message)).build();
178-
publisher.publish(pubsubMessage);
179-
messages.add(message);
180-
}
181-
return messages;
174+
private Subscription createSubscription(String topicName, String subscriptionName)
175+
throws Exception {
176+
createTopic(topicName);
177+
Subscription subscription =
178+
subscriptionAdminClientSnippets.createSubscription(topicName, subscriptionName);
179+
assertNotNull(subscription);
180+
Subscription retrievedSubscription = subscriptionAdminClientSnippets.getSubscription(subscriptionName);
181+
assertNotNull(retrievedSubscription);
182+
assertEquals(subscription.getName(), retrievedSubscription.getName());
183+
return subscription;
182184
}
183185

184186
@After

0 commit comments

Comments
 (0)