From 15ec3e6e6194a9d43cae154ff90900f52dacd16e Mon Sep 17 00:00:00 2001 From: chenyumic Date: Tue, 8 May 2018 15:38:08 -0700 Subject: [PATCH 1/3] Update PublisherExample.java Remove the error handling part from quickstart sample as requested. --- .../com/example/pubsub/PublisherExample.java | 35 ++++++------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java b/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java index 454eeeeae7f..cb3f6350d2d 100644 --- a/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java +++ b/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java @@ -18,9 +18,7 @@ // [START pubsub_quickstart_publisher] import com.google.api.core.ApiFuture; -import com.google.api.core.ApiFutureCallback; import com.google.api.core.ApiFutures; -import com.google.api.gax.rpc.ApiException; import com.google.cloud.ServiceOptions; import com.google.cloud.pubsub.v1.Publisher; import com.google.protobuf.ByteString; @@ -41,6 +39,8 @@ public static void main(String... args) throws Exception { int messageCount = Integer.parseInt(args[1]); ProjectTopicName topicName = ProjectTopicName.of(PROJECT_ID, topicId); Publisher publisher = null; + List> futures = new ArrayList<>(); + try { // Create a publisher instance with default settings bound to the topic publisher = Publisher.newBuilder(topicName).build(); @@ -54,31 +54,18 @@ public static void main(String... args) throws Exception { .setData(data) .build(); - //schedule a message to be published, messages are automatically batched + // Schedule a message to be published. Messages are automatically batched. ApiFuture future = publisher.publish(pubsubMessage); - - // add an asynchronous callback to handle success / failure - ApiFutures.addCallback(future, new ApiFutureCallback() { - - @Override - public void onFailure(Throwable throwable) { - if (throwable instanceof ApiException) { - ApiException apiException = ((ApiException) throwable); - // details on the API exception - System.out.println(apiException.getStatusCode().getCode()); - System.out.println(apiException.isRetryable()); - } - System.out.println("Error publishing message : " + message); - } - - @Override - public void onSuccess(String messageId) { - // Once published, returns server-assigned message ids (unique within the topic) - System.out.println(messageId); - } - }); + futures.add(future); } } finally { + // Wait on any pending requests + List messageIds = ApiFutures.allAsList(futures).get(); + + for (String messageId : messageIds) { + System.out.println("messageId"); + } + if (publisher != null) { // When finished with the publisher, shutdown to free up resources. publisher.shutdown(); From e0f1d4ba1ab8c7a51fbb1d40d27025f36116c50f Mon Sep 17 00:00:00 2001 From: chenyumic Date: Tue, 8 May 2018 15:42:30 -0700 Subject: [PATCH 2/3] Minor Fix --- .../src/main/java/com/example/pubsub/PublisherExample.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java b/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java index cb3f6350d2d..0433316090f 100644 --- a/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java +++ b/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java @@ -25,6 +25,10 @@ import com.google.pubsub.v1.ProjectTopicName; import com.google.pubsub.v1.PubsubMessage; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + public class PublisherExample { // use the default project id From de40e53e7f17d885e77e3a59f5a8210ae08eab72 Mon Sep 17 00:00:00 2001 From: chenyumic Date: Tue, 8 May 2018 15:55:46 -0700 Subject: [PATCH 3/3] Minor Fix --- .../src/main/java/com/example/pubsub/PublisherExample.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java b/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java index 0433316090f..3969aad4ad2 100644 --- a/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java +++ b/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java @@ -67,7 +67,7 @@ public static void main(String... args) throws Exception { List messageIds = ApiFutures.allAsList(futures).get(); for (String messageId : messageIds) { - System.out.println("messageId"); + System.out.println(messageId); } if (publisher != null) {