Search in sources :

Example 6 with Subscription

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]
}
Also used : ListTopicSubscriptionsPagedResponse(com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse) TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) ListTopicSubscriptionsRequest(com.google.pubsub.v1.ListTopicSubscriptionsRequest) TopicName(com.google.pubsub.v1.TopicName)

Example 7 with Subscription

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);
}
Also used : DeleteSubscriptionRequest(com.google.pubsub.v1.DeleteSubscriptionRequest)

Example 8 with Subscription

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);
}
Also used : DeleteSnapshotRequest(com.google.pubsub.v1.DeleteSnapshotRequest)

Example 9 with Subscription

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);
}
Also used : PullResponse(com.google.pubsub.v1.PullResponse) Duration(org.threeten.bp.Duration)

Example 10 with Subscription

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);
}
Also used : ModifyPushConfigRequest(com.google.pubsub.v1.ModifyPushConfigRequest)

Aggregations

SubscriptionName (com.google.pubsub.v1.SubscriptionName)31 Test (org.junit.Test)26 Subscription (com.google.pubsub.v1.Subscription)14 ByteString (com.google.protobuf.ByteString)11 SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)10 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)10 TopicName (com.google.pubsub.v1.TopicName)10 StatusRuntimeException (io.grpc.StatusRuntimeException)9 ApiException (com.google.api.gax.grpc.ApiException)8 ArrayList (java.util.ArrayList)8 PushConfig (com.google.pubsub.v1.PushConfig)6 PubsubMessage (com.google.pubsub.v1.PubsubMessage)5 Empty (com.google.protobuf.Empty)4 PullResponse (com.google.pubsub.v1.PullResponse)4 AckReplyConsumer (com.google.cloud.pubsub.spi.v1.AckReplyConsumer)3 MessageReceiver (com.google.cloud.pubsub.spi.v1.MessageReceiver)3 ListSubscriptionsPagedResponse (com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListSubscriptionsPagedResponse)3 Subscriber (com.google.cloud.pubsub.spi.v1.Subscriber)3 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)3 AcknowledgeRequest (com.google.pubsub.v1.AcknowledgeRequest)3