A Cloud Pub/Sub publisher, that is
associated with a specific topic at creation.
A Publisher provides 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.
Publisher will use the credentials set on the channel, which uses application default
credentials through GoogleCredentials#getApplicationDefault by default.
StringprojectName="my_project";StringtopicName="my_topic";ProjectTopicNametopic=ProjectTopicName.create(projectName,topicName);Publisherpublisher=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);}
Stringtopic="projects/my_project/topics/my_topic";Publisherpublisher=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);}
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.
Stringmessage="my_message";ByteStringdata=ByteString.copyFromUtf8(message);PubsubMessagepubsubMessage=PubsubMessage.newBuilder().setData(data).build();ApiFuture<String>messageIdFuture=publisher.publish(pubsubMessage);ApiFutures.addCallback(messageIdFuture,newApiFutureCallback<String>(){publicvoidonSuccess(StringmessageId){System.out.println("published with message id: "+messageId);}publicvoidonFailure(Throwablet){System.out.println("failed to publish: "+t);}},MoreExecutors.directExecutor());
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, call get on the
futures returned from publish.
resumePublish(String key)
publicvoidresumePublish(Stringkey)
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.
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 Publisher object in order to ensure that no
pending messages are lost.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Class Publisher (1.141.2)\n\nVersion latestkeyboard_arrow_down\n\n- [1.141.2 (latest)](/java/docs/reference/google-cloud-pubsub/latest/com.google.cloud.pubsub.v1.Publisher)\n- [1.141.1](/java/docs/reference/google-cloud-pubsub/1.141.1/com.google.cloud.pubsub.v1.Publisher)\n- [1.140.2](/java/docs/reference/google-cloud-pubsub/1.140.2/com.google.cloud.pubsub.v1.Publisher)\n- [1.139.4](/java/docs/reference/google-cloud-pubsub/1.139.4/com.google.cloud.pubsub.v1.Publisher)\n- [1.138.0](/java/docs/reference/google-cloud-pubsub/1.138.0/com.google.cloud.pubsub.v1.Publisher)\n- [1.137.1](/java/docs/reference/google-cloud-pubsub/1.137.1/com.google.cloud.pubsub.v1.Publisher)\n- [1.136.1](/java/docs/reference/google-cloud-pubsub/1.136.1/com.google.cloud.pubsub.v1.Publisher)\n- [1.135.0](/java/docs/reference/google-cloud-pubsub/1.135.0/com.google.cloud.pubsub.v1.Publisher)\n- [1.134.2](/java/docs/reference/google-cloud-pubsub/1.134.2/com.google.cloud.pubsub.v1.Publisher)\n- [1.133.1](/java/docs/reference/google-cloud-pubsub/1.133.1/com.google.cloud.pubsub.v1.Publisher)\n- [1.132.2](/java/docs/reference/google-cloud-pubsub/1.132.2/com.google.cloud.pubsub.v1.Publisher)\n- [1.131.0](/java/docs/reference/google-cloud-pubsub/1.131.0/com.google.cloud.pubsub.v1.Publisher)\n- [1.130.0](/java/docs/reference/google-cloud-pubsub/1.130.0/com.google.cloud.pubsub.v1.Publisher)\n- [1.129.6](/java/docs/reference/google-cloud-pubsub/1.129.6/com.google.cloud.pubsub.v1.Publisher)\n- [1.127.3](/java/docs/reference/google-cloud-pubsub/1.127.3/com.google.cloud.pubsub.v1.Publisher)\n- [1.126.6](/java/docs/reference/google-cloud-pubsub/1.126.6/com.google.cloud.pubsub.v1.Publisher)\n- [1.125.13](/java/docs/reference/google-cloud-pubsub/1.125.13/com.google.cloud.pubsub.v1.Publisher)\n- [1.123.18](/java/docs/reference/google-cloud-pubsub/1.123.18/com.google.cloud.pubsub.v1.Publisher)\n- [1.122.2](/java/docs/reference/google-cloud-pubsub/1.122.2/com.google.cloud.pubsub.v1.Publisher)\n- [1.121.1](/java/docs/reference/google-cloud-pubsub/1.121.1/com.google.cloud.pubsub.v1.Publisher)\n- [1.120.24](/java/docs/reference/google-cloud-pubsub/1.120.24/com.google.cloud.pubsub.v1.Publisher)\n- [1.119.1](/java/docs/reference/google-cloud-pubsub/1.119.1/com.google.cloud.pubsub.v1.Publisher)\n- [1.118.0](/java/docs/reference/google-cloud-pubsub/1.118.0/com.google.cloud.pubsub.v1.Publisher)\n- [1.117.0](/java/docs/reference/google-cloud-pubsub/1.117.0/com.google.cloud.pubsub.v1.Publisher)\n- [1.116.4](/java/docs/reference/google-cloud-pubsub/1.116.4/com.google.cloud.pubsub.v1.Publisher)\n- [1.115.5](/java/docs/reference/google-cloud-pubsub/1.115.5/com.google.cloud.pubsub.v1.Publisher) \n\n public class Publisher implements PublisherInterface\n\nA Cloud Pub/Sub [publisher](https://cloud.google.com/pubsub/docs/publisher), that is\nassociated with a specific topic at creation.\n\nA [Publisher](/java/docs/reference/google-cloud-pubsub/latest/com.google.cloud.pubsub.v1.Publisher) provides built-in capabilities to automatically handle batching of\nmessages, controlling memory utilization, and retrying API calls on transient errors.\n\nWith customizable options that control:\n\n- Message batching: such as number of messages or max batch byte size.\n- Retries: such as the maximum duration of retries for a failing batch of messages.\n\n[Publisher](/java/docs/reference/google-cloud-pubsub/latest/com.google.cloud.pubsub.v1.Publisher) will use the credentials set on the channel, which uses application default\ncredentials through GoogleCredentials#getApplicationDefault by default. \n\nInheritance\n-----------\n\n[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \\\u003e Publisher \n\nImplements\n----------\n\n[PublisherInterface](/java/docs/reference/google-cloud-pubsub/latest/com.google.cloud.pubsub.v1.PublisherInterface) \n\nInherited Members\n-----------------\n\n[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) \n[Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) \n[Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) \n[Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) \n[Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) \n[Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) \n[Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) \n[Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) \n[Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) \n[Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) \n[Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)\n\nStatic Methods\n--------------\n\n### getApiMaxRequestBytes()\n\n public static long getApiMaxRequestBytes()\n\nThe maximum size of one request. Defined by the API.\n\n### getApiMaxRequestElementCount()\n\n public static long getApiMaxRequestElementCount()\n\nThe maximum number of messages in one request. Defined by the API.\n\n### newBuilder(TopicName topicName)\n\n public static Publisher.Builder newBuilder(TopicName topicName)\n\nConstructs a new Builder using the given topic.\n\nExample of creating a `Publisher`. \n\n\n String projectName = \"my_project\";\n String topicName = \"my_topic\";\n ProjectTopicName topic = ProjectTopicName.create(projectName, topicName);\n Publisher publisher = Publisher.newBuilder(topic).build();\n try {\n // ...\n } finally {\n // When finished with the publisher, make sure to shutdown to free up resources.\n publisher.shutdown();\n publisher.awaitTermination(1, TimeUnit.MINUTES);\n }\n \n### newBuilder(String topicName)\n\n public static Publisher.Builder newBuilder(String topicName)\n\nConstructs a new Builder using the given topic.\n\nExample of creating a `Publisher`. \n\n\n String topic = \"projects/my_project/topics/my_topic\";\n Publisher publisher = Publisher.newBuilder(topic).build();\n try {\n // ...\n } finally {\n // When finished with the publisher, make sure to shutdown to free up resources.\n publisher.shutdown();\n publisher.awaitTermination(1, TimeUnit.MINUTES);\n }\n \nMethods\n-------\n\n### awaitTermination(long duration, TimeUnit unit)\n\n public boolean awaitTermination(long duration, TimeUnit unit)\n\nWait for all work has completed execution after a [#shutdown()](/java/docs/reference/google-cloud-pubsub/latest/com.google.cloud.pubsub.v1.Publisher#com_google_cloud_pubsub_v1_Publisher_shutdown__) request, or the timeout\noccurs, or the current thread is interrupted.\n\nCall this method to make sure all resources are freed properly.\n\n### getBatchingSettings()\n\n public BatchingSettings getBatchingSettings()\n\nThe batching settings configured on this `Publisher`.\n\n### getTopicName()\n\n public TopicName getTopicName()\n\nTopic which the publisher publishes to.\n\n### getTopicNameString()\n\n public String getTopicNameString()\n\nTopic which the publisher publishes to.\n\n### publish(PubsubMessage message)\n\n public ApiFuture\u003cString\u003e publish(PubsubMessage message)\n\nSchedules the publishing of a message. The publishing of the message may occur immediately or\nbe delayed based on the publisher batching options.\n\nThis method blocks in the downcall if using LimitExceededBehavior.Block in the flow control\nsettings.\n\nExample of publishing a message. \n\n\n String message = \"my_message\";\n ByteString data = ByteString.copyFromUtf8(message);\n PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();\n ApiFuture\u003cString\u003e messageIdFuture = publisher.publish(pubsubMessage);\n ApiFutures.addCallback(messageIdFuture, new ApiFutureCallback\u003cString\u003e() {\n public void onSuccess(String messageId) {\n System.out.println(\"published with message id: \" + messageId);\n }\n\n public void onFailure(Throwable t) {\n System.out.println(\"failed to publish: \" + t);\n }\n }, MoreExecutors.directExecutor());\n \n### publishAllOutstanding()\n\n public void publishAllOutstanding()\n\nPublish any outstanding batches if non-empty. This method sends buffered messages, but does not\nwait for the send operations to complete. To wait for messages to send, call `get` on the\nfutures returned from `publish`.\n\n### resumePublish(String key)\n\n public void resumePublish(String key)\n\nThere may be non-recoverable problems with a request for an ordering key. In that case, all\nsubsequent requests will fail until this method is called. If the key is not currently paused,\ncalling this method will be a no-op.\n\n### shutdown()\n\n public void shutdown()\n\nSchedules immediate publishing of any outstanding messages and waits until all are processed.\n\nSends remaining outstanding messages and prevents future calls to publish. This method\nshould be invoked prior to deleting the [Publisher](/java/docs/reference/google-cloud-pubsub/latest/com.google.cloud.pubsub.v1.Publisher) object in order to ensure that no\npending messages are lost."]]