Interface PublisherInterface (1.125.10)

public interface PublisherInterface

An interface for a Cloud Pub/Sub publisher.

Methods

publish(PubsubMessage message)

public abstract ApiFuture<String> publish(PubsubMessage message)

Schedules the publishing of a message. The future will be returned with the message ID on success or an exception on failure.

Some implementations of this method may block in the downcall until allowed by flow control.

Example of publishing a message.


 String message = "my_message";
 ByteString data = ByteString.copyFromUtf8(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());
 
Parameter
NameDescription
messagePubsubMessage

the message to publish.

Returns
TypeDescription
ApiFuture<String>

the message ID wrapped in a future.