|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. |
| 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 | +package com.example.pubsub; |
| 17 | + |
| 18 | +// [START pubsub_quickstart_subscriber] |
| 19 | +import com.google.cloud.ServiceOptions; |
| 20 | +import com.google.cloud.pubsub.spi.v1.AckReplyConsumer; |
| 21 | +import com.google.cloud.pubsub.spi.v1.MessageReceiver; |
| 22 | +import com.google.cloud.pubsub.spi.v1.Subscriber; |
| 23 | +import com.google.common.collect.ImmutableList; |
| 24 | +import com.google.pubsub.v1.PubsubMessage; |
| 25 | +import com.google.pubsub.v1.SubscriptionName; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.concurrent.BlockingQueue; |
| 30 | +import java.util.concurrent.LinkedBlockingDeque; |
| 31 | + |
| 32 | +public class SubscriberExample implements Runnable { |
| 33 | + // use the default project id |
| 34 | + private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); |
| 35 | + |
| 36 | + private final BlockingQueue<PubsubMessage> messages = new LinkedBlockingDeque<>(); |
| 37 | + |
| 38 | + private final List<String> receivedMessageIds = new ArrayList<>(); |
| 39 | + |
| 40 | + private final String subscriptionId; |
| 41 | + |
| 42 | + private volatile boolean listen = true; |
| 43 | + |
| 44 | + public SubscriberExample(String subscriptionId) { |
| 45 | + this.subscriptionId = subscriptionId; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void run() { |
| 50 | + MessageReceiver receiver = |
| 51 | + new MessageReceiver() { |
| 52 | + @Override |
| 53 | + public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) { |
| 54 | + messages.offer(message); |
| 55 | + consumer.ack(); |
| 56 | + } |
| 57 | + }; |
| 58 | + SubscriptionName subscriptionName = SubscriptionName.create(PROJECT_ID, subscriptionId); |
| 59 | + Subscriber subscriber = null; |
| 60 | + try { |
| 61 | + // create a subscriber bound to the asynchronous message receiver |
| 62 | + subscriber = Subscriber.defaultBuilder(subscriptionName, receiver).build(); |
| 63 | + subscriber.startAsync().awaitRunning(); |
| 64 | + // continue to wait on received messages, Ctrl-C to exit |
| 65 | + while (listen) { |
| 66 | + // block on receiving a message |
| 67 | + PubsubMessage message = messages.take(); |
| 68 | + System.out.println("Message Id: " + message.getMessageId()); |
| 69 | + System.out.println("Data: " + message.getData().toStringUtf8()); |
| 70 | + receivedMessageIds.add(message.getMessageId()); |
| 71 | + } |
| 72 | + } catch (InterruptedException e) { |
| 73 | + throw new RuntimeException(e); |
| 74 | + } finally { |
| 75 | + if (subscriber != null) { |
| 76 | + subscriber.stopAsync(); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + void stopSubscriber() { |
| 82 | + listen = false; |
| 83 | + } |
| 84 | + |
| 85 | + List<String> getReceivedMessages() { |
| 86 | + return ImmutableList.copyOf(receivedMessageIds); |
| 87 | + } |
| 88 | + |
| 89 | + public static void main(String... args) throws Exception { |
| 90 | + // set subscriber id, eg. my-subscriber-id |
| 91 | + String subscriberId = args[0]; |
| 92 | + SubscriberExample subscriber = new SubscriberExample(subscriberId); |
| 93 | + Thread t = new Thread(subscriber); |
| 94 | + t.start(); |
| 95 | + // Stop subscriber after 5 minutes of listening |
| 96 | + Thread.sleep(5 * 60000); |
| 97 | + subscriber.stopSubscriber(); |
| 98 | + t.join(); |
| 99 | + } |
| 100 | +} |
| 101 | +// [END pubsub_quickstart_subscriber] |
0 commit comments