Skip to content

Commit ade7981

Browse files
samples: add publish with flow control sample (#717)
* samples: add publish with flow control sample * address kamal's comments
1 parent 2449a7c commit ade7981

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2021 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_publisher_flow_control]
20+
21+
import com.google.api.core.ApiFuture;
22+
import com.google.api.core.ApiFutures;
23+
import com.google.api.gax.batching.BatchingSettings;
24+
import com.google.api.gax.batching.FlowControlSettings;
25+
import com.google.api.gax.batching.FlowController.LimitExceededBehavior;
26+
import com.google.cloud.pubsub.v1.Publisher;
27+
import com.google.protobuf.ByteString;
28+
import com.google.pubsub.v1.PubsubMessage;
29+
import com.google.pubsub.v1.TopicName;
30+
import java.io.IOException;
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
import java.util.concurrent.ExecutionException;
34+
import java.util.concurrent.TimeUnit;
35+
36+
public class PublishWithFlowControlExample {
37+
public static void main(String... args) throws Exception {
38+
// TODO(developer): Replace these variables before running the sample.
39+
String projectId = "your-project-id";
40+
String topicId = "your-topic-id";
41+
42+
publishWithFlowControlExample(projectId, topicId);
43+
}
44+
45+
public static void publishWithFlowControlExample(String projectId, String topicId)
46+
throws IOException, ExecutionException, InterruptedException {
47+
TopicName topicName = TopicName.of(projectId, topicId);
48+
Publisher publisher = null;
49+
List<ApiFuture<String>> messageIdFutures = new ArrayList<>();
50+
51+
try {
52+
// Configure how many messages the publisher client can hold in memory
53+
// and what to do when messages exceed the limit.
54+
FlowControlSettings flowControlSettings =
55+
FlowControlSettings.newBuilder()
56+
// Block more messages from being published when the limit is reached. The other
57+
// options are Ignore (or continue publishing) and ThrowException (or error out).
58+
.setLimitExceededBehavior(LimitExceededBehavior.Block)
59+
.setMaxOutstandingRequestBytes(10 * 1024 * 1024L) // 10 MiB
60+
.setMaxOutstandingElementCount(100L) // 100 messages
61+
.build();
62+
63+
// By default, messages are not batched.
64+
BatchingSettings batchingSettings =
65+
BatchingSettings.newBuilder().setFlowControlSettings(flowControlSettings).build();
66+
67+
publisher = Publisher.newBuilder(topicName).setBatchingSettings(batchingSettings).build();
68+
69+
// Publish 1000 messages in quick succession may be constrained by publisher flow control.
70+
for (int i = 0; i < 1000; i++) {
71+
String message = "message " + i;
72+
ByteString data = ByteString.copyFromUtf8(message);
73+
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
74+
75+
// Once published, returns a server-assigned message id (unique within the topic)
76+
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
77+
messageIdFutures.add(messageIdFuture);
78+
}
79+
} finally {
80+
// Wait on any pending publish requests.
81+
List<String> messageIds = ApiFutures.allAsList(messageIdFutures).get();
82+
83+
System.out.println(
84+
"Published " + messageIds.size() + " messages with flow control settings.");
85+
86+
if (publisher != null) {
87+
// When finished with the publisher, shut down to free up resources.
88+
publisher.shutdown();
89+
publisher.awaitTermination(1, TimeUnit.MINUTES);
90+
}
91+
}
92+
}
93+
}
94+
// [END pubsub_publisher_flow_control]

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public void testPublisher() throws Exception {
9696
PublishWithBatchSettingsExample.publishWithBatchSettingsExample(projectId, topicId);
9797
assertThat(bout.toString()).contains("Published 100 messages with batch settings.");
9898

99+
bout.reset();
100+
// Test publish with flow control settings.
101+
PublishWithFlowControlExample.publishWithFlowControlExample(projectId, topicId);
102+
assertThat(bout.toString()).contains("Published 1000 messages with flow control settings.");
103+
99104
bout.reset();
100105
// Test publish with concurrency control.
101106
PublishWithConcurrencyControlExample.publishWithConcurrencyControlExample(projectId, topicId);

0 commit comments

Comments
 (0)