Skip to content

Commit 613b96b

Browse files
committed
pr comment
1 parent a727b8c commit 613b96b

2 files changed

Lines changed: 79 additions & 76 deletions

File tree

google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherSnippets.java

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,50 @@
2626
import java.util.List;
2727

2828
public class PublisherSnippets {
29+
private final Publisher publisher;
30+
31+
public PublisherSnippets(Publisher publisher) {
32+
this.publisher = publisher;
33+
}
34+
2935
/**
30-
* Example of publishing messages.
36+
* Example of publishing a message.
3137
*/
3238
// [TARGET publish(PubsubMessage)]
33-
// [VARIABLE "my_project_name"]
34-
// [VARIABLE "my_topic_name"]
35-
public void publish(String projectName, String topicName) throws Exception {
39+
// [VARIABLE "my_message"]
40+
public void publish(String message) {
3641
// [START publish]
37-
Publisher publisher = Publisher.newBuilder(TopicName.create(projectName, topicName)).build();
38-
List<String> messages = Arrays.asList("message1", "message2");
42+
ByteString data = ByteString.copyFromUtf8(message);
43+
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
44+
RpcFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
45+
messageIdFuture.addCallback(new RpcFutureCallback<String>() {
46+
public void onSuccess(String messageId) {
47+
System.out.println("published with message id: " + messageId);
48+
}
3949

40-
for (String message : messages) {
41-
ByteString data = ByteString.copyFromUtf8(message);
42-
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
43-
RpcFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
44-
messageIdFuture.addCallback(new RpcFutureCallback<String>() {
45-
public void onSuccess(String messageId) {
46-
System.out.println("published with message id: " + messageId);
47-
}
50+
public void onFailure(Throwable t) {
51+
System.out.println("failed to publish: " + t);
52+
}
53+
});
54+
// [END publish]
55+
}
4856

49-
public void onFailure(Throwable t) {
50-
System.out.println("failed to publish: " + t);
51-
}
52-
});
57+
/**
58+
* Example of creating a {@code Publisher}.
59+
*/
60+
// [TARGET newBuilder(TopicName)]
61+
// [VARIABLE "my_project"]
62+
// [VARIABLE "my_topic"]
63+
public static void newBuilder(String projectName, String topicName) throws Exception {
64+
// [START newBuilder]
65+
TopicName topic = TopicName.create(projectName, topicName);
66+
Publisher publisher = Publisher.newBuilder(topic).build();
67+
try {
68+
// ...
69+
} finally {
70+
// When finished with the publisher, make sure to shutdown to free up resources.
71+
publisher.shutdown();
5372
}
54-
55-
publisher.shutdown();
56-
// [END publish]
73+
// [END newBuilder]
5774
}
5875
}

google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/Publisher.java

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -76,37 +76,6 @@
7676
*
7777
* <p>If no credentials are provided, the {@link Publisher} will use application default credentials
7878
* 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&lt;RpcFuture&lt;String&gt;&gt; results = new ArrayList&lt;&gt;();
89-
*
90-
* for (PubsubMessage messages : messagesToPublish) {
91-
* results.add(publisher.publish(message));
92-
* }
93-
*
94-
* Futures.addCallback(
95-
* Futures.allAsList(results),
96-
* new FutureCallback&lt;List&lt;String&gt;&gt;() {
97-
* &#64;Override
98-
* public void onSuccess(List&lt;String&gt; messageIds) {
99-
* // ... process the acknowledgement of publish ...
100-
* }
101-
* &#64;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>
11079
*/
11180
public class Publisher {
11281
private static final Logger logger = Logger.getLogger(Publisher.class.getName());
@@ -208,29 +177,22 @@ public TopicName getTopicName() {
208177
* future might immediately fail with a {@link FlowController.FlowControlException} or block the
209178
* current thread until there are more resources available to publish.
210179
*
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+
* });
234196
* }</pre>
235197
*
236198
* @param message the message to publish.
@@ -354,7 +316,7 @@ public V apply(X input) {
354316
return callback.apply(input);
355317
}
356318
}));
357-
}
319+
}
358320
}
359321

360322
private void setupDurationBasedPublishAlarm() {
@@ -660,6 +622,30 @@ private Builder(TopicName topic) {
660622
* <p>For performance, this client benefits from having multiple channels open at once. Users
661623
* are encouraged to provide instances of {@code ChannelProvider} that creates new channels
662624
* 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>
663649
*/
664650
public Builder setChannelProvider(ChannelProvider channelProvider) {
665651
this.channelProvider = Preconditions.checkNotNull(channelProvider);

0 commit comments

Comments
 (0)