10000 Update PublisherExample.java (#1097) · tuliocota/java-docs-samples@86603c2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 86603c2

Browse files
chenyumicjabubake
chenyumic
authored andcommitted
Update PublisherExample.java (GoogleCloudPlatform#1097)
* Update PublisherExample.java
1 parent 1d07106 commit 86603c2

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
// [START pubsub_quickstart_publisher]
1919

2020
import com.google.api.core.ApiFuture;
21-
import com.google.api.core.ApiFutureCallback;
2221
import com.google.api.core.ApiFutures;
23-
import com.google.api.gax.rpc.ApiException;
2422
import com.google.cloud.ServiceOptions;
2523
import com.google.cloud.pubsub.v1.Publisher;
2624
import com.google.protobuf.ByteString;
2725
import com.google.pubsub.v1.ProjectTopicName;
2826
import com.google.pubsub.v1.PubsubMessage;
2927

28+
import java.util.ArrayList;
29+
import java.util.Arrays;
30+
import java.util.List;
31+
3032
public class PublisherExample {
3133

3234
// use the default project id
@@ -41,6 +43,8 @@ public static void main(String... args) throws Exception {
4143
int messageCount = Integer.parseInt(args[1]);
4244
ProjectTopicName topicName = ProjectTopicName.of(PROJECT_ID, topicId);
4345
Publisher publisher = null;
46+
List<ApiFuture<String>> futures = new ArrayList<>();
47+
4448
try {
4549
// Create a publisher instance with default settings bound to the topic
4650
publisher = Publisher.newBuilder(topicName).build();
@@ -54,31 +58,18 @@ public static void main(String... args) throws Exception {
5458
.setData(data)
5559
.build();
5660

57-
//schedule a message to be published, messages are automatically batched
61+
// Schedule a message to be published. Messages are automatically batched.
5862
ApiFuture<String> future = publisher.publish(pubsubMessage);
59-
60-
// add an asynchronous callback to handle success / failure
61-
ApiFutures.addCallback(future, new ApiFutureCallback<String>() {
62-
63-
@Override
64-
public void onFailure(Throwable throwable) {
65-
if (throwable instanceof ApiException) {
66-
ApiException apiException = ((ApiException) throwable);
67-
// details on the API exception
68-
System.out.println(apiException.getStatusCode().getCode());
69-
System.out.println(apiException.isRetryable());
70-
}
71-
System.out.println("Error publishing message : " + message);
72-
}
73-
74-
@Override
75-
public void onSuccess(String messageId) {
76-
// Once published, returns server-assigned message ids (unique within the topic)
77-
System.out.println(messageId);
78-
}
79-
});
63+
futures.add(future);
8064
}
8165
} finally {
66+
// Wait on any pending requests
67+
List<String> messageIds = ApiFutures.allAsList(futures).get();
68+
69+
for (String messageId : messageIds) {
70+
System.out.println(messageId);
71+
}
72+
8273
if (publisher != null) {
8374
// When finished with the publisher, shutdown to free up resources.
8475
publisher.shutdown();

0 commit comments

Comments
 (0)
0