public class Publisher extends Object
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:
Publisher
will use the credentials set on the channel, which uses application default
credentials through GoogleCredentials.getApplicationDefault()
by default.
Modifier and Type | Class and Description |
---|---|
static class |
Publisher.Builder
A builder of
Publisher s. |
Modifier and Type | Method and Description |
---|---|
boolean |
awaitTermination(long duration,
TimeUnit unit)
Wait for all work has completed execution after a
shutdown() request, or the timeout
occurs, or the current thread is interrupted. |
static long |
getApiMaxRequestBytes()
The maximum size of one request.
|
static long |
getApiMaxRequestElementCount()
The maximum number of messages in one request.
|
BatchingSettings |
getBatchingSettings()
The batching settings configured on this
Publisher . |
com.google.pubsub.v1.TopicName |
getTopicName()
Topic which the publisher publishes to.
|
String |
getTopicNameString()
Topic which the publisher publishes to.
|
static Publisher.Builder |
newBuilder(String topicName)
Constructs a new
Publisher.Builder using the given topic. |
static Publisher.Builder |
newBuilder(com.google.pubsub.v1.TopicName topicName)
Constructs a new
Publisher.Builder using the given topic. |
com.google.api.core.ApiFuture<String> |
publish(com.google.pubsub.v1.PubsubMessage message)
Schedules the publishing of a message.
|
void |
publishAllOutstanding()
Publish any outstanding batches if non-empty.
|
void |
shutdown()
Schedules immediate publishing of any outstanding messages and waits until all are processed.
|
public static long getApiMaxRequestElementCount()
public static long getApiMaxRequestBytes()
public com.google.pubsub.v1.TopicName getTopicName()
public String getTopicNameString()
public com.google.api.core.ApiFuture<String> publish(com.google.pubsub.v1.PubsubMessage message)
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);
}
});
message
- the message to publish.public void publishAllOutstanding()
get
on the
futures returned from publish
.public BatchingSettings getBatchingSettings()
Publisher
.public void shutdown()
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.
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException
shutdown()
request, or the timeout
occurs, or the current thread is interrupted.
Call this method to make sure all resources are freed properly.
InterruptedException
public static Publisher.Builder newBuilder(com.google.pubsub.v1.TopicName topicName)
Publisher.Builder
using 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);
}
public static Publisher.Builder newBuilder(String topicName)
Publisher.Builder
using 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);
}
Copyright © 2019 Google LLC. All rights reserved.