|
| 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 | +} |
0 commit comments