From f1ac84a429f163688fcd32ed2179f708456fbabe Mon Sep 17 00:00:00 2001 From: chenyumic Date: Tue, 8 May 2018 15:00:56 -0700 Subject: [PATCH 1/7] Update CreateTopicAndPublishMessages.java Add the publisher error handler sample. As requested, the error handling part in the publishing quickstart sample is now removed. --- .../CreateTopicAndPublishMessages.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java index 5ffedde5a2d8..b6b8d98041d4 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java @@ -78,6 +78,54 @@ public static void publishMessages() throws Exception { // [END pubsub_publish] } + public static void publishMessagesWithErrorHandler() throws Exception { + // [START pubsub_publish_error_handler] + ProjectTopicName topicName = ProjectTopicName.of("my-project-id", "my-topic-id"); + Publisher publisher = null; + + try { + // Create a publisher instance with default settings bound to the topic + publisher = Publisher.newBuilder(topicName).build(); + + List messages = Arrays.asList("first message", "second message"); + + for (String message : messages) { + ByteString data = ByteString.copyFromUtf8(message); + PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); + + // Once published, returns a server-assigned message id (unique within the topic) + ApiFuture future = publisher.publish(pubsubMessage); + + // Add an asynchronous callback to handle success / failure + ApiFuture.addCallback(future, newApiFutureCallback()) { + + @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); + } + } + } + } finally { + if (publisher != null) { + // When finished with the publisher, shutdown to free up resources. + publisher.shutdown(); + } + } + // [END pubsub_publish_error_handler] + } + public static void main(String... args) throws Exception { createTopic(); publishMessages(); From 44849f3fcbfec21bc0c047fbea5b3fad6ee3f92a Mon Sep 17 00:00:00 2001 From: chenyumic Date: Tue, 8 May 2018 15:04:25 -0700 Subject: [PATCH 2/7] Minor Fix --- .../pubsub/snippets/CreateTopicAndPublishMessages.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java index b6b8d98041d4..3505ad878ff1 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java @@ -97,7 +97,7 @@ public static void publishMessagesWithErrorHandler() throws Exception { ApiFuture future = publisher.publish(pubsubMessage); // Add an asynchronous callback to handle success / failure - ApiFuture.addCallback(future, newApiFutureCallback()) { + ApiFuture.addCallback(future, new ApiFutureCallback() { @Override public void onFailure(Throwable throwable) { @@ -115,7 +115,7 @@ public void onSuccess(String messageId) { // Once published, returns server-assigned message ids (unique within the topic) System.out.println(messageId); } - } + }); } } finally { if (publisher != null) { From da3e2a7c79989501263b56d0036de76e9daca102 Mon Sep 17 00:00:00 2001 From: chenyumic Date: Tue, 8 May 2018 15:07:53 -0700 Subject: [PATCH 3/7] Minor Fix --- .../examples/pubsub/snippets/CreateTopicAndPublishMessages.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java index 3505ad878ff1..40259719dbca 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java @@ -17,7 +17,9 @@ package com.google.cloud.examples.pubsub.snippets; 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.pubsub.v1.Publisher; import com.google.cloud.pubsub.v1.TopicAdminClient; import com.google.protobuf.ByteString; From a3bce15c61acff0854fa88931837fc89dee98a40 Mon Sep 17 00:00:00 2001 From: chenyumic Date: Tue, 8 May 2018 15:14:43 -0700 Subject: [PATCH 4/7] Minor Fix --- .../examples/pubsub/snippets/CreateTopicAndPublishMessages.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java index 40259719dbca..38c142f2b8f9 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java @@ -99,7 +99,7 @@ public static void publishMessagesWithErrorHandler() throws Exception { ApiFuture future = publisher.publish(pubsubMessage); // Add an asynchronous callback to handle success / failure - ApiFuture.addCallback(future, new ApiFutureCallback() { + ApiFutures.addCallback(future, new ApiFutureCallback() { @Override public void onFailure(Throwable throwable) { From dd56e8735c06721cd1a5ad4a847f69c91a4b851c Mon Sep 17 00:00:00 2001 From: chenyumic Date: Tue, 8 May 2018 15:21:53 -0700 Subject: [PATCH 5/7] Minor Fix :sad --- .../examples/pubsub/snippets/CreateTopicAndPublishMessages.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java index 38c142f2b8f9..16b1dc7c14b6 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java @@ -91,7 +91,7 @@ public static void publishMessagesWithErrorHandler() throws Exception { List messages = Arrays.asList("first message", "second message"); - for (String message : messages) { + for (final String message : messages) { ByteString data = ByteString.copyFromUtf8(message); PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); From 9f624a00499b4bfa4438a63e65c5113ae43f6a5c Mon Sep 17 00:00:00 2001 From: chenyumic Date: Thu, 17 May 2018 10:01:25 -0700 Subject: [PATCH 6/7] Update CreateTopicAndPublishMessages.java --- .../snippets/CreateTopicAndPublishMessages.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java index 16b1dc7c14b6..c9d05b64f713 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java @@ -83,11 +83,8 @@ public static void publishMessages() throws Exception { public static void publishMessagesWithErrorHandler() throws Exception { // [START pubsub_publish_error_handler] ProjectTopicName topicName = ProjectTopicName.of("my-project-id", "my-topic-id"); - Publisher publisher = null; - try { - // Create a publisher instance with default settings bound to the topic - publisher = Publisher.newBuilder(topicName).build(); + try (Publisher publisher = Publisher.newBuilder(topicName).build()) { List messages = Arrays.asList("first message", "second message"); @@ -119,11 +116,6 @@ public void onSuccess(String messageId) { } }); } - } finally { - if (publisher != null) { - // When finished with the publisher, shutdown to free up resources. - publisher.shutdown(); - } } // [END pubsub_publish_error_handler] } @@ -131,5 +123,6 @@ public void onSuccess(String messageId) { public static void main(String... args) throws Exception { createTopic(); publishMessages(); + publishMessagesWithErrorHandler(); } } From 244423736b94700c9bc54fc63ec4f5dda65fb625 Mon Sep 17 00:00:00 2001 From: michaelawyu Date: Thu, 17 May 2018 10:10:51 -0700 Subject: [PATCH 7/7] Revert "Update CreateTopicAndPublishMessages.java" This reverts commit 9f624a00499b4bfa4438a63e65c5113ae43f6a5c. --- .../snippets/CreateTopicAndPublishMessages.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java index c9d05b64f713..16b1dc7c14b6 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java @@ -83,8 +83,11 @@ public static void publishMessages() throws Exception { public static void publishMessagesWithErrorHandler() throws Exception { // [START pubsub_publish_error_handler] ProjectTopicName topicName = ProjectTopicName.of("my-project-id", "my-topic-id"); + Publisher publisher = null; - try (Publisher publisher = Publisher.newBuilder(topicName).build()) { + try { + // Create a publisher instance with default settings bound to the topic + publisher = Publisher.newBuilder(topicName).build(); List messages = Arrays.asList("first message", "second message"); @@ -116,6 +119,11 @@ public void onSuccess(String messageId) { } }); } + } finally { + if (publisher != null) { + // When finished with the publisher, shutdown to free up resources. + publisher.shutdown(); + } } // [END pubsub_publish_error_handler] } @@ -123,6 +131,5 @@ public void onSuccess(String messageId) { public static void main(String... args) throws Exception { createTopic(); publishMessages(); - publishMessagesWithErrorHandler(); } }