Class Publisher
- java.lang.Object
-
- com.google.cloud.pubsub.v1.Publisher
-
- All Implemented Interfaces:
PublisherInterface
public class Publisher extends Object implements PublisherInterface
A Cloud Pub/Sub publisher, that is associated with a specific topic at creation.A
Publisherprovides built-in capabilities to automatically handle batching of messages, controlling memory utilization, and retrying API calls on transient errors.With customizable options that control:
- Message batching: such as number of messages or max batch byte size.
- Retries: such as the maximum duration of retries for a failing batch of messages.
Publisherwill use the credentials set on the channel, which uses application default credentials throughGoogleCredentials.getApplicationDefault()by default.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPublisher.BuilderA builder ofPublishers.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanawaitTermination(long duration, TimeUnit unit)Wait for all work has completed execution after ashutdown()request, or the timeout occurs, or the current thread is interrupted.static longgetApiMaxRequestBytes()The maximum size of one request.static longgetApiMaxRequestElementCount()The maximum number of messages in one request.com.google.api.gax.batching.BatchingSettingsgetBatchingSettings()The batching settings configured on thisPublisher.TopicNamegetTopicName()Topic which the publisher publishes to.StringgetTopicNameString()Topic which the publisher publishes to.static Publisher.BuildernewBuilder(TopicName topicName)Constructs a newPublisher.Builderusing the given topic.static Publisher.BuildernewBuilder(String topicName)Constructs a newPublisher.Builderusing the given topic.com.google.api.core.ApiFuture<String>publish(PubsubMessage message)Schedules the publishing of a message.voidpublishAllOutstanding()Publish any outstanding batches if non-empty.voidresumePublish(String key)There may be non-recoverable problems with a request for an ordering key.voidshutdown()Schedules immediate publishing of any outstanding messages and waits until all are processed.
-
-
-
Method Detail
-
getApiMaxRequestElementCount
public static long getApiMaxRequestElementCount()
The maximum number of messages in one request. Defined by the API.
-
getApiMaxRequestBytes
public static long getApiMaxRequestBytes()
The maximum size of one request. Defined by the API.
-
getTopicName
public TopicName getTopicName()
Topic which the publisher publishes to.
-
getTopicNameString
public String getTopicNameString()
Topic which the publisher publishes to.
-
publish
public com.google.api.core.ApiFuture<String> publish(PubsubMessage message)
Schedules the publishing of a message. The publishing of the message may occur immediately or be delayed based on the publisher batching options.This method blocks in the downcall if using LimitExceededBehavior.Block in the flow control settings.
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());- Specified by:
publishin interfacePublisherInterface- Parameters:
message- the message to publish.- Returns:
- the message ID wrapped in a future.
-
resumePublish
public void resumePublish(String key)
There may be non-recoverable problems with a request for an ordering key. In that case, all subsequent requests will fail until this method is called. If the key is not currently paused, calling this method will be a no-op.- Parameters:
key- The key for which to resume publishing.
-
publishAllOutstanding
public void publishAllOutstanding()
Publish any outstanding batches if non-empty. This method sends buffered messages, but does not wait for the send operations to complete. To wait for messages to send, callgeton the futures returned frompublish.
-
getBatchingSettings
public com.google.api.gax.batching.BatchingSettings getBatchingSettings()
The batching settings configured on thisPublisher.
-
shutdown
public void shutdown()
Schedules immediate publishing of any outstanding messages and waits until all are processed.Sends remaining outstanding messages and prevents future calls to publish. This method should be invoked prior to deleting the
Publisherobject in order to ensure that no pending messages are lost.
-
awaitTermination
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedExceptionWait for all work has completed execution after ashutdown()request, or the timeout occurs, or the current thread is interrupted.Call this method to make sure all resources are freed properly.
- Throws:
InterruptedException
-
newBuilder
public static Publisher.Builder newBuilder(TopicName topicName)
Constructs a newPublisher.Builderusing the given topic.Example of creating a
Publisher.String projectName = "my_project"; String topicName = "my_topic"; ProjectTopicName topic = ProjectTopicName.create(projectName, topicName); Publisher publisher = Publisher.newBuilder(topic).build(); try { // ... } finally { // When finished with the publisher, make sure to shutdown to free up resources. publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); }
-
newBuilder
public static Publisher.Builder newBuilder(String topicName)
Constructs a newPublisher.Builderusing the given topic.Example of creating a
Publisher.String topic = "projects/my_project/topics/my_topic"; Publisher publisher = Publisher.newBuilder(topic).build(); try { // ... } finally { // When finished with the publisher, make sure to shutdown to free up resources. publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); }
-
-