@@ -122,7 +122,10 @@ With Pub/Sub you can publish messages to a topic. Add the following import at th
122
122
123
123
``` java
124
124
import com.google.api.core.ApiFuture ;
125
+ import com.google.api.core.ApiFutureCallback ;
126
+ import com.google.api.core.ApiFutures ;
125
127
import com.google.cloud.pubsub.v1.Publisher ;
128
+ import com.google.common.util.concurrent.MoreExecutors ;
126
129
import com.google.protobuf.ByteString ;
127
130
import com.google.pubsub.v1.PubsubMessage ;
128
131
```
@@ -135,6 +138,16 @@ try {
135
138
ByteString data = ByteString . copyFromUtf8(" my-message" );
136
139
PubsubMessage pubsubMessage = PubsubMessage . newBuilder(). setData(data). build();
137
140
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
+ // ...
138
151
} finally {
139
152
if (publisher != null ) {
140
153
publisher. shutdown();
@@ -284,4 +297,4 @@ Java 11 | [![Kokoro CI][kokoro-badge-image-5]][kokoro-badge-link-5]
284
297
[ troubleshooting ] : https://github.com/googleapis/google-cloud-common/blob/master/troubleshooting/readme.md#troubleshooting
285
298
[ contributing ] : https://github.com/googleapis/java-pubsub/blob/master/CONTRIBUTING.md
286
299
[ 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