|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. |
| 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 com.example.pubsub; |
| 18 | +// [START pubsub_quickstart_publisher] |
| 19 | + |
| 20 | +import com.google.api.core.ApiFuture; |
| 21 | +import com.google.api.core.ApiFutures; |
| 22 | +import com.google.cloud.ServiceOptions; |
| 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.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +public class PublisherExample { |
| 31 | + |
| 32 | + static final int MESSAGE_COUNT = 5; |
| 33 | + |
| 34 | + // use the default project id |
| 35 | + private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); |
| 36 | + |
| 37 | + //schedule a message to be published, messages are automatically batched |
| 38 | + private static ApiFuture<String> publishMessage(Publisher publisher, String message) |
| 39 | + throws Exception { |
| 40 | + // convert message to bytes |
| 41 | + ByteString data = ByteString.copyFromUtf8(message); |
| 42 | + PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); |
| 43 | + return publisher.publish(pubsubMessage); |
| 44 | + } |
| 45 | + |
| 46 | + /** Publish messages to a topic. */ |
| 47 | + public static void main(String... args) throws Exception { |
| 48 | + // topic id, eg. "my-topic" |
| 49 | + String topicId = args[0]; |
| 50 | + TopicName topicName = TopicName.create(PROJECT_ID, topicId); |
| 51 | + Publisher publisher = null; |
| 52 | + List<ApiFuture<String>> apiFutures = new ArrayList<>(); |
| 53 | + try { |
| 54 | + // Create a publisher instance with default settings bound to the topic |
| 55 | + publisher = Publisher.defaultBuilder(topicName).build(); |
| 56 | + for (int i = 0; i < MESSAGE_COUNT; i++) { |
| 57 | + String message = "message-" + i; |
| 58 | + ApiFuture<String> messageId = publishMessage(publisher, message); |
| 59 | + apiFutures.add(messageId); |
| 60 | + } |
| 61 | + } finally { |
| 62 | + // Once published, returns server-assigned message ids (unique within the topic) |
| 63 | + List<String> messageIds = ApiFutures.allAsList(apiFutures).get(); |
| 64 | + for (String messageId : messageIds) { |
| 65 | + System.out.println(messageId); |
| 66 | + } |
| 67 | + if (publisher != null) { |
| 68 | + // When finished with the publisher, shutdown to free up resources. |
| 69 | + publisher.shutdown(); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +// [END pubsub_quickstart_quickstart] |
0 commit comments