Skip to content

Commit 03e1869

Browse files
chenyumicpongad
authored andcommitted
Update CreateTopicAndPublishMessages.java (#3249)
Add the publisher error handler sample. As requested, the error handling part in the publishing quickstart sample is now removed.
1 parent 70adcab commit 03e1869

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package com.google.cloud.examples.pubsub.snippets;
1818

1919
import com.google.api.core.ApiFuture;
20+
import com.google.api.core.ApiFutureCallback;
2021
import com.google.api.core.ApiFutures;
22+
import com.google.api.gax.rpc.ApiException;
2123
import com.google.cloud.pubsub.v1.Publisher;
2224
import com.google.cloud.pubsub.v1.TopicAdminClient;
2325
import com.google.protobuf.ByteString;
@@ -78,6 +80,54 @@ public static void publishMessages() throws Exception {
7880
// [END pubsub_publish]
7981
}
8082

83+
public static void publishMessagesWithErrorHandler() throws Exception {
84+
// [START pubsub_publish_error_handler]
85+
ProjectTopicName topicName = ProjectTopicName.of("my-project-id", "my-topic-id");
86+
Publisher publisher = null;
87+
88+
try {
89+
// Create a publisher instance with default settings bound to the topic
90+
publisher = Publisher.newBuilder(topicName).build();
91+
92+
List<String> messages = Arrays.asList("first message", "second message");
93+
94+
for (final String message : messages) {
95+
ByteString data = ByteString.copyFromUtf8(message);
96+
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
97+
98+
// Once published, returns a server-assigned message id (unique within the topic)
99+
ApiFuture<String> future = publisher.publish(pubsubMessage);
100+
101+
// Add an asynchronous callback to handle success / failure
102+
ApiFutures.addCallback(future, new ApiFutureCallback<String>() {
103+
104+
@Override
105+
public void onFailure(Throwable throwable) {
106+
if (throwable instanceof ApiException) {
107+
ApiException apiException = ((ApiException) throwable);
108+
// details on the API exception
109+
System.out.println(apiException.getStatusCode().getCode());
110+
System.out.println(apiException.isRetryable());
111+
}
112+
System.out.println("Error publishing message : " + message);
113+
}
114+
115+
@Override
116+
public void onSuccess(String messageId) {
117+
// Once published, returns server-assigned message ids (unique within the topic)
118+
System.out.println(messageId);
119+
}
120+
});
121+
}
122+
} finally {
123+
if (publisher != null) {
124+
// When finished with the publisher, shutdown to free up resources.
125+
publisher.shutdown();
126+
}
127+
}
128+
// [END pubsub_publish_error_handler]
129+
}
130+
81131
public static void main(String... args) throws Exception {
82132
createTopic();
83133
publishMessages();

0 commit comments

Comments
 (0)