|
| 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 | +/* |
| 18 | + * EDITING INSTRUCTIONS |
| 19 | + * This file is referenced in Subscriber's javadoc. Any change to this file should be reflected in |
| 20 | + * PubSub's javadoc. |
| 21 | + */ |
| 22 | + |
| 23 | +package com.google.cloud.examples.pubsub.snippets; |
| 24 | + |
| 25 | +import com.google.cloud.pubsub.spi.v1.AckReply; |
| 26 | +import com.google.cloud.pubsub.spi.v1.AckReplyConsumer; |
| 27 | +import com.google.cloud.pubsub.spi.v1.MessageReceiver; |
| 28 | +import com.google.cloud.pubsub.spi.v1.Subscriber; |
| 29 | +import com.google.pubsub.v1.PubsubMessage; |
| 30 | +import com.google.pubsub.v1.SubscriptionName; |
| 31 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 32 | +import java.util.concurrent.atomic.AtomicInteger; |
| 33 | +import java.util.concurrent.Executor; |
| 34 | +import java.util.concurrent.locks.Condition; |
| 35 | +import java.util.concurrent.locks.Lock; |
| 36 | +import java.util.concurrent.locks.ReentrantLock; |
| 37 | + |
| 38 | +public class SubscriberSnippets { |
| 39 | + /** |
| 40 | + * Example of receiving a specific number of messages. |
| 41 | + */ |
| 42 | + // [TARGET startAsync()] |
| 43 | + // [VARIABLE "my_project_name"] |
| 44 | + // [VARIABLE "my_subscription_name"] |
| 45 | + // [VARIABLE 3] |
| 46 | + public void startAsync(String projectName, String subscriptionName, int receiveNum) throws Exception { |
| 47 | + // [START startAsync] |
| 48 | + SubscriptionName subscription = SubscriptionName.create(projectName, subscriptionName); |
| 49 | + final Lock lock = new ReentrantLock(); |
| 50 | + final Condition doneCondition = lock.newCondition(); |
| 51 | + final AtomicInteger pendingReceives = new AtomicInteger(receiveNum); |
| 52 | + final AtomicBoolean done = new AtomicBoolean(); |
| 53 | + |
| 54 | + MessageReceiver receiver = new MessageReceiver() { |
| 55 | + public void receiveMessage(final PubsubMessage message, final AckReplyConsumer consumer) { |
| 56 | + System.out.println("got message: " + message); |
| 57 | + consumer.accept(AckReply.ACK, null); |
| 58 | + if (pendingReceives.decrementAndGet() != 0) { |
| 59 | + return; |
| 60 | + } |
| 61 | + lock.lock(); |
| 62 | + try { |
| 63 | + done.set(true); |
| 64 | + doneCondition.signal(); |
| 65 | + } finally { |
| 66 | + lock.unlock(); |
| 67 | + } |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + Subscriber subscriber = Subscriber.newBuilder(subscription, receiver).build(); |
| 72 | + subscriber.addListener(new Subscriber.SubscriberListener() { |
| 73 | + public void failed(Subscriber.State from, Throwable failure) { |
| 74 | + System.err.println(failure); |
| 75 | + lock.lock(); |
| 76 | + try { |
| 77 | + done.set(true); |
| 78 | + doneCondition.signal(); |
| 79 | + } finally { |
| 80 | + lock.unlock(); |
| 81 | + } |
| 82 | + } |
| 83 | + }, new Executor() { |
| 84 | + public void execute(Runnable command) { |
| 85 | + command.run(); |
| 86 | + } |
| 87 | + }); |
| 88 | + subscriber.startAsync(); |
| 89 | + lock.lock(); |
| 90 | + try { |
| 91 | + while (!done.get()) { |
| 92 | + doneCondition.await(); |
| 93 | + } |
| 94 | + } finally { |
| 95 | + lock.unlock(); |
| 96 | + } |
| 97 | + subscriber.stopAsync().awaitTerminated(); |
| 98 | + // [END startAsync] |
| 99 | + } |
| 100 | +} |
0 commit comments