|
1 | 1 | /* |
2 | | - * Copyright 2016 Google Inc. All Rights Reserved. |
| 2 | + * Copyright 2017 Google Inc. All Rights Reserved. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 |
|
17 | 17 | package com.google.cloud.examples.pubsub.snippets; |
18 | 18 |
|
19 | | -import com.google.cloud.pubsub.Message; |
20 | | -import com.google.cloud.pubsub.PubSub; |
21 | | -import com.google.cloud.pubsub.PubSub.MessageConsumer; |
22 | | -import com.google.cloud.pubsub.PubSub.MessageProcessor; |
23 | | -import com.google.cloud.pubsub.PubSubOptions; |
24 | | -import com.google.cloud.pubsub.Subscription; |
25 | | -import com.google.cloud.pubsub.SubscriptionInfo; |
| 19 | +import com.google.cloud.pubsub.spi.v1.MessageReceiver; |
| 20 | +import com.google.cloud.pubsub.spi.v1.Subscriber; |
| 21 | +import com.google.cloud.pubsub.spi.v1.SubscriberClient; |
| 22 | +import com.google.common.util.concurrent.MoreExecutors; |
| 23 | +import com.google.common.util.concurrent.SettableFuture; |
| 24 | +import com.google.pubsub.v1.PubsubMessage; |
| 25 | +import com.google.pubsub.v1.PushConfig; |
| 26 | +import com.google.pubsub.v1.SubscriptionName; |
| 27 | +import com.google.pubsub.v1.TopicName; |
26 | 28 |
|
27 | 29 | /** |
28 | 30 | * A snippet for Google Cloud Pub/Sub showing how to create a Pub/Sub pull subscription and |
|
31 | 33 | public class CreateSubscriptionAndPullMessages { |
32 | 34 |
|
33 | 35 | public static void main(String... args) throws Exception { |
34 | | - try (PubSub pubsub = PubSubOptions.getDefaultInstance().getService()) { |
35 | | - Subscription subscription = |
36 | | - pubsub.create(SubscriptionInfo.of("test-topic", "test-subscription")); |
37 | | - MessageProcessor callback = new MessageProcessor() { |
38 | | - @Override |
39 | | - public void process(Message message) throws Exception { |
40 | | - System.out.printf("Received message \"%s\"%n", message.getPayloadAsString()); |
41 | | - } |
42 | | - }; |
43 | | - // Create a message consumer and pull messages (for 60 seconds) |
44 | | - try (MessageConsumer consumer = subscription.pullAsync(callback)) { |
45 | | - Thread.sleep(60_000); |
| 36 | + TopicName topic = TopicName.create("test-project", "test-topic"); |
| 37 | + SubscriptionName subscription = SubscriptionName.create("test-project", "test-subscription"); |
| 38 | + |
| 39 | + try (SubscriberClient subscriberClient = SubscriberClient.create()) { |
| 40 | + subscriberClient.createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 0); |
| 41 | + } |
| 42 | + |
| 43 | + MessageReceiver receiver = |
| 44 | + new MessageReceiver() { |
| 45 | + @Override |
| 46 | + public void receiveMessage( |
| 47 | + PubsubMessage message, SettableFuture<MessageReceiver.AckReply> response) { |
| 48 | + System.out.println("got message: " + message.getData().toStringUtf8()); |
| 49 | + response.set(MessageReceiver.AckReply.ACK); |
| 50 | + } |
| 51 | + }; |
| 52 | + Subscriber subscriber = null; |
| 53 | + try { |
| 54 | + subscriber = Subscriber.newBuilder(subscription, receiver).build(); |
| 55 | + subscriber.addListener( |
| 56 | + new Subscriber.SubscriberListener() { |
| 57 | + @Override |
| 58 | + public void failed(Subscriber.State from, Throwable failure) { |
| 59 | + System.err.println(failure); |
| 60 | + } |
| 61 | + }, |
| 62 | + MoreExecutors.directExecutor()); |
| 63 | + subscriber.startAsync().awaitRunning(); |
| 64 | + Thread.sleep(60000); |
| 65 | + } finally { |
| 66 | + if (subscriber != null) { |
| 67 | + subscriber.stopAsync(); |
46 | 68 | } |
47 | 69 | } |
48 | 70 | } |
|
0 commit comments