@Generated(value="by GAPIC")
public class SubscriberApi
extends java.lang.Object
implements java.lang.AutoCloseable
This class provides the ability to make remote calls to the backing service through method calls that map to API methods. Sample code to get started:
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedName = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
String formattedTopic = SubscriberApi.formatTopicName("[PROJECT]", "[TOPIC]");
PushConfig pushConfig = PushConfig.newBuilder().build();
int ackDeadlineSeconds = 0;
Subscription response = subscriberApi.createSubscription(formattedName, formattedTopic, pushConfig, ackDeadlineSeconds);
}
Note: close() needs to be called on the subscriberApi object to clean up resources such as threads. In the example above, try-with-resources is used, which automatically calls close().
The surface of this class includes several types of Java methods for each of the API's methods:
See the individual methods for example code.
Many parameters require resource names to be formatted in a particular way. To assist with these names, this class includes a format method for each type of name, and additionally a parse method to extract the individual identifiers contained within names that are returned.
This class can be customized by passing in a custom instance of SubscriberSettings to create(). For example:
SubscriberSettings subscriberSettings = SubscriberSettings.defaultBuilder()
.provideChannelWith(myCredentials)
.build();
SubscriberApi subscriberApi = SubscriberApi.create(subscriberSettings);
| Modifier and Type | Method and Description |
|---|---|
void |
acknowledge(AcknowledgeRequest request)
Acknowledges the messages associated with the `ack_ids` in the
`AcknowledgeRequest`.
|
void |
acknowledge(java.lang.String subscription,
java.util.List<java.lang.String> ackIds)
Acknowledges the messages associated with the `ack_ids` in the
`AcknowledgeRequest`.
|
ApiCallable<AcknowledgeRequest,Empty> |
acknowledgeCallable()
Acknowledges the messages associated with the `ack_ids` in the
`AcknowledgeRequest`.
|
void |
close()
Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately
cancelled.
|
static SubscriberApi |
create(SubscriberSettings settings)
Constructs an instance of SubscriberApi, using the given settings.
|
Subscription |
createSubscription(java.lang.String name,
java.lang.String topic,
PushConfig pushConfig,
int ackDeadlineSeconds)
Creates a subscription to a given topic.
|
Subscription |
createSubscription(Subscription request)
Creates a subscription to a given topic.
|
ApiCallable<Subscription,Subscription> |
createSubscriptionCallable()
Creates a subscription to a given topic.
|
static SubscriberApi |
createWithDefaults()
Constructs an instance of SubscriberApi with default settings.
|
void |
deleteSubscription(java.lang.String subscription)
Deletes an existing subscription.
|
ApiCallable<DeleteSubscriptionRequest,Empty> |
deleteSubscriptionCallable()
Deletes an existing subscription.
|
static java.lang.String |
formatProjectName(java.lang.String project)
Formats a string containing the fully-qualified path to represent
a project resource.
|
static java.lang.String |
formatSubscriptionName(java.lang.String project,
java.lang.String subscription)
Formats a string containing the fully-qualified path to represent
a subscription resource.
|
static java.lang.String |
formatTopicName(java.lang.String project,
java.lang.String topic)
Formats a string containing the fully-qualified path to represent
a topic resource.
|
SubscriberSettings |
getSettings() |
Subscription |
getSubscription(java.lang.String subscription)
Gets the configuration details of a subscription.
|
ApiCallable<GetSubscriptionRequest,Subscription> |
getSubscriptionCallable()
Gets the configuration details of a subscription.
|
PageAccessor<Subscription> |
listSubscriptions(ListSubscriptionsRequest request)
Lists matching subscriptions.
|
PageAccessor<Subscription> |
listSubscriptions(java.lang.String project)
Lists matching subscriptions.
|
ApiCallable<ListSubscriptionsRequest,ListSubscriptionsResponse> |
listSubscriptionsCallable()
Lists matching subscriptions.
|
ApiCallable<ListSubscriptionsRequest,PageAccessor<Subscription>> |
listSubscriptionsPagedCallable()
Lists matching subscriptions.
|
void |
modifyAckDeadline(ModifyAckDeadlineRequest request)
Modifies the ack deadline for a specific message.
|
void |
modifyAckDeadline(java.lang.String subscription,
java.util.List<java.lang.String> ackIds,
int ackDeadlineSeconds)
Modifies the ack deadline for a specific message.
|
ApiCallable<ModifyAckDeadlineRequest,Empty> |
modifyAckDeadlineCallable()
Modifies the ack deadline for a specific message.
|
void |
modifyPushConfig(ModifyPushConfigRequest request)
Modifies the `PushConfig` for a specified subscription.
|
void |
modifyPushConfig(java.lang.String subscription,
PushConfig pushConfig)
Modifies the `PushConfig` for a specified subscription.
|
ApiCallable<ModifyPushConfigRequest,Empty> |
modifyPushConfigCallable()
Modifies the `PushConfig` for a specified subscription.
|
static java.lang.String |
parseProjectFromProjectName(java.lang.String projectName)
Parses the project from the given fully-qualified path which
represents a project resource.
|
static java.lang.String |
parseProjectFromSubscriptionName(java.lang.String subscriptionName)
Parses the project from the given fully-qualified path which
represents a subscription resource.
|
static java.lang.String |
parseProjectFromTopicName(java.lang.String topicName)
Parses the project from the given fully-qualified path which
represents a topic resource.
|
static java.lang.String |
parseSubscriptionFromSubscriptionName(java.lang.String subscriptionName)
Parses the subscription from the given fully-qualified path which
represents a subscription resource.
|
static java.lang.String |
parseTopicFromTopicName(java.lang.String topicName)
Parses the topic from the given fully-qualified path which
represents a topic resource.
|
PullResponse |
pull(PullRequest request)
Pulls messages from the server.
|
PullResponse |
pull(java.lang.String subscription,
boolean returnImmediately,
int maxMessages)
Pulls messages from the server.
|
ApiCallable<PullRequest,PullResponse> |
pullCallable()
Pulls messages from the server.
|
public final SubscriberSettings getSettings()
public static final java.lang.String formatProjectName(java.lang.String project)
public static final java.lang.String formatSubscriptionName(java.lang.String project,
java.lang.String subscription)
public static final java.lang.String formatTopicName(java.lang.String project,
java.lang.String topic)
public static final java.lang.String parseProjectFromProjectName(java.lang.String projectName)
public static final java.lang.String parseProjectFromSubscriptionName(java.lang.String subscriptionName)
public static final java.lang.String parseSubscriptionFromSubscriptionName(java.lang.String subscriptionName)
public static final java.lang.String parseProjectFromTopicName(java.lang.String topicName)
public static final java.lang.String parseTopicFromTopicName(java.lang.String topicName)
public static final SubscriberApi createWithDefaults() throws java.io.IOException
java.io.IOExceptionpublic static final SubscriberApi create(SubscriberSettings settings) throws java.io.IOException
java.io.IOExceptionpublic final Subscription createSubscription(java.lang.String name, java.lang.String topic, PushConfig pushConfig, int ackDeadlineSeconds)
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedName = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
String formattedTopic = SubscriberApi.formatTopicName("[PROJECT]", "[TOPIC]");
PushConfig pushConfig = PushConfig.newBuilder().build();
int ackDeadlineSeconds = 0;
Subscription response = subscriberApi.createSubscription(formattedName, formattedTopic, pushConfig, ackDeadlineSeconds);
}
name - The name of the subscription. It must have the format
`"projects/{project}/subscriptions/{subscription}"`. `{subscription}` must
start with a letter, and contain only letters (`[A-Za-z]`), numbers
(`[0-9]`), dashes (`-`), underscores (`_`), periods (`.`), tildes (`~`),
plus (`+`) or percent signs (`%`). It must be between 3 and 255 characters
in length, and it must not start with `"goog"`.topic - The name of the topic from which this subscription is receiving messages.pushConfig - If push delivery is used with this subscription, this field is
used to configure it. An empty `pushConfig` signifies that the subscriber
will pull and ack messages using API methods.ackDeadlineSeconds - This value is the maximum time after a subscriber receives a message
before the subscriber should acknowledge the message. After message
delivery but before the ack deadline expires and before the message is
acknowledged, it is an outstanding message and will not be delivered
again during that time (on a best-effort basis).
For pull subscriptions, this value is used as the initial value for the ack
deadline. To override this value for a given message, call
`ModifyAckDeadline` with the corresponding `ack_id` if using
pull.
For push delivery, this value is also used to set the request timeout for
the call to the push endpoint.
If the subscriber never acknowledges the message, the Pub/Sub
system will eventually redeliver the message.
If this parameter is not set, the default value of 10 seconds is used.ApiException - if the remote call failspublic Subscription createSubscription(Subscription request)
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedName = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
String formattedTopic = SubscriberApi.formatTopicName("[PROJECT]", "[TOPIC]");
Subscription request = Subscription.newBuilder()
.setName(formattedName)
.setTopic(formattedTopic)
.build();
Subscription response = subscriberApi.createSubscription(request);
}
request - The request object containing all of the parameters for the API call.ApiException - if the remote call failspublic final ApiCallable<Subscription,Subscription> createSubscriptionCallable()
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedName = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
String formattedTopic = SubscriberApi.formatTopicName("[PROJECT]", "[TOPIC]");
Subscription request = Subscription.newBuilder()
.setName(formattedName)
.setTopic(formattedTopic)
.build();
ListenableFuture<Subscription> future = subscriberApi.createSubscriptionCallable().futureCall(request);
// Do something
Subscription response = future.get();
}
public final Subscription getSubscription(java.lang.String subscription)
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
Subscription response = subscriberApi.getSubscription(formattedSubscription);
}
subscription - The name of the subscription to get.ApiException - if the remote call failspublic final ApiCallable<GetSubscriptionRequest,Subscription> getSubscriptionCallable()
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
GetSubscriptionRequest request = GetSubscriptionRequest.newBuilder()
.setSubscription(formattedSubscription)
.build();
ListenableFuture<Subscription> future = subscriberApi.getSubscriptionCallable().futureCall(request);
// Do something
Subscription response = future.get();
}
public final PageAccessor<Subscription> listSubscriptions(java.lang.String project)
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedProject = SubscriberApi.formatProjectName("[PROJECT]");
for (Subscription elements : subscriberApi.listSubscriptions(formattedProject)) {
// doThingsWith(elements);
}
}
project - The name of the cloud project that subscriptions belong to.ApiException - if the remote call failspublic final PageAccessor<Subscription> listSubscriptions(ListSubscriptionsRequest request)
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedProject = SubscriberApi.formatProjectName("[PROJECT]");
ListSubscriptionsRequest request = ListSubscriptionsRequest.newBuilder()
.setProject(formattedProject)
.build();
for (Subscription elements : subscriberApi.listSubscriptions(request)) {
// doThingsWith(elements);
}
}
request - The request object containing all of the parameters for the API call.ApiException - if the remote call failspublic final ApiCallable<ListSubscriptionsRequest,PageAccessor<Subscription>> listSubscriptionsPagedCallable()
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedProject = SubscriberApi.formatProjectName("[PROJECT]");
ListSubscriptionsRequest request = ListSubscriptionsRequest.newBuilder()
.setProject(formattedProject)
.build();
ListenableFuture<PageAccessor<Subscription>> future = subscriberApi.listSubscriptionsPagedCallable().futureCall(request);
// Do something
for (Subscription elements : future.get()) {
// doThingsWith(elements);
}
}
public final ApiCallable<ListSubscriptionsRequest,ListSubscriptionsResponse> listSubscriptionsCallable()
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedProject = SubscriberApi.formatProjectName("[PROJECT]");
ListSubscriptionsRequest request = ListSubscriptionsRequest.newBuilder()
.setProject(formattedProject)
.build();
while (true) {
ListSubscriptionsResponse response = subscriberApi.listSubscriptionsCallable().call(request);
for (Subscription elements : response.getSubscriptionsList()) {
// doThingsWith(elements);
}
String nextPageToken = response.getNextPageToken();
if (!Strings.isNullOrEmpty(nextPageToken)) {
request = request.toBuilder().setPageToken(nextPageToken).build();
} else {
break;
}
}
}
public final void deleteSubscription(java.lang.String subscription)
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
subscriberApi.deleteSubscription(formattedSubscription);
}
subscription - The subscription to delete.ApiException - if the remote call failspublic final ApiCallable<DeleteSubscriptionRequest,Empty> deleteSubscriptionCallable()
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
DeleteSubscriptionRequest request = DeleteSubscriptionRequest.newBuilder()
.setSubscription(formattedSubscription)
.build();
ListenableFuture<Void> future = subscriberApi.deleteSubscriptionCallable().futureCall(request);
// Do something
future.get();
}
public final void modifyAckDeadline(java.lang.String subscription,
java.util.List<java.lang.String> ackIds,
int ackDeadlineSeconds)
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
List<String> ackIds = new ArrayList<>();
int ackDeadlineSeconds = 0;
subscriberApi.modifyAckDeadline(formattedSubscription, ackIds, ackDeadlineSeconds);
}
subscription - The name of the subscription.ackIds - List of acknowledgment IDs.ackDeadlineSeconds - The new ack deadline with respect to the time this request was sent to
the Pub/Sub system. Must be >= 0. For example, if the value is 10, the new
ack deadline will expire 10 seconds after the `ModifyAckDeadline` call
was made. Specifying zero may immediately make the message available for
another pull request.ApiException - if the remote call failspublic void modifyAckDeadline(ModifyAckDeadlineRequest request)
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
List<String> ackIds = new ArrayList<>();
int ackDeadlineSeconds = 0;
ModifyAckDeadlineRequest request = ModifyAckDeadlineRequest.newBuilder()
.setSubscription(formattedSubscription)
.addAllAckIds(ackIds)
.setAckDeadlineSeconds(ackDeadlineSeconds)
.build();
subscriberApi.modifyAckDeadline(request);
}
request - The request object containing all of the parameters for the API call.ApiException - if the remote call failspublic final ApiCallable<ModifyAckDeadlineRequest,Empty> modifyAckDeadlineCallable()
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
List<String> ackIds = new ArrayList<>();
int ackDeadlineSeconds = 0;
ModifyAckDeadlineRequest request = ModifyAckDeadlineRequest.newBuilder()
.setSubscription(formattedSubscription)
.addAllAckIds(ackIds)
.setAckDeadlineSeconds(ackDeadlineSeconds)
.build();
ListenableFuture<Void> future = subscriberApi.modifyAckDeadlineCallable().futureCall(request);
// Do something
future.get();
}
public final void acknowledge(java.lang.String subscription,
java.util.List<java.lang.String> ackIds)
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
List<String> ackIds = new ArrayList<>();
subscriberApi.acknowledge(formattedSubscription, ackIds);
}
subscription - The subscription whose message is being acknowledged.ackIds - The acknowledgment ID for the messages being acknowledged that was returned
by the Pub/Sub system in the `Pull` response. Must not be empty.ApiException - if the remote call failspublic void acknowledge(AcknowledgeRequest request)
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
List<String> ackIds = new ArrayList<>();
AcknowledgeRequest request = AcknowledgeRequest.newBuilder()
.setSubscription(formattedSubscription)
.addAllAckIds(ackIds)
.build();
subscriberApi.acknowledge(request);
}
request - The request object containing all of the parameters for the API call.ApiException - if the remote call failspublic final ApiCallable<AcknowledgeRequest,Empty> acknowledgeCallable()
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
List<String> ackIds = new ArrayList<>();
AcknowledgeRequest request = AcknowledgeRequest.newBuilder()
.setSubscription(formattedSubscription)
.addAllAckIds(ackIds)
.build();
ListenableFuture<Void> future = subscriberApi.acknowledgeCallable().futureCall(request);
// Do something
future.get();
}
public final PullResponse pull(java.lang.String subscription, boolean returnImmediately, int maxMessages)
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
boolean returnImmediately = false;
int maxMessages = 0;
PullResponse response = subscriberApi.pull(formattedSubscription, returnImmediately, maxMessages);
}
subscription - The subscription from which messages should be pulled.returnImmediately - If this is specified as true the system will respond immediately even if
it is not able to return a message in the `Pull` response. Otherwise the
system is allowed to wait until at least one message is available rather
than returning no messages.maxMessages - The maximum number of messages returned for this request. The Pub/Sub
system may return fewer than the number specified.ApiException - if the remote call failspublic PullResponse pull(PullRequest request)
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
int maxMessages = 0;
PullRequest request = PullRequest.newBuilder()
.setSubscription(formattedSubscription)
.setMaxMessages(maxMessages)
.build();
PullResponse response = subscriberApi.pull(request);
}
request - The request object containing all of the parameters for the API call.ApiException - if the remote call failspublic final ApiCallable<PullRequest,PullResponse> pullCallable()
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
int maxMessages = 0;
PullRequest request = PullRequest.newBuilder()
.setSubscription(formattedSubscription)
.setMaxMessages(maxMessages)
.build();
ListenableFuture<PullResponse> future = subscriberApi.pullCallable().futureCall(request);
// Do something
PullResponse response = future.get();
}
public final void modifyPushConfig(java.lang.String subscription,
PushConfig pushConfig)
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
PushConfig pushConfig = PushConfig.newBuilder().build();
subscriberApi.modifyPushConfig(formattedSubscription, pushConfig);
}
subscription - The name of the subscription.pushConfig - The push configuration for future deliveries.
An empty `pushConfig` indicates that the Pub/Sub system should
stop pushing messages from the given subscription and allow
messages to be pulled and acknowledged - effectively pausing
the subscription if `Pull` is not called.ApiException - if the remote call failspublic void modifyPushConfig(ModifyPushConfigRequest request)
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
PushConfig pushConfig = PushConfig.newBuilder().build();
ModifyPushConfigRequest request = ModifyPushConfigRequest.newBuilder()
.setSubscription(formattedSubscription)
.setPushConfig(pushConfig)
.build();
subscriberApi.modifyPushConfig(request);
}
request - The request object containing all of the parameters for the API call.ApiException - if the remote call failspublic final ApiCallable<ModifyPushConfigRequest,Empty> modifyPushConfigCallable()
try (SubscriberApi subscriberApi = SubscriberApi.createWithDefaults()) {
String formattedSubscription = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
PushConfig pushConfig = PushConfig.newBuilder().build();
ModifyPushConfigRequest request = ModifyPushConfigRequest.newBuilder()
.setSubscription(formattedSubscription)
.setPushConfig(pushConfig)
.build();
ListenableFuture<Void> future = subscriberApi.modifyPushConfigCallable().futureCall(request);
// Do something
future.get();
}
public final void close()
throws java.lang.Exception
close in interface java.lang.AutoCloseablejava.lang.Exception