|
| 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 | + @Override |
| 212 | + public void process(Message message) throws Exception { |
| 213 | + // Ack deadline is renewed until this method returns |
| 214 | + // Message is acked if this method returns successfully |
| 215 | + // Message is nacked if this method throws an exception |
| 216 | + } |
| 217 | + }; |
| 218 | + MessageConsumer consumer = subscription.pullAsync(callback); |
| 219 | + // ... |
| 220 | + // Stop pulling |
| 221 | + consumer.close(); |
| 222 | + // [END pullWithMessageConsumer] |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Example of getting the subscription's policy. |
| 227 | + */ |
| 228 | + // [TARGET getPolicy()] |
| 229 | + public Policy getPolicy() { |
| 230 | + // [START getPolicy] |
| 231 | + Policy policy = subscription.getPolicy(); |
| 232 | + if (policy == null) { |
| 233 | + // subscription was not found |
| 234 | + } |
| 235 | + // [END getPolicy] |
| 236 | + return policy; |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Example of asynchronously getting the subscription's policy. |
| 241 | + */ |
| 242 | + // [TARGET getPolicyAsync()] |
| 243 | + public Policy getPolicyAsync() throws ExecutionException, InterruptedException { |
| 244 | + // [START getPolicyAsync] |
| 245 | + Future<Policy> future = subscription.getPolicyAsync(); |
| 246 | + // ... |
| 247 | + Policy policy = future.get(); |
| 248 | + if (policy == null) { |
| 249 | + // subscription was not found |
| 250 | + } |
| 251 | + // [END getPolicyAsync] |
| 252 | + return policy; |
| 253 | + } |
| 254 | + |
| 255 | + /** |
| 256 | + * Example of replacing the subscription's policy. |
| 257 | + */ |
| 258 | + // [TARGET replacePolicy(Policy)] |
| 259 | + public Policy replacePolicy() { |
| 260 | + // [START replacePolicy] |
| 261 | + Policy policy = subscription.getPolicy(); |
| 262 | + Policy updatedPolicy = policy.toBuilder() |
| 263 | + .addIdentity(Role.viewer(), Identity.allAuthenticatedUsers()) |
| 264 | + .build(); |
| 265 | + updatedPolicy = subscription.replacePolicy(updatedPolicy); |
| 266 | + // [END replacePolicy] |
| 267 | + return updatedPolicy; |
| 268 | + } |
| 269 | + |
| 270 | + /** |
| 271 | + * Example of asynchronously replacing the subscription's policy. |
| 272 | + */ |
| 273 | + // [TARGET replacePolicyAsync(Policy)] |
| 274 | + public Policy replacePolicyAsync() |
| 275 | + throws ExecutionException, InterruptedException { |
| 276 | + // [START replacePolicyAsync] |
| 277 | + Policy policy = subscription.getPolicy(); |
| 278 | + Policy updatedPolicy = policy.toBuilder() |
| 279 | + .addIdentity(Role.viewer(), Identity.allAuthenticatedUsers()) |
| 280 | + .build(); |
| 281 | + Future<Policy> future = subscription.replacePolicyAsync(updatedPolicy); |
| 282 | + // ... |
| 283 | + updatedPolicy = future.get(); |
| 284 | + // [END replacePolicyAsync] |
| 285 | + return updatedPolicy; |
| 286 | + } |
| 287 | + |
| 288 | + /** |
| 289 | + * Example of testing whether the caller has the provided permissions on the subscription. |
| 290 | + */ |
| 291 | + // [TARGET testPermissions(List)] |
| 292 | + public List<Boolean> testPermissions() { |
| 293 | + // [START testPermissions] |
| 294 | + List<String> permissions = new LinkedList<>(); |
| 295 | + permissions.add("pubsub.subscriptions.get"); |
| 296 | + List<Boolean> testedPermissions = subscription.testPermissions(permissions); |
| 297 | + // [END testPermissions] |
| 298 | + return testedPermissions; |
| 299 | + } |
| 300 | + |
| 301 | + /** |
| 302 | + * Example of asynchronously testing whether the caller has the provided permissions on the |
| 303 | + * subscription. |
| 304 | + */ |
| 305 | + // [TARGET testPermissionsAsync(List)] |
| 306 | + public List<Boolean> testPermissionsAsync() |
| 307 | + throws ExecutionException, InterruptedException { |
| 308 | + // [START testPermissionsAsync] |
| 309 | + List<String> permissions = new LinkedList<>(); |
| 310 | + permissions.add("pubsub.subscriptions.get"); |
| 311 | + Future<List<Boolean>> future = subscription.testPermissionsAsync(permissions); |
| 312 | + // ... |
| 313 | + List<Boolean> testedPermissions = future.get(); |
| 314 | + // [END testPermissionsAsync] |
| 315 | + return testedPermissions; |
| 316 | + } |
| 317 | +} |
0 commit comments