Search in sources :

Example 11 with SubscriptionAdminClient

use of com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient in project google-cloud-java by GoogleCloudPlatform.

the class SubscriptionAdminClientSnippets method deleteSubscription.

/** Example of deleting a subscription. */
public SubscriptionName deleteSubscription(String subscriptionId) throws Exception {
    // [START pubsub_delete_subscription]
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
        subscriptionAdminClient.deleteSubscription(subscriptionName);
        return subscriptionName;
    }
// [END pubsub_delete_subscription]
}
Also used : SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient) SubscriptionName(com.google.pubsub.v1.SubscriptionName)

Example 12 with SubscriptionAdminClient

use of com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient in project google-cloud-java by GoogleCloudPlatform.

the class SubscriptionAdminClientSnippets method createSubscription.

/** Example of creating a pull subscription for a topic. */
public Subscription createSubscription(String topicId, String subscriptionId) throws Exception {
    // [START pubsub_create_pull_subscription]
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        // eg. projectId = "my-test-project", topicId = "my-test-topic"
        TopicName topicName = TopicName.create(projectId, topicId);
        // eg. subscriptionId = "my-test-subscription"
        SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
        // create a pull subscription with default acknowledgement deadline
        Subscription subscription = subscriptionAdminClient.createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
        return subscription;
    }
// [END pubsub_create_pull_subscription]
}
Also used : SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient) SubscriptionName(com.google.pubsub.v1.SubscriptionName) Subscription(com.google.pubsub.v1.Subscription) TopicName(com.google.pubsub.v1.TopicName)

Example 13 with SubscriptionAdminClient

use of com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient in project google-cloud-java by GoogleCloudPlatform.

the class ITPubSubSnippets method testPublisherSubscriber.

@Test
public void testPublisherSubscriber() throws Exception {
    TopicName topicName = TopicName.create(ServiceOptions.getDefaultProjectId(), formatForTest("test-topic"));
    SubscriptionName subscriptionName = SubscriptionName.create(ServiceOptions.getDefaultProjectId(), formatForTest("test-subscription"));
    try (TopicAdminClient publisherClient = TopicAdminClient.create();
        SubscriptionAdminClient subscriberClient = SubscriptionAdminClient.create()) {
        publisherClient.createTopic(topicName);
        subscriberClient.createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
        testPublisherSubscriberHelper(topicName, subscriptionName);
        subscriberClient.deleteSubscription(subscriptionName);
        publisherClient.deleteTopic(topicName);
    }
}
Also used : TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient) SubscriptionName(com.google.pubsub.v1.SubscriptionName) TopicName(com.google.pubsub.v1.TopicName) Test(org.junit.Test)

Example 14 with SubscriptionAdminClient

use of com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient in project java-docs-samples by GoogleCloudPlatform.

the class CreatePullSubscriptionExample method main.

/**
 * Create a pull subscription.
 *
 * @param args topic subscriptionId
 * @throws Exception exception thrown if operation is unsuccessful
 */
public static void main(String... args) throws Exception {
    // Your Google Cloud Platform project ID
    String projectId = ServiceOptions.getDefaultProjectId();
    // Your topic ID, eg. "my-topic"
    String topicId = args[0];
    // Your subscription ID eg. "my-sub"
    String subscriptionId = args[1];
    ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
    // Create a new subscription
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        // create a pull subscription with default acknowledgement deadline (= 10 seconds)
        Subscription subscription = subscriptionAdminClient.createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
    } catch (ApiException e) {
        // example : code = ALREADY_EXISTS(409) implies subscription already exists
        System.out.print(e.getStatusCode().getCode());
        System.out.print(e.isRetryable());
    }
    System.out.printf("Subscription %s:%s created.\n", subscriptionName.getProject(), subscriptionName.getSubscription());
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) SubscriptionAdminClient(com.google.cloud.pubsub.v1.SubscriptionAdminClient) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) Subscription(com.google.pubsub.v1.Subscription) ApiException(com.google.api.gax.rpc.ApiException)

Example 15 with SubscriptionAdminClient

use of com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient in project flink by apache.

the class PubsubHelper method deleteSubscription.

public void deleteSubscription(ProjectSubscriptionName subscriptionName) throws IOException {
    SubscriptionAdminClient adminClient = getSubscriptionAdminClient();
    try {
        adminClient.getSubscription(subscriptionName);
        // If it already exists we must first delete it.
        LOG.info("DeleteSubscription {}", subscriptionName);
        adminClient.deleteSubscription(subscriptionName);
    } catch (NotFoundException e) {
    // Doesn't exist. Good.
    }
}
Also used : SubscriptionAdminClient(com.google.cloud.pubsub.v1.SubscriptionAdminClient) NotFoundException(com.google.api.gax.rpc.NotFoundException)

Aggregations

SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)11 SubscriptionName (com.google.pubsub.v1.SubscriptionName)9 Subscription (com.google.pubsub.v1.Subscription)6 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)4 TopicName (com.google.pubsub.v1.TopicName)4 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)3 PushConfig (com.google.pubsub.v1.PushConfig)3 Policy (com.google.iam.v1.Policy)2 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)2 Test (org.junit.Test)2 ApiException (com.google.api.gax.rpc.ApiException)1 NotFoundException (com.google.api.gax.rpc.NotFoundException)1 AckReplyConsumer (com.google.cloud.pubsub.spi.v1.AckReplyConsumer)1 MessageReceiver (com.google.cloud.pubsub.spi.v1.MessageReceiver)1 ListSubscriptionsPagedResponse (com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListSubscriptionsPagedResponse)1 Subscriber (com.google.cloud.pubsub.spi.v1.Subscriber)1 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)1 Subscriber (com.google.cloud.pubsub.v1.Subscriber)1 Occurrence (com.google.containeranalysis.v1alpha1.Occurrence)1 Binding (com.google.iam.v1.Binding)1