Skip to content

Commit f3176bc

Browse files
committed
getting started guide files
1 parent 65e9a24 commit f3176bc

File tree

3 files changed

+244
-0
lines changed

3 files changed

+244
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
17+
package com.example.pubsub;
18+
19+
// [START pubsub_quickstart_create_subscription]
20+
import com.google.cloud.ServiceOptions;
21+
import com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient;
22+
import com.google.pubsub.v1.PushConfig;
23+
import com.google.pubsub.v1.Subscription;
24+
import com.google.pubsub.v1.SubscriptionName;
25+
import com.google.pubsub.v1.TopicName;
26+
27+
public class CreatePullSubscriptionExample {
28+
29+
public static void main(String... args) throws Exception {
30+
31+
// Your Google Cloud Platform project ID
32+
String projectId = ServiceOptions.getDefaultProjectId();
33+
34+
// Your topic ID, eg. "my-topic-id"
35+
String topicId = args[0];
36+
37+
// Your subscription ID eg. "my-subscription-id"
38+
String subscriptionId = args[1];
39+
40+
TopicName topicName = TopicName.create(projectId, topicId);
41+
42+
// Create a new subscription
43+
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
44+
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
45+
// create a pull subscription with default acknowledgement deadline (= 10 seconds)
46+
Subscription subscription =
47+
subscriptionAdminClient.createSubscription(
48+
subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
49+
}
50+
51+
System.out.printf(
52+
"Subscription %s:%s created.\n",
53+
subscriptionName.getProject(), subscriptionName.getSubscription());
54+
}
55+
}
56+
// [END pubsub_quickstart_create_subscription]
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
// [START pubsub_quickstart_publisher]
18+
import com.google.api.core.ApiFuture;
19+
import com.google.api.core.ApiFutures;
20+
import com.google.cloud.ServiceOptions;
21+
import com.google.cloud.pubsub.spi.v1.Publisher;
22+
import com.google.protobuf.ByteString;
23+
import com.google.pubsub.v1.PubsubMessage;
24+
import com.google.pubsub.v1.TopicName;
25+
26+
import java.util.ArrayList;
27+
import java.util.List;
28+
29+
public class PublisherExample {
30+
31+
// use the default project id
32+
private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId();
33+
34+
private static final int MESSAGE_COUNT = 5;
35+
36+
//publish messages asynchronously one at a time.
37+
static List<String> publishMessages(String topicId) throws Exception {
38+
List<String> messageIds;
39+
List<ApiFuture<String>> messageIdFutures = new ArrayList<>();
40+
TopicName topicName = TopicName.create(PROJECT_ID, topicId);
41+
Publisher publisher = null;
42+
try {
43+
// Create a publisher instance with default settings bound to the topic
44+
publisher = Publisher.defaultBuilder(topicName).build();
45+
List<String> messages = getMessages();
46+
47+
// schedule publishing one message at a time : messages get automatically batched
48+
for (String message : messages) {
49+
// convert message to bytes
50+
ByteString data = ByteString.copyFromUtf8(message);
51+
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
52+
53+
// Once published, returns a server-assigned message id (unique within the topic)
54+
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
55+
messageIdFutures.add(messageIdFuture);
56+
}
57+
} finally {
58+
// wait on any pending publish requests.
59+
messageIds = ApiFutures.allAsList(messageIdFutures).get();
60+
61+
for (String messageId : messageIds) {
62+
System.out.println("published with message ID: " + messageId);
63+
}
64+
65+
if (publisher != null) {
66+
// When finished with the publisher, shutdown to free up resources.
67+
publisher.shutdown();
68+
}
69+
}
70+
return messageIds;
71+
}
72+
73+
private static List<String> getMessages() {
74+
List<String> messages = new ArrayList<>();
75+
for (int i = 0; i < MESSAGE_COUNT; i++) {
76+
messages.add("message-" + String.valueOf(i));
77+
}
78+
return messages;
79+
}
80+
81+
public static void main(String... args) throws Exception {
82+
// topic id, eg. "my-topic-id"
83+
String topicId = args[0];
84+
publishMessages(topicId);
85+
}
86+
}
87+
// [END pubsub_quickstart_quickstart]
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)