Repository navigation
docs(pubsub): Create a Cloud Pub/Sub sample for publishing with hedging - #14623
Conversation
…ng and bump the samples libraries-bom version
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request updates the libraries-bom dependency version to 26.90.0 and introduces a new sample snippet, PublishWithHedgingSettingsExample.java, which demonstrates how to publish Pub/Sub messages with custom hedging settings. It also adds a corresponding integration test in PublisherIT.java. Feedback on the changes suggests refactoring the resource management of the Publisher instance to use a try-with-resources block for cleaner and safer resource handling, as well as using a double instead of a float for the refillRatio setting to avoid implicit widening.
| 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); | ||
| } | ||
| } |
There was a problem hiding this comment.
Using try-with-resources is the modern, idiomatic Java way to manage resources. Since Publisher implements AutoCloseable (via BackgroundResource), its close() method automatically handles shutdown() and awaitTermination(). Refactoring this to use try-with-resources reduces boilerplate and ensures the publisher is safely closed. Additionally, HedgingSettings.Builder#setRefillRatio expects a double parameter, so we can use double directly instead of float to avoid implicit widening.
// 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
double refillRatio = 0.05; // 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
try (Publisher 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);
}
This also bumps the samples libraries-bom version