use of com.google.pubsub.v1.Subscription in project google-cloud-java by GoogleCloudPlatform.
the class TopicAdminClientSnippets method listTopicSubscriptions.
/** Example of listing topics for a subscription. */
public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicId) throws Exception {
// [START pubsub_list_topic_subscriptions]
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
TopicName topicName = TopicName.create(projectId, topicId);
ListTopicSubscriptionsRequest request = ListTopicSubscriptionsRequest.newBuilder().setTopicWithTopicName(topicName).build();
ListTopicSubscriptionsPagedResponse response = topicAdminClient.listTopicSubscriptions(request);
Iterable<String> subscriptionNames = response.iterateAll();
for (String subscriptionName : subscriptionNames) {
// do something with the subscription name
}
return response;
}
// [END pubsub_list_topic_subscriptions]
}
use of com.google.pubsub.v1.Subscription in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClient method deleteSubscription.
// AUTO-GENERATED DOCUMENTATION AND METHOD
/**
* Deletes an existing subscription. All messages retained in the subscription are immediately
* dropped. Calls to `Pull` after deletion will return `NOT_FOUND`. After a subscription is
* deleted, a new one may be created with the same name, but the new one has no association with
* the old subscription or its topic unless the same topic is specified.
*
* <p>Sample code:
*
* <pre><code>
* try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
* SubscriptionName subscription = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
* subscriptionAdminClient.deleteSubscription(subscription);
* }
* </code></pre>
*
* @param subscription The subscription to delete. Format is
* `projects/{project}/subscriptions/{sub}`.
* @throws com.google.api.gax.grpc.ApiException if the remote call fails
*/
public final void deleteSubscription(SubscriptionName subscription) {
DeleteSubscriptionRequest request = DeleteSubscriptionRequest.newBuilder().setSubscriptionWithSubscriptionName(subscription).build();
deleteSubscription(request);
}
use of com.google.pubsub.v1.Subscription in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClient method deleteSnapshot.
// AUTO-GENERATED DOCUMENTATION AND METHOD
/**
* Removes an existing snapshot. All messages retained in the snapshot are immediately dropped.
* After a snapshot is deleted, a new one may be created with the same name, but the new one has
* no association with the old snapshot or its subscription, unless the same subscription is
* specified.
*
* <p>Sample code:
*
* <pre><code>
* try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
* SnapshotName snapshot = SnapshotName.create("[PROJECT]", "[SNAPSHOT]");
* subscriptionAdminClient.deleteSnapshot(snapshot);
* }
* </code></pre>
*
* @param snapshot The name of the snapshot to delete. Format is
* `projects/{project}/snapshots/{snap}`.
* @throws com.google.api.gax.grpc.ApiException if the remote call fails
*/
public final void deleteSnapshot(SnapshotName snapshot) {
DeleteSnapshotRequest request = DeleteSnapshotRequest.newBuilder().setSnapshotWithSnapshotName(snapshot).build();
deleteSnapshot(request);
}
use of com.google.pubsub.v1.Subscription in project google-cloud-java by GoogleCloudPlatform.
the class PollingSubscriberConnection method pullMessages.
private void pullMessages(final Duration backoff) {
ListenableFuture<PullResponse> pullResult = stub.withDeadlineAfter(DEFAULT_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS).pull(PullRequest.newBuilder().setSubscription(subscription).setMaxMessages(maxDesiredPulledMessages).setReturnImmediately(true).build());
Futures.addCallback(pullResult, new FutureCallback<PullResponse>() {
@Override
public void onSuccess(PullResponse pullResponse) {
if (pullResponse.getReceivedMessagesCount() == 0) {
// No messages in response, possibly caught up in backlog, we backoff to avoid
// slamming the server.
pollingExecutor.schedule(new Runnable() {
@Override
public void run() {
Duration newBackoff = backoff.multipliedBy(2);
if (newBackoff.compareTo(MAX_BACKOFF) > 0) {
newBackoff = MAX_BACKOFF;
}
pullMessages(newBackoff);
}
}, backoff.toMillis(), TimeUnit.MILLISECONDS);
return;
}
messageDispatcher.processReceivedMessages(pullResponse.getReceivedMessagesList(), new Runnable() {
@Override
public void run() {
pullMessages(INITIAL_BACKOFF);
}
});
}
@Override
public void onFailure(Throwable cause) {
if (!isAlive()) {
// we don't care about subscription failures when we're no longer running.
logger.log(Level.FINE, "pull failure after service no longer running", cause);
return;
}
if (StatusUtil.isRetryable(cause)) {
logger.log(Level.WARNING, "Failed to pull messages (recoverable): ", cause);
pollingExecutor.schedule(new Runnable() {
@Override
public void run() {
Duration newBackoff = backoff.multipliedBy(2);
if (newBackoff.compareTo(MAX_BACKOFF) > 0) {
newBackoff = MAX_BACKOFF;
}
pullMessages(newBackoff);
}
}, backoff.toMillis(), TimeUnit.MILLISECONDS);
} else {
messageDispatcher.stop();
notifyFailed(cause);
}
}
}, pollingExecutor);
}
use of com.google.pubsub.v1.Subscription in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClient method modifyPushConfig.
// AUTO-GENERATED DOCUMENTATION AND METHOD
/**
* Modifies the `PushConfig` for a specified subscription.
*
* <p>This may be used to change a push subscription to a pull one (signified by an empty
* `PushConfig`) or vice versa, or change the endpoint URL and other attributes of a push
* subscription. Messages will accumulate for delivery continuously through the call regardless of
* changes to the `PushConfig`.
*
* <p>Sample code:
*
* <pre><code>
* try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
* SubscriptionName subscription = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
* PushConfig pushConfig = PushConfig.newBuilder().build();
* subscriptionAdminClient.modifyPushConfig(subscription, pushConfig);
* }
* </code></pre>
*
* @param subscription The name of the subscription. Format is
* `projects/{project}/subscriptions/{sub}`.
* @param pushConfig The push configuration for future deliveries.
* <p>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.
* @throws com.google.api.gax.grpc.ApiException if the remote call fails
*/
public final void modifyPushConfig(SubscriptionName subscription, PushConfig pushConfig) {
ModifyPushConfigRequest request = ModifyPushConfigRequest.newBuilder().setSubscriptionWithSubscriptionName(subscription).setPushConfig(pushConfig).build();
modifyPushConfig(request);
}
Aggregations