diff --git a/README.md b/README.md index 3b928096b..ca3e1c833 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,10 @@ With Pub/Sub you can publish messages to a topic. Add the following import at th ```java import com.google.api.core.ApiFuture; +import com.google.api.core.ApiFutureCallback; +import com.google.api.core.ApiFutures; import com.google.cloud.pubsub.v1.Publisher; +import com.google.common.util.concurrent.MoreExecutors; import com.google.protobuf.ByteString; import com.google.pubsub.v1.PubsubMessage; ``` @@ -135,6 +138,16 @@ try { ByteString data = ByteString.copyFromUtf8("my-message"); PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); ApiFuture messageIdFuture = publisher.publish(pubsubMessage); + ApiFutures.addCallback(messageIdFuture, new ApiFutureCallback() { + public void onSuccess(String messageId) { + System.out.println("published with message id: " + messageId); + } + + public void onFailure(Throwable t) { + System.out.println("failed to publish: " + t); + } + }, MoreExecutors.directExecutor()); + //... } finally { if (publisher != null) { publisher.shutdown(); @@ -284,4 +297,4 @@ Java 11 | [![Kokoro CI][kokoro-badge-image-5]][kokoro-badge-link-5] [troubleshooting]: https://github.com/googleapis/google-cloud-common/blob/master/troubleshooting/readme.md#troubleshooting [contributing]: https://github.com/googleapis/java-pubsub/blob/master/CONTRIBUTING.md [code-of-conduct]: https://github.com/googleapis/java-pubsub/blob/master/CODE_OF_CONDUCT.md#contributor-code-of-conduct -[license]: https://github.com/googleapis/java-pubsub/blob/master/LICENSE \ No newline at end of file +[license]: https://github.com/googleapis/java-pubsub/blob/master/LICENSE