|
76 | 76 | * |
77 | 77 | * <p>If no credentials are provided, the {@link Publisher} will use application default credentials |
78 | 78 | * through {@link GoogleCredentials#getApplicationDefault}. |
79 | | - * |
80 | | - * <p>For example, a {@link Publisher} can be constructed and used to publish a list of messages as |
81 | | - * follows: |
82 | | - * |
83 | | - * <pre><code> |
84 | | - * Publisher publisher = |
85 | | - * Publisher.newBuilder(MY_TOPIC) |
86 | | - * .setMaxBundleDuration(new Duration(10 * 1000)) |
87 | | - * .build(); |
88 | | - * List<RpcFuture<String>> results = new ArrayList<>(); |
89 | | - * |
90 | | - * for (PubsubMessage messages : messagesToPublish) { |
91 | | - * results.add(publisher.publish(message)); |
92 | | - * } |
93 | | - * |
94 | | - * Futures.addCallback( |
95 | | - * Futures.allAsList(results), |
96 | | - * new FutureCallback<List<String>>() { |
97 | | - * @Override |
98 | | - * public void onSuccess(List<String> messageIds) { |
99 | | - * // ... process the acknowledgement of publish ... |
100 | | - * } |
101 | | - * @Override |
102 | | - * public void onFailure(Throwable t) { |
103 | | - * // .. handle the failure ... |
104 | | - * } |
105 | | - * }); |
106 | | - * |
107 | | - * // Ensure all the outstanding messages have been published before shutting down your process. |
108 | | - * publisher.shutdown(); |
109 | | - * </code></pre> |
110 | 79 | */ |
111 | 80 | public class Publisher { |
112 | 81 | private static final Logger logger = Logger.getLogger(Publisher.class.getName()); |
@@ -208,29 +177,22 @@ public TopicName getTopicName() { |
208 | 177 | * future might immediately fail with a {@link FlowController.FlowControlException} or block the |
209 | 178 | * current thread until there are more resources available to publish. |
210 | 179 | * |
211 | | - * <p>Example of publishing messages. |
212 | | - * <pre> {@code |
213 | | - * String projectName = "my_project_name"; |
214 | | - * String topicName = "my_topic_name"; |
215 | | - * Publisher publisher = Publisher.newBuilder(TopicName.create(projectName, topicName)).build(); |
216 | | - * List<String> messages = Arrays.asList("message1", "message2"); |
217 | | - * |
218 | | - * for (String message : messages) { |
219 | | - * ByteString data = ByteString.copyFromUtf8(message); |
220 | | - * PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); |
221 | | - * RpcFuture<String> messageIdFuture = publisher.publish(pubsubMessage); |
222 | | - * messageIdFuture.addCallback(new RpcFutureCallback<String>() { |
223 | | - * public void onSuccess(String messageId) { |
224 | | - * System.out.println("published with message id: " + messageId); |
225 | | - * } |
226 | | - * |
227 | | - * public void onFailure(Throwable t) { |
228 | | - * System.out.println("failed to publish: " + t); |
229 | | - * } |
230 | | - * }); |
231 | | - * } |
232 | | - * |
233 | | - * publisher.shutdown(); |
| 180 | + * <p>Example of publishing a message. |
| 181 | + * |
| 182 | + * <pre>{@code |
| 183 | + * String message = "my_message"; |
| 184 | + * ByteString data = ByteString.copyFromUtf8(message); |
| 185 | + * PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); |
| 186 | + * RpcFuture<String> messageIdFuture = publisher.publish(pubsubMessage); |
| 187 | + * messageIdFuture.addCallback(new RpcFutureCallback<String>() { |
| 188 | + * public void onSuccess(String messageId) { |
| 189 | + * System.out.println("published with message id: " + messageId); |
| 190 | + * } |
| 191 | + * |
| 192 | + * public void onFailure(Throwable t) { |
| 193 | + * System.out.println("failed to publish: " + t); |
| 194 | + * } |
| 195 | + * }); |
234 | 196 | * }</pre> |
235 | 197 | * |
236 | 198 | * @param message the message to publish. |
@@ -354,7 +316,7 @@ public V apply(X input) { |
354 | 316 | return callback.apply(input); |
355 | 317 | } |
356 | 318 | })); |
357 | | - } |
| 319 | + } |
358 | 320 | } |
359 | 321 |
|
360 | 322 | private void setupDurationBasedPublishAlarm() { |
@@ -660,6 +622,30 @@ private Builder(TopicName topic) { |
660 | 622 | * <p>For performance, this client benefits from having multiple channels open at once. Users |
661 | 623 | * are encouraged to provide instances of {@code ChannelProvider} that creates new channels |
662 | 624 | * instead of returning pre-initialized ones. |
| 625 | + * |
| 626 | + * <p>Example of creating a {@code Publisher}. |
| 627 | + * |
| 628 | + * <pre>{@code |
| 629 | + * String projectName = "my_project"; |
| 630 | + * String topicName = "my_topic"; |
| 631 | + * TopicName topic = TopicName.create(projectName, topicName); |
| 632 | + * Publisher publisher = Publisher.newBuilder(topic).build(); |
| 633 | + * }</pre> |
| 634 | + * |
| 635 | + * <p>Example of creating a {@code Publisher}. |
| 636 | + * |
| 637 | + * <pre>{@code |
| 638 | + * String projectName = "my_project"; |
| 639 | + * String topicName = "my_topic"; |
| 640 | + * TopicName topic = TopicName.create(projectName, topicName); |
| 641 | + * Publisher publisher = Publisher.newBuilder(topic).build(); |
| 642 | + * try { |
| 643 | + * // ... |
| 644 | + * } finally { |
| 645 | + * // When finished with the publisher, make sure to shutdown to free up resources. |
| 646 | + * publisher.shutdown(); |
| 647 | + * } |
| 648 | + * }</pre> |
663 | 649 | */ |
664 | 650 | public Builder setChannelProvider(ChannelProvider channelProvider) { |
665 | 651 | this.channelProvider = Preconditions.checkNotNull(channelProvider); |
|
0 commit comments