I'm using the google cloud PubSub Java client libaries and have a basic question about publishing guarantees. In the code below, if the publish returns successfully, i.e., the code gets to the // do other processing
part, is the message guaranteed to be safely on Google's servers? Do I need to check the returned messageId?
BTW, I know about the ApiFutures.addCallback to add a success/failure callback, but I'd rather not do that.
Publisher myPublisher = ...
PubsubMessage myMessage = ...
try {
String messageId = myPublisher.publish(myMessage).get();
// do other processing
} catch(Exception e) {
// do error handling
}
Once .get()
on the future returns, it is guaranteed that Cloud Pub/Sub has received the message and will deliver it to interested subscribers. Note that if you are going to use .get()
immediately like this and you aren't going to share a publisher instance across threads, you should set the max messages in the batching settings to 1. Otherwise, you will have an extra delay in sending the message to Pub/Sub as the client will attempt to fill the batch for up to the max latency setting amount of time. If you aren't making multiple calls in parallel, nothing will ever get batched and so the time waiting will have been wasted.