Class Subscriber
- java.lang.Object
-
- com.google.api.core.AbstractApiService
-
- com.google.cloud.pubsub.v1.Subscriber
-
- All Implemented Interfaces:
com.google.api.core.ApiService
,SubscriberInterface
public class Subscriber extends com.google.api.core.AbstractApiService implements SubscriberInterface
A Cloud Pub/Sub subscriber that is associated with a specific subscription at creation.A
Subscriber
allows you to provide an implementation of areceiver
to which messages are going to be delivered as soon as they are received by the subscriber. The delivered messages then can beacked
ornacked
at will as they get processed by the receiver. Nacking a messages implies a later redelivery of such message.The subscriber handles the ack management, by automatically extending the ack deadline while the message is being processed, to then issue the ack or nack of such message when the processing is done (see
Subscriber.Builder.setMaxAckExtensionPeriod(Duration)
). Note: message redelivery is still possible.It also provides customizable options that control:
- Ack deadline extension: such as the amount of time ahead to trigger the extension of message acknowledgement expiration.
- Flow control: such as the maximum outstanding messages or maximum outstanding bytes to keep in memory before the receiver either ack or nack them.
Subscriber
will use the credentials set on the channel, which uses application default credentials throughGoogleCredentials.getApplicationDefault()
by default.Subscriber
is implemented using Guava's Service and provides the same methods. See Guava documentation for more details.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Subscriber.Builder
Builder ofSubscribers
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
doStart()
protected void
doStop()
static Integer
getDeliveryAttempt(PubsubMessage message)
Returns the delivery attempt count for a receivedPubsubMessage
com.google.api.gax.batching.FlowControlSettings
getFlowControlSettings()
The flow control settings the Subscriber is configured with.String
getSubscriptionNameString()
Subscription which the subscriber is subscribed to.static Subscriber.Builder
newBuilder(ProjectSubscriptionName subscription, MessageReceiver receiver)
Constructs a newSubscriber.Builder
.static Subscriber.Builder
newBuilder(ProjectSubscriptionName subscription, MessageReceiverWithAckResponse receiver)
static Subscriber.Builder
newBuilder(String subscription, MessageReceiver receiver)
Constructs a newSubscriber.Builder
.static Subscriber.Builder
newBuilder(String subscription, MessageReceiverWithAckResponse receiver)
com.google.api.core.ApiService
startAsync()
Initiates service startup and returns immediately.-
Methods inherited from class com.google.api.core.AbstractApiService
addListener, awaitRunning, awaitRunning, awaitTerminated, awaitTerminated, failureCause, isRunning, notifyFailed, notifyStarted, notifyStopped, state, stopAsync
-
-
-
-
Method Detail
-
newBuilder
public static Subscriber.Builder newBuilder(ProjectSubscriptionName subscription, MessageReceiver receiver)
Constructs a newSubscriber.Builder
.- Parameters:
subscription
- Cloud Pub/Sub subscription to bind the subscriber toreceiver
- an implementation ofMessageReceiver
used to process the received messages
-
newBuilder
public static Subscriber.Builder newBuilder(ProjectSubscriptionName subscription, MessageReceiverWithAckResponse receiver)
-
newBuilder
public static Subscriber.Builder newBuilder(String subscription, MessageReceiver receiver)
Constructs a newSubscriber.Builder
.- Parameters:
subscription
- Cloud Pub/Sub subscription to bind the subscriber toreceiver
- an implementation ofMessageReceiver
used to process the received messages
-
newBuilder
public static Subscriber.Builder newBuilder(String subscription, MessageReceiverWithAckResponse receiver)
-
getDeliveryAttempt
public static Integer getDeliveryAttempt(PubsubMessage message)
Returns the delivery attempt count for a receivedPubsubMessage
-
getSubscriptionNameString
public String getSubscriptionNameString()
Subscription which the subscriber is subscribed to.
-
getFlowControlSettings
public com.google.api.gax.batching.FlowControlSettings getFlowControlSettings()
The flow control settings the Subscriber is configured with.
-
startAsync
public com.google.api.core.ApiService startAsync()
Initiates service startup and returns immediately.Example of receiving a specific number of messages.
Subscriber subscriber = Subscriber.newBuilder(subscription, receiver).build(); subscriber.addListener(new Subscriber.Listener() { public void failed(Subscriber.State from, Throwable failure) { // Handle error. } }, executor); subscriber.startAsync(); // Wait for a stop signal. // In a server, this might be a signal to stop serving. // In this example, the signal is just a dummy Future. // // By default, Subscriber uses daemon threads (see // https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html). // Consequently, once other threads have terminated, Subscriber will not stop the JVM from // exiting. // If the Subscriber should simply run forever, either use the setExecutorProvider method in // Subscriber.Builder // to use non-daemon threads or run // for (;;) { // Thread.sleep(Long.MAX_VALUE); // } // at the end of main() to previent the main thread from exiting. done.get(); subscriber.stopAsync().awaitTerminated();
- Specified by:
startAsync
in interfacecom.google.api.core.ApiService
- Overrides:
startAsync
in classcom.google.api.core.AbstractApiService
-
doStart
protected void doStart()
- Specified by:
doStart
in classcom.google.api.core.AbstractApiService
-
doStop
protected void doStop()
- Specified by:
doStop
in classcom.google.api.core.AbstractApiService
-
-