Skip to content

Commit 4a9c410

Browse files
Modifying Publish example in README to match other examples given, and (#10)
fix issue #11
1 parent 53c4030 commit 4a9c410

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ With Pub/Sub you can publish messages to a topic. Add the following import at th
122122

123123
```java
124124
import com.google.api.core.ApiFuture;
125+
import com.google.api.core.ApiFutureCallback;
126+
import com.google.api.core.ApiFutures;
125127
import com.google.cloud.pubsub.v1.Publisher;
128+
import com.google.common.util.concurrent.MoreExecutors;
126129
import com.google.protobuf.ByteString;
127130
import com.google.pubsub.v1.PubsubMessage;
128131
```
@@ -135,6 +138,16 @@ try {
135138
ByteString data = ByteString.copyFromUtf8("my-message");
136139
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
137140
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
141+
ApiFutures.addCallback(messageIdFuture, new ApiFutureCallback<String>() {
142+
public void onSuccess(String messageId) {
143+
System.out.println("published with message id: " + messageId);
144+
}
145+
146+
public void onFailure(Throwable t) {
147+
System.out.println("failed to publish: " + t);
148+
}
149+
}, MoreExecutors.directExecutor());
150+
//...
138151
} finally {
139152
if (publisher != null) {
140153
publisher.shutdown();
@@ -284,4 +297,4 @@ Java 11 | [![Kokoro CI][kokoro-badge-image-5]][kokoro-badge-link-5]
284297
[troubleshooting]: https://github.com/googleapis/google-cloud-common/blob/master/troubleshooting/readme.md#troubleshooting
285298
[contributing]: https://github.com/googleapis/java-pubsub/blob/master/CONTRIBUTING.md
286299
[code-of-conduct]: https://github.com/googleapis/java-pubsub/blob/master/CODE_OF_CONDUCT.md#contributor-code-of-conduct
287-
[license]: https://github.com/googleapis/java-pubsub/blob/master/LICENSE
300+
[license]: https://github.com/googleapis/java-pubsub/blob/master/LICENSE

0 commit comments

Comments
 (0)