Skip to content

Commit

Permalink
fix: Modifying Publish example in README to match other examples, and
Browse files Browse the repository at this point in the history
fix Issue googleapis#11
  • Loading branch information
hannahrogers-google committed Nov 25, 2019
1 parent 53c4030 commit 4158529
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion README.md
Expand Up @@ -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;
```
Expand All @@ -135,6 +138,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 Expand Up @@ -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
[license]: https://github.com/googleapis/java-pubsub/blob/master/LICENSE

0 comments on commit 4158529

Please sign in to comment.