Skip to content

Commit 17d16bb

Browse files
samples: publish with gRPC compression (#1131)
* samples: publish with gRPC compression * lint * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * add license header * remove explicit versioning in pom * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 44b71d0 commit 17d16bb

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-pubsub/tree/m
270270
| Publish With Custom Attributes Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/PublishWithCustomAttributesExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/PublishWithCustomAttributesExample.java) |
271271
| Publish With Error Handler Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/PublishWithErrorHandlerExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/PublishWithErrorHandlerExample.java) |
272272
| Publish With Flow Control Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/PublishWithFlowControlExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/PublishWithFlowControlExample.java) |
273+
| Publish With Grpc Compression Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/PublishWithGrpcCompressionExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/PublishWithGrpcCompressionExample.java) |
273274
| Publish With Ordering Keys | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/PublishWithOrderingKeys.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/PublishWithOrderingKeys.java) |
274275
| Publish With Retry Settings Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/PublishWithRetrySettingsExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/PublishWithRetrySettingsExample.java) |
275276
| Publisher Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/PublisherExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/PublisherExample.java) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2022 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+
import com.google.api.core.ApiFuture;
20+
import com.google.cloud.pubsub.v1.Publisher;
21+
import com.google.protobuf.ByteString;
22+
import com.google.pubsub.v1.PubsubMessage;
23+
import com.google.pubsub.v1.TopicName;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.TimeUnit;
27+
28+
public class PublishWithGrpcCompressionExample {
29+
public static void main(String... args) throws Exception {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "your-project-id";
32+
// Choose an existing topic.
33+
String topicId = "your-topic-id";
34+
35+
publishWithGrpcCompressionExample(projectId, topicId);
36+
}
37+
38+
public static void publishWithGrpcCompressionExample(String projectId, String topicId)
39+
throws IOException, ExecutionException, InterruptedException {
40+
TopicName topicName = TopicName.of(projectId, topicId);
41+
42+
// Create a publisher and set enable compression to true.
43+
Publisher publisher = null;
44+
try {
45+
// Enable compression and configure the compression threshold to 10 bytes (default to 240 B).
46+
// Publish requests of sizes > 10 B (excluding the request headers) will get compressed.
47+
// The number of messages in a publish request is determined by publisher batch settings.
48+
// Batching is turned off by default, i.e. each publish request contains only one message.
49+
publisher =
50+
Publisher.newBuilder(topicName)
51+
.setEnableCompression(true)
52+
.setCompressionBytesThreshold(10L)
53+
.build();
54+
55+
byte[] bytes = new byte[1024];
56+
ByteString data = ByteString.copyFrom(bytes);
57+
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
58+
59+
// Once published, returns a server-assigned message id (unique within the topic).
60+
// You can look up the actual size of the outbound data using the Java Logging API.
61+
// Configure logging properties as shown in
62+
// https://github.com/googleapis/java-pubsub/tree/main/samples/snippets/src/main/resources/logging.properties
63+
// and look for "OUTBOUND DATA" with "length=" in the output log.
64+
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
65+
String messageId = messageIdFuture.get();
66+
System.out.println("Published a compressed message of message ID: " + messageId);
67+
} finally {
68+
if (publisher != null) {
69+
// When finished with the publisher, shutdown to free up resources.
70+
publisher.shutdown();
71+
publisher.awaitTermination(1, TimeUnit.MINUTES);
72+
}
73+
}
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# To run the example with the following logging config:
16+
# cd samples/snippets/
17+
# mvn clean exec:java -Dexec.mainClass=pubsub.PublishWithGrpcCompression -Djava.util.logging.config.file=src/main/resources/logging.properties
18+
19+
handlers=java.util.logging.FileHandler
20+
.level=FINE
21+
java.util.logging.FileHandler.pattern=log.txt
22+
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
23+
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] %4$-5s %5$s %n

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

+5
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,10 @@ public void testPublisher() throws Exception {
124124
for (int i = 1; i <= 4; i++) {
125125
assertThat(bout.toString()).contains("message" + i);
126126
}
127+
128+
bout.reset();
129+
// Test publish with gRPC compression.
130+
PublishWithGrpcCompressionExample.publishWithGrpcCompressionExample(projectId, topicId);
131+
assertThat(bout.toString()).contains("Published a compressed message of message ID: ");
127132
}
128133
}

0 commit comments

Comments
 (0)