-
Notifications
You must be signed in to change notification settings - Fork 100
docs(samples): Optimistic subscribe sample #2063
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
Merged
michaelpri10
merged 6 commits into
googleapis:main
from
michaelpri10:optimistic-subscribe-sample
Jul 1, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
403de22
docs(samples): Add code sample for optimistic subscribe
michaelpri10 b75b471
docs(samples): Fix formatting on test
michaelpri10 6002fb4
docs(samples): Use an error listener instead of catching an exception…
michaelpri10 db2d905
test: Add exception handler to OptimisticSubscribeExample
michaelpri10 62bd047
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] ad2ea92
Merge branch 'main' into optimistic-subscribe-sample
michaelpri10 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 hidden or 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
103 changes: 103 additions & 0 deletions
103
samples/snippets/src/main/java/pubsub/OptimisticSubscribeExample.java
This file contains hidden or 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,103 @@ | ||
| /* | ||
| * Copyright 2024 Google LLC | ||
| * | ||
| * 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 pubsub; | ||
|
|
||
| // [START pubsub_optimistic_subscribe] | ||
|
|
||
| import com.google.api.gax.rpc.NotFoundException; | ||
| import com.google.cloud.pubsub.v1.AckReplyConsumer; | ||
| import com.google.cloud.pubsub.v1.MessageReceiver; | ||
| import com.google.cloud.pubsub.v1.Subscriber; | ||
| import com.google.cloud.pubsub.v1.SubscriptionAdminClient; | ||
| import com.google.common.util.concurrent.MoreExecutors; | ||
| import com.google.pubsub.v1.ProjectSubscriptionName; | ||
| import com.google.pubsub.v1.PubsubMessage; | ||
| import com.google.pubsub.v1.PushConfig; | ||
| import com.google.pubsub.v1.Subscription; | ||
| import com.google.pubsub.v1.TopicName; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.TimeoutException; | ||
|
|
||
| public class OptimisticSubscribeExample { | ||
| public static void main(String... args) throws Exception { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String projectId = "your-project-id"; | ||
| String subscriptionId = "your-subscription-id"; | ||
| String topicId = "your-topic-id"; | ||
|
|
||
| optimisticSubscribeExample(projectId, subscriptionId, topicId); | ||
| } | ||
|
|
||
| public static void optimisticSubscribeExample( | ||
| String projectId, String subscriptionId, String topicId) throws IOException { | ||
| ProjectSubscriptionName subscriptionName = | ||
| ProjectSubscriptionName.of(projectId, subscriptionId); | ||
|
|
||
| // Instantiate an asynchronous message receiver. | ||
| MessageReceiver receiver = | ||
| (PubsubMessage message, AckReplyConsumer consumer) -> { | ||
| // Handle incoming message, then ack the received message. | ||
| System.out.println("Id: " + message.getMessageId()); | ||
| System.out.println("Data: " + message.getData().toStringUtf8()); | ||
| consumer.ack(); | ||
| }; | ||
|
|
||
| Subscriber subscriber = null; | ||
| try { | ||
| subscriber = Subscriber.newBuilder(subscriptionName, receiver).build(); | ||
|
|
||
| // Listen for resource NOT_FOUND errors and rebuild the subscriber and restart subscribing | ||
| // when the current subscriber encounters these errors. | ||
| subscriber.addListener( | ||
| new Subscriber.Listener() { | ||
| public void failed(Subscriber.State from, Throwable failure) { | ||
| System.out.println(failure.getStackTrace()); | ||
| if (failure instanceof NotFoundException) { | ||
| try (SubscriptionAdminClient subscriptionAdminClient = | ||
| SubscriptionAdminClient.create()) { | ||
| TopicName topicName = TopicName.of(projectId, topicId); | ||
| // Create a pull subscription with default acknowledgement deadline of 10 seconds. | ||
| // The client library will automatically extend acknowledgement deadlines. | ||
| Subscription subscription = | ||
| subscriptionAdminClient.createSubscription( | ||
| subscriptionName, topicName, PushConfig.getDefaultInstance(), 10); | ||
| System.out.println("Created pull subscription: " + subscription.getName()); | ||
| optimisticSubscribeExample(projectId, subscriptionId, topicId); | ||
| } catch (IOException err) { | ||
| System.out.println("Failed to create pull subscription: " + err.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| MoreExecutors.directExecutor()); | ||
|
|
||
| subscriber.startAsync().awaitRunning(); | ||
| System.out.printf("Listening for messages on %s:\n", subscriptionName.toString()); | ||
| subscriber.awaitTerminated(30, TimeUnit.SECONDS); | ||
| } catch (IllegalStateException e) { | ||
| // Prevent an exception from being thrown if it is the expected NotFoundException | ||
| if (!(subscriber.failureCause() instanceof NotFoundException)) { | ||
| throw e; | ||
| } | ||
| } catch (TimeoutException e) { | ||
| subscriber.stopAsync(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // [END pubsub_optimistic_subscribe] | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.