-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pubsub getting started #684
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
1435445
initial commit iap samples
jabubake ee46851
adding test
jabubake b491208
cleanup
jabubake dca4a02
license update
jabubake 0327851
readme cleanup
jabubake 79af7d6
license cleanup
jabubake 64bf09a
Merge branch 'master' into iap_samples
jabubake e242a82
pom.xml cleanup
jabubake 7a13ebc
using new tooling
jabubake ebd29dc
adding doc tags
jabubake 65e9a24
PubSub getting started : initial commit
jabubake f3176bc
getting started guide files
jabubake 3f57f82
Merge branch 'master' of https://github.com/GoogleCloudPlatform/java-…
jabubake e53c182
formatting fixes
jabubake 011c88f
create subscription before publishing a message
jabubake 9404a36
simplifying publisher, subscriber snippets
jabubake b9038c9
Merge remote-tracking branch 'origin/master' into pubsub_getting_started
jabubake e2a3302
updates
jabubake 5129e14
remove unused code
jabubake 48ecccc
removing unused imports
jabubake 952f001
formatting updates
jabubake 34f57fa
Merge branch 'master' of https://github.com/GoogleCloudPlatform/java-…
jabubake 3e59626
pub/sub getting started
jabubake e6ec754
update README instructions
jabubake 4093499
Merge branch 'master' of https://github.com/GoogleCloudPlatform/java-…
jabubake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
pubsub/cloud-client/src/main/java/com/example/pubsub/CreatePullSubscriptionExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright 2017, Google, Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package com.example.pubsub; | ||
|
||
// [START pubsub_quickstart_create_subscription] | ||
import com.google.cloud.ServiceOptions; | ||
import com.google.cloud.pubsub.v1.SubscriptionAdminClient; | ||
import com.google.pubsub.v1.PushConfig; | ||
import com.google.pubsub.v1.Subscription; | ||
import com.google.pubsub.v1.SubscriptionName; | ||
import com.google.pubsub.v1.TopicName; | ||
|
||
public class CreatePullSubscriptionExample { | ||
|
||
/** | ||
* Create a pull subscription. | ||
* | ||
* @param args topic subscriptionId | ||
* @throws Exception exception thrown if operation is unsuccessful | ||
*/ | ||
public static void main(String... args) throws Exception { | ||
|
||
// Your Google Cloud Platform project ID | ||
String projectId = ServiceOptions.getDefaultProjectId(); | ||
|
||
// Your topic ID, eg. "my-topic" | ||
String topicId = args[0]; | ||
|
||
// Your subscription ID eg. "my-sub" | ||
String subscriptionId = args[1]; | ||
|
||
TopicName topicName = TopicName.create(projectId, topicId); | ||
|
||
// Create a new subscription | ||
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); | ||
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) { | ||
// create a pull subscription with default acknowledgement deadline (= 10 seconds) | ||
Subscription subscription = | ||
subscriptionAdminClient.createSubscription( | ||
subscriptionName, topicName, PushConfig.getDefaultInstance(), 0); | ||
} | ||
|
||
System.out.printf( | ||
"Subscription %s:%s created.\n", | ||
subscriptionName.getProject(), subscriptionName.getSubscription()); | ||
} | ||
} | ||
// [END pubsub_quickstart_create_subscription] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2017 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.pubsub; | ||
// [START pubsub_quickstart_publisher] | ||
|
||
import com.google.api.core.ApiFuture; | ||
import com.google.api.core.ApiFutures; | ||
import com.google.cloud.ServiceOptions; | ||
import com.google.cloud.pubsub.v1.Publisher; | ||
import com.google.protobuf.ByteString; | ||
import com.google.pubsub.v1.PubsubMessage; | ||
import com.google.pubsub.v1.TopicName; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class PublisherExample { | ||
|
||
static final int MESSAGE_COUNT = 5; | ||
|
||
// use the default project id | ||
private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); | ||
|
||
//schedule a message to be published, messages are automatically batched | ||
private static ApiFuture<String> publishMessage(Publisher publisher, String message) | ||
throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than throwing an exception, shouldn't we catch them? and explain what's going on? |
||
// convert message to bytes | ||
ByteString data = ByteString.copyFromUtf8(message); | ||
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); | ||
return publisher.publish(pubsubMessage); | ||
} | ||
|
||
/** Publish messages to a topic. */ | ||
public static void main(String... args) throws Exception { | ||
// topic id, eg. "my-topic" | ||
String topicId = args[0]; | ||
TopicName topicName = TopicName.create(PROJECT_ID, topicId); | ||
Publisher publisher = null; | ||
List<ApiFuture<String>> apiFutures = new ArrayList<>(); | ||
try { | ||
// Create a publisher instance with default settings bound to the topic | ||
publisher = Publisher.defaultBuilder(topicName).build(); | ||
for (int i = 0; i < MESSAGE_COUNT; i++) { | ||
String message = "message-" + i; | ||
ApiFuture<String> messageId = publishMessage(publisher, message); | ||
apiFutures.add(messageId); | ||
} | ||
} finally { | ||
// Once published, returns server-assigned message ids (unique within the topic) | ||
List<String> messageIds = ApiFutures.allAsList(apiFutures).get(); | ||
for (String messageId : messageIds) { | ||
System.out.println(messageId); | ||
} | ||
if (publisher != null) { | ||
// When finished with the publisher, shutdown to free up resources. | ||
publisher.shutdown(); | ||
} | ||
} | ||
} | ||
} | ||
// [END pubsub_quickstart_quickstart] |
70 changes: 70 additions & 0 deletions
70
pubsub/cloud-client/src/main/java/com/example/pubsub/SubscriberExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2017 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.pubsub; | ||
|
||
// [START pubsub_quickstart_subscriber] | ||
|
||
import com.google.cloud.ServiceOptions; | ||
import com.google.cloud.pubsub.v1.AckReplyConsumer; | ||
import com.google.cloud.pubsub.v1.MessageReceiver; | ||
import com.google.cloud.pubsub.v1.Subscriber; | ||
import com.google.pubsub.v1.PubsubMessage; | ||
import com.google.pubsub.v1.SubscriptionName; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.LinkedBlockingDeque; | ||
|
||
public class SubscriberExample { | ||
|
||
// use the default project id | ||
private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); | ||
|
||
private static final BlockingQueue<PubsubMessage> messages = new LinkedBlockingDeque<>(); | ||
|
||
static class MessageReceiverExample implements MessageReceiver { | ||
|
||
@Override | ||
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) { | ||
messages.offer(message); | ||
consumer.ack(); | ||
} | ||
} | ||
|
||
/** Receive messages over a subscription. */ | ||
public static void main(String... args) throws Exception { | ||
// set subscriber id, eg. my-sub | ||
String subscriptionId = args[0]; | ||
SubscriptionName subscriptionName = SubscriptionName.create(PROJECT_ID, subscriptionId); | ||
Subscriber subscriber = null; | ||
try { | ||
// create a subscriber bound to the asynchronous message receiver | ||
subscriber = | ||
Subscriber.defaultBuilder(subscriptionName, new MessageReceiverExample()).build(); | ||
subscriber.startAsync().awaitRunning(); | ||
// Continue to listen to messages | ||
while (true) { | ||
PubsubMessage message = messages.take(); | ||
System.out.println("Message Id: " + message.getMessageId()); | ||
System.out.println("Data: " + message.getData().toStringUtf8()); | ||
} | ||
} finally { | ||
if (subscriber != null) { | ||
subscriber.stopAsync(); | ||
} | ||
} | ||
} | ||
} | ||
// [END pubsub_quickstart_subscriber] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than throw an exception (above), shouldn't we catch them and explain why they might get one and what to do if they do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Submitted googleapis/google-cloud-java#2295