Skip to content

Commit

Permalink
Updating README file to fix Issue #6784: Pubsub: Uncaught exception i…
Browse files Browse the repository at this point in the history
…n the SynchronizationContext When using example code from the Getting Started Guide
  • Loading branch information
hannahrogers-google committed Nov 22, 2019
1 parent ac6cd0b commit 2aece0f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions google-cloud-clients/google-cloud-pubsub/README.md
Expand Up @@ -119,7 +119,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;
```
Expand All @@ -132,6 +135,16 @@ try {
ByteString data = ByteString.copyFromUtf8("my-message");
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
ApiFutures.addCallback(messageIdFuture, new ApiFutureCallback<String>() {
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();
Expand Down

0 comments on commit 2aece0f

Please sign in to comment.