public class Subscriber
extends com.google.api.core.AbstractApiService
A Subscriber allows you to provide an implementation of a receiver to which messages are going to be delivered as soon as they are received by the
subscriber. The delivered messages then can be acked or nacked 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. Note: message redelivery is still possible.
It also provides customizable options that control:
Subscriber will use the credentials set on the channel, which uses application default
credentials through GoogleCredentials.getApplicationDefault() by default.
Subscriber is implemented using Guava's
Service and provides the same methods. See Guava documentation for more
details.
| Modifier and Type | Class and Description |
|---|---|
static class |
Subscriber.Builder
Builder of
Subscribers. |
| Modifier and Type | Method and Description |
|---|---|
static Subscriber.Builder |
defaultBuilder(com.google.pubsub.v1.SubscriptionName subscription,
MessageReceiver receiver)
Deprecated.
Use
newBuilder(SubscriptionName, MessageReceiver) instead. |
protected void |
doStart() |
protected void |
doStop() |
com.google.api.gax.batching.FlowControlSettings |
getFlowControlSettings()
The flow control settings the Subscriber is configured with.
|
com.google.pubsub.v1.SubscriptionName |
getSubscriptionName()
Subscription which the subscriber is subscribed to.
|
static Subscriber.Builder |
newBuilder(com.google.pubsub.v1.SubscriptionName subscription,
MessageReceiver receiver)
Constructs a new
Subscriber.Builder. |
com.google.api.core.ApiService |
startAsync()
Initiates service startup and returns immediately.
|
@Deprecated public static Subscriber.Builder defaultBuilder(com.google.pubsub.v1.SubscriptionName subscription, MessageReceiver receiver)
newBuilder(SubscriptionName, MessageReceiver) instead.Subscriber.Builder.
Once Subscriber.Builder.build() is called a gRPC stub will be created for use of the Subscriber.
subscription - Cloud Pub/Sub subscription to bind the subscriber toreceiver - an implementation of MessageReceiver used to process the received
messagespublic static Subscriber.Builder newBuilder(com.google.pubsub.v1.SubscriptionName subscription, MessageReceiver receiver)
Subscriber.Builder.
Once Subscriber.Builder.build() is called a gRPC stub will be created for use of the Subscriber.
subscription - Cloud Pub/Sub subscription to bind the subscriber toreceiver - an implementation of MessageReceiver used to process the received
messagespublic com.google.pubsub.v1.SubscriptionName getSubscriptionName()
public com.google.api.gax.batching.FlowControlSettings getFlowControlSettings()
public com.google.api.core.ApiService startAsync()
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.
done.get();
subscriber.stopAsync().awaitTerminated();
startAsync in interface com.google.api.core.ApiServicestartAsync in class com.google.api.core.AbstractApiServiceprotected void doStart()
doStart in class com.google.api.core.AbstractApiServiceprotected void doStop()
doStop in class com.google.api.core.AbstractApiServiceCopyright © 2017 Google. All rights reserved.