Skip to content

Commit 00df00b

Browse files
committed
get tests to compile and pass
1 parent 37c160c commit 00df00b

52 files changed

Lines changed: 11358 additions & 67 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 898 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 1050 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 316 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,316 @@
1+
/*
2+
* Copyright 2016 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+
/*
18+
* EDITING INSTRUCTIONS
19+
* This file is referenced in Subscription's javadoc. Any change to this file should be reflected in
20+
* Subscription's javadoc.
21+
*/
22+
23+
package com.google.cloud.examples.pubsub.snippets;
24+
25+
import com.google.cloud.Identity;
26+
import com.google.cloud.Policy;
27+
import com.google.cloud.Role;
28+
import com.google.cloud.pubsub.Message;
29+
import com.google.cloud.pubsub.PubSub.MessageConsumer;
30+
import com.google.cloud.pubsub.PubSub.MessageProcessor;
31+
import com.google.cloud.pubsub.PushConfig;
32+
import com.google.cloud.pubsub.ReceivedMessage;
33+
import com.google.cloud.pubsub.Subscription;
34+
35+
import java.util.Iterator;
36+
import java.util.LinkedList;
37+
import java.util.List;
38+
import java.util.concurrent.ExecutionException;
39+
import java.util.concurrent.Future;
40+
41+
/**
42+
* This class contains a number of snippets for the {@link Subscription} class.
43+
*/
44+
public class SubscriptionSnippets {
45+
46+
private final Subscription subscription;
47+
48+
public SubscriptionSnippets(Subscription subscription) {
49+
this.subscription = subscription;
50+
}
51+
52+
/**
53+
* Example of getting the subscription's latest information.
54+
*/
55+
// [TARGET reload()]
56+
public Subscription reload() {
57+
// [START reload]
58+
Subscription latestSubscription = subscription.reload();
59+
if (latestSubscription == null) {
60+
// the subscription was not found
61+
}
62+
// [END reload]
63+
return latestSubscription;
64+
}
65+
66+
/**
67+
* Example of asynchronously getting the subscription's latest information.
68+
*/
69+
// [TARGET reloadAsync()]
70+
public Subscription reloadAsync() throws ExecutionException, InterruptedException {
71+
// [START reloadAsync]
72+
Future<Subscription> future = subscription.reloadAsync();
73+
// ...
74+
Subscription latestSubscription = future.get();
75+
if (latestSubscription == null) {
76+
// the subscription was not found
77+
}
78+
// [END reloadAsync]
79+
return latestSubscription;
80+
}
81+
82+
/**
83+
* Example of deleting the subscription.
84+
*/
85+
// [TARGET delete()]
86+
public boolean delete() {
87+
// [START delete]
88+
boolean deleted = subscription.delete();
89+
if (deleted) {
90+
// the subscription was deleted
91+
} else {
92+
// the subscription was not found
93+
}
94+
// [END delete]
95+
return deleted;
96+
}
97+
98+
/**
99+
* Example of asynchronously deleting the subscription.
100+
*/
101+
// [TARGET deleteAsync()]
102+
public boolean deleteAsync() throws ExecutionException, InterruptedException {
103+
// [START deleteAsync]
104+
Future<Boolean> future = subscription.deleteAsync();
105+
// ...
106+
boolean deleted = future.get();
107+
if (deleted) {
108+
// the subscription was deleted
109+
} else {
110+
// the subscription was not found
111+
}
112+
// [END deleteAsync]
113+
return deleted;
114+
}
115+
116+
/**
117+
* Example of replacing the push configuration of the subscription, setting the push endpoint.
118+
*/
119+
// [TARGET replacePushConfig(PushConfig)]
120+
// [VARIABLE "https://www.example.com/push"]
121+
public void replacePushConfig(String endpoint) {
122+
// [START replacePushConfig]
123+
PushConfig pushConfig = PushConfig.of(endpoint);
124+
subscription.replacePushConfig(pushConfig);
125+
// [END replacePushConfig]
126+
}
127+
128+
/**
129+
* Example of replacing the push configuration of the subscription, making it a pull
130+
* subscription.
131+
*/
132+
// [TARGET replacePushConfig(PushConfig)]
133+
public void replacePushConfigToPull() {
134+
// [START replacePushConfigToPull]
135+
subscription.replacePushConfig(null);
136+
// [END replacePushConfigToPull]
137+
}
138+
139+
/**
140+
* Example of asynchronously replacing the push configuration of the subscription, setting the
141+
* push endpoint.
142+
*/
143+
// [TARGET replacePushConfigAsync(PushConfig)]
144+
// [VARIABLE "https://www.example.com/push"]
145+
public void replacePushConfigAsync(String endpoint)
146+
throws ExecutionException, InterruptedException {
147+
// [START replacePushConfigAsync]
148+
PushConfig pushConfig = PushConfig.of(endpoint);
149+
Future<Void> future = subscription.replacePushConfigAsync(pushConfig);
150+
// ...
151+
future.get();
152+
// [END replacePushConfigAsync]
153+
}
154+
155+
/**
156+
* Example of asynchronously replacing the push configuration of the subscription, making it a
157+
* pull subscription.
158+
*/
159+
// [TARGET replacePushConfigAsync(PushConfig)]
160+
public void replacePushConfigToPullAsync()
161+
throws ExecutionException, InterruptedException {
162+
// [START replacePushConfigToPullAsync]
163+
Future<Void> future = subscription.replacePushConfigAsync(null);
164+
// ...
165+
future.get();
166+
// [END replacePushConfigToPullAsync]
167+
}
168+
169+
/**
170+
* Example of pulling a maximum number of messages from the subscription.
171+
*/
172+
// [TARGET pull(int)]
173+
public void pull() {
174+
// [START pull]
175+
Iterator<ReceivedMessage> messages = subscription.pull(100);
176+
// Ack deadline is renewed until the message is consumed
177+
while (messages.hasNext()) {
178+
ReceivedMessage message = messages.next();
179+
// do something with message and ack/nack it
180+
message.ack(); // or message.nack()
181+
}
182+
// [END pull]
183+
}
184+
185+
/**
186+
* Example of asynchronously pulling a maximum number of messages from the subscription.
187+
*/
188+
// [TARGET pullAsync(int)]
189+
public void pullAsync() throws ExecutionException, InterruptedException {
190+
// [START pullAsync]
191+
Future<Iterator<ReceivedMessage>> future = subscription.pullAsync(100);
192+
// ...
193+
Iterator<ReceivedMessage> messages = future.get();
194+
// Ack deadline is renewed until the message is consumed
195+
while (messages.hasNext()) {
196+
ReceivedMessage message = messages.next();
197+
// do something with message and ack/nack it
198+
message.ack(); // or message.nack()
199+
}
200+
// [END pullAsync]
201+
}
202+
203+
/**
204+
* Example of continuously pulling messages from the subscription.
205+
*/
206+
// [TARGET pullAsync(MessageProcessor, PullOption...)]
207+
// [VARIABLE "my_subscription_name"]
208+
public void pullWithMessageConsumer(String subscriptionName) throws Exception {
209+
// [START pullWithMessageConsumer]
210+
MessageProcessor callback = new MessageProcessor() {
211+
public void process(Message message) throws Exception {
212+
// Ack deadline is renewed until this method returns
213+
// Message is acked if this method returns successfully
214+
// Message is nacked if this method throws an exception
215+
}
216+
};
217+
MessageConsumer consumer = subscription.pullAsync(callback);
218+
// ...
219+
// Stop pulling
220+
consumer.close();
221+
// [END pullWithMessageConsumer]
222+
}
223+
224+
/**
225+
* Example of getting the subscription's policy.
226+
*/
227+
// [TARGET getPolicy()]
228+
public Policy getPolicy() {
229+
// [START getPolicy]
230+
Policy policy = subscription.getPolicy();
231+
if (policy == null) {
232+
// subscription was not found
233+
}
234+
// [END getPolicy]
235+
return policy;
236+
}
237+
238+
/**
239+
* Example of asynchronously getting the subscription's policy.
240+
*/
241+
// [TARGET getPolicyAsync()]
242+
public Policy getPolicyAsync() throws ExecutionException, InterruptedException {
243+
// [START getPolicyAsync]
244+
Future<Policy> future = subscription.getPolicyAsync();
245+
// ...
246+
Policy policy = future.get();
247+
if (policy == null) {
248+
// subscription was not found
249+
}
250+
// [END getPolicyAsync]
251+
return policy;
252+
}
253+
254+
/**
255+
* Example of replacing the subscription's policy.
256+
*/
257+
// [TARGET replacePolicy(Policy)]
258+
public Policy replacePolicy() {
259+
// [START replacePolicy]
260+
Policy policy = subscription.getPolicy();
261+
Policy updatedPolicy = policy.toBuilder()
262+
.addIdentity(Role.viewer(), Identity.allAuthenticatedUsers())
263+
.build();
264+
updatedPolicy = subscription.replacePolicy(updatedPolicy);
265+
// [END replacePolicy]
266+
return updatedPolicy;
267+
}
268+
269+
/**
270+
* Example of asynchronously replacing the subscription's policy.
271+
*/
272+
// [TARGET replacePolicyAsync(Policy)]
273+
public Policy replacePolicyAsync()
274+
throws ExecutionException, InterruptedException {
275+
// [START replacePolicyAsync]
276+
Policy policy = subscription.getPolicy();
277+
Policy updatedPolicy = policy.toBuilder()
278+
.addIdentity(Role.viewer(), Identity.allAuthenticatedUsers())
279+
.build();
280+
Future<Policy> future = subscription.replacePolicyAsync(updatedPolicy);
281+
// ...
282+
updatedPolicy = future.get();
283+
// [END replacePolicyAsync]
284+
return updatedPolicy;
285+
}
286+
287+
/**
288+
* Example of testing whether the caller has the provided permissions on the subscription.
289+
*/
290+
// [TARGET testPermissions(List)]
291+
public List<Boolean> testPermissions() {
292+
// [START testPermissions]
293+
List<String> permissions = new LinkedList<>();
294+
permissions.add("pubsub.subscriptions.get");
295+
List<Boolean> testedPermissions = subscription.testPermissions(permissions);
296+
// [END testPermissions]
297+
return testedPermissions;
298+
}
299+
300+
/**
301+
* Example of asynchronously testing whether the caller has the provided permissions on the
302+
* subscription.
303+
*/
304+
// [TARGET testPermissionsAsync(List)]
305+
public List<Boolean> testPermissionsAsync()
306+
throws ExecutionException, InterruptedException {
307+
// [START testPermissionsAsync]
308+
List<String> permissions = new LinkedList<>();
309+
permissions.add("pubsub.subscriptions.get");
310+
Future<List<Boolean>> future = subscription.testPermissionsAsync(permissions);
311+
// ...
312+
List<Boolean> testedPermissions = future.get();
313+
// [END testPermissionsAsync]
314+
return testedPermissions;
315+
}
316+
}

0 commit comments

Comments
 (0)