Search in sources :

Example 16 with SubscriptionAdminClient

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

the class UsePubSubEmulatorSnippet method main.

public static void main(String... args) throws IOException {
    // [START pubsub_use_emulator]
    String hostport = System.getenv("PUBSUB_EMULATOR_HOST");
    ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext().build();
    try {
        TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
        CredentialsProvider credentialsProvider = NoCredentialsProvider.create();
        // Set the channel and credentials provider when creating a `TopicAdminClient`.
        // Similarly for SubscriptionAdminClient
        TopicAdminClient topicClient = TopicAdminClient.create(TopicAdminSettings.newBuilder().setTransportChannelProvider(channelProvider).setCredentialsProvider(credentialsProvider).build());
        TopicName topicName = TopicName.of("my-project-id", "my-topic-id");
        // Set the channel and credentials provider when creating a `Publisher`.
        // Similarly for Subscriber
        Publisher publisher = Publisher.newBuilder(topicName).setChannelProvider(channelProvider).setCredentialsProvider(credentialsProvider).build();
    } finally {
        channel.shutdown();
    }
// [END pubsub_use_emulator]
}
Also used : TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) ManagedChannel(io.grpc.ManagedChannel) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) NoCredentialsProvider(com.google.api.gax.core.NoCredentialsProvider) Publisher(com.google.cloud.pubsub.v1.Publisher) FixedTransportChannelProvider(com.google.api.gax.rpc.FixedTransportChannelProvider) TransportChannelProvider(com.google.api.gax.rpc.TransportChannelProvider) TopicName(com.google.pubsub.v1.TopicName)

Example 17 with SubscriptionAdminClient

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

the class PubsubHelper method deleteTopic.

public void deleteTopic(TopicName topicName) throws IOException {
    TopicAdminClient adminClient = getTopicAdminClient();
    try {
        adminClient.getTopic(topicName);
    } catch (NotFoundException e) {
        // Doesn't exist. Good.
        return;
    }
    // If it exists we delete all subscriptions and the topic itself.
    LOG.info("DeleteTopic {} first delete old subscriptions.", topicName);
    adminClient.listTopicSubscriptions(topicName).iterateAllAsProjectSubscriptionName().forEach(subscriptionAdminClient::deleteSubscription);
    LOG.info("DeleteTopic {}", topicName);
    adminClient.deleteTopic(topicName);
}
Also used : TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) NotFoundException(com.google.api.gax.rpc.NotFoundException)

Example 18 with SubscriptionAdminClient

use of com.google.cloud.pubsub.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 TopicName (com.google.pubsub.v1.TopicName)5 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)4 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)3 PushConfig (com.google.pubsub.v1.PushConfig)3 NotFoundException (com.google.api.gax.rpc.NotFoundException)2 TopicAdminClient (com.google.cloud.pubsub.v1.TopicAdminClient)2 Policy (com.google.iam.v1.Policy)2 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)2 Test (org.junit.Test)2 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)1 NoCredentialsProvider (com.google.api.gax.core.NoCredentialsProvider)1 ApiException (com.google.api.gax.rpc.ApiException)1 FixedTransportChannelProvider (com.google.api.gax.rpc.FixedTransportChannelProvider)1 TransportChannelProvider (com.google.api.gax.rpc.TransportChannelProvider)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