Skip to content

Commit 9de611f

Browse files
authored
docs(pubsub): Create a Cloud Pub/Sub sample for publishing with hedging (#14623)
This also bumps the samples libraries-bom version
1 parent 27559f4 commit 9de611f

3 files changed

Lines changed: 87 additions & 1 deletion

File tree

‎java-pubsub/samples/snippets/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<dependency>
4646
<groupId>com.google.cloud</groupId>
4747
<artifactId>libraries-bom</artifactId>
48-
<version>26.76.0</version>
48+
<version>26.90.0</version>
4949
<type>pom</type>
5050
<scope>import</scope>
5151
</dependency>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2026 Google LLC
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 pubsub;
18+
19+
// [START pubsub_publish_hedging_settings]
20+
21+
import com.google.api.core.ApiFuture;
22+
import com.google.cloud.pubsub.v1.HedgingSettings;
23+
import com.google.cloud.pubsub.v1.Publisher;
24+
import com.google.protobuf.ByteString;
25+
import com.google.pubsub.v1.PubsubMessage;
26+
import com.google.pubsub.v1.TopicName;
27+
import java.io.IOException;
28+
import java.time.Duration;
29+
import java.util.concurrent.ExecutionException;
30+
import java.util.concurrent.TimeUnit;
31+
32+
public class PublishWithHedgingSettingsExample {
33+
public static void main(String... args) throws Exception {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String projectId = "your-project-id";
36+
String topicId = "your-topic-id";
37+
38+
publishWithHedgingSettingsExample(projectId, topicId);
39+
}
40+
41+
public static void publishWithHedgingSettingsExample(String projectId, String topicId)
42+
throws IOException, ExecutionException, InterruptedException {
43+
TopicName topicName = TopicName.of(projectId, topicId);
44+
Publisher publisher = null;
45+
46+
try {
47+
// Hedging settings configures how and when the publisher sends multiple concurrent publish
48+
// requests to reduce tail latency.
49+
Duration hedgeDelay = Duration.ofMillis(500); // default: 1000 ms (1 second)
50+
int maxTokens = 100; // default: 50
51+
float refillRatio = 0.05f; // default: 0.1
52+
53+
HedgingSettings hedgingSettings =
54+
HedgingSettings.newBuilder()
55+
.setHedgeDelay(hedgeDelay)
56+
.setMaxTokens(maxTokens)
57+
.setRefillRatio(refillRatio)
58+
.build();
59+
60+
// Create a publisher instance with hedging settings bound to the topic
61+
publisher = Publisher.newBuilder(topicName).setHedgingSettings(hedgingSettings).build();
62+
63+
String message = "Hello world!";
64+
ByteString data = ByteString.copyFromUtf8(message);
65+
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
66+
67+
// Once published, returns a server-assigned message id (unique within the topic)
68+
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
69+
String messageId = messageIdFuture.get();
70+
System.out.println("Published a message with hedging settings: " + messageId);
71+
72+
} finally {
73+
if (publisher != null) {
74+
// When finished with the publisher, shutdown to free up resources.
75+
publisher.shutdown();
76+
publisher.awaitTermination(1, TimeUnit.MINUTES);
77+
}
78+
}
79+
}
80+
}
81+
// [END pubsub_publish_hedging_settings]

‎java-pubsub/samples/snippets/src/test/java/pubsub/PublisherIT.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,10 @@ public void testPublisher() throws Exception {
129129
// Test publish with gRPC compression.
130130
PublishWithGrpcCompressionExample.publishWithGrpcCompressionExample(projectId, topicId);
131131
assertThat(bout.toString()).contains("Published a compressed message of message ID: ");
132+
133+
bout.reset();
134+
// Test publish with hedging settings.
135+
PublishWithHedgingSettingsExample.publishWithHedgingSettingsExample(projectId, topicId);
136+
assertThat(bout.toString()).contains("Published a message with hedging settings: ");
132137
}
133138
}

0 commit comments

Comments
 (0)