Repository navigation
docs(pubsub): Create a Cloud Pub/Sub sample for publishing with hedging #14623
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 1 commit into
googleapis:main
from
michaelpri10:docs/publish-hedging-sample
Oct 9, 2026
Merged
Changes from all commits
Commits
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
81 changes: 81 additions & 0 deletions
81
java-pubsub/samples/snippets/src/main/java/pubsub/PublishWithHedgingSettingsExample.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,81 @@ | ||
| /* | ||
| * Copyright 2026 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_publish_hedging_settings] | ||
|
|
||
| import com.google.api.core.ApiFuture; | ||
| import com.google.cloud.pubsub.v1.HedgingSettings; | ||
| 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.io.IOException; | ||
| import java.time.Duration; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| public class PublishWithHedgingSettingsExample { | ||
| public static void main(String... args) throws Exception { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String projectId = "your-project-id"; | ||
| String topicId = "your-topic-id"; | ||
|
|
||
| publishWithHedgingSettingsExample(projectId, topicId); | ||
| } | ||
|
|
||
| public static void publishWithHedgingSettingsExample(String projectId, String topicId) | ||
| throws IOException, ExecutionException, InterruptedException { | ||
| TopicName topicName = TopicName.of(projectId, topicId); | ||
| Publisher publisher = null; | ||
|
|
||
| try { | ||
| // Hedging settings configures how and when the publisher sends multiple concurrent publish | ||
| // requests to reduce tail latency. | ||
| Duration hedgeDelay = Duration.ofMillis(500); // default: 1000 ms (1 second) | ||
| int maxTokens = 100; // default: 50 | ||
| float refillRatio = 0.05f; // default: 0.1 | ||
|
|
||
| HedgingSettings hedgingSettings = | ||
| HedgingSettings.newBuilder() | ||
| .setHedgeDelay(hedgeDelay) | ||
| .setMaxTokens(maxTokens) | ||
| .setRefillRatio(refillRatio) | ||
| .build(); | ||
|
|
||
| // Create a publisher instance with hedging settings bound to the topic | ||
| publisher = Publisher.newBuilder(topicName).setHedgingSettings(hedgingSettings).build(); | ||
|
|
||
| String message = "Hello world!"; | ||
| ByteString data = ByteString.copyFromUtf8(message); | ||
| PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); | ||
|
|
||
| // Once published, returns a server-assigned message id (unique within the topic) | ||
| ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage); | ||
| String messageId = messageIdFuture.get(); | ||
| System.out.println("Published a message with hedging settings: " + messageId); | ||
|
|
||
| } finally { | ||
| if (publisher != null) { | ||
| // When finished with the publisher, shutdown to free up resources. | ||
| publisher.shutdown(); | ||
| publisher.awaitTermination(1, TimeUnit.MINUTES); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // [END pubsub_publish_hedging_settings] | ||
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
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.
Using try-with-resources is the modern, idiomatic Java way to manage resources. Since
PublisherimplementsAutoCloseable(viaBackgroundResource), itsclose()method automatically handlesshutdown()andawaitTermination(). Refactoring this to use try-with-resources reduces boilerplate and ensures the publisher is safely closed. Additionally,HedgingSettings.Builder#setRefillRatioexpects adoubleparameter, so we can usedoubledirectly instead offloatto avoid implicit widening.