Search in sources :

Example 1 with SubscriptionAdminClient

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

the class Cleanup method deleteTestSubscriptions.

private static void deleteTestSubscriptions(String projectId, String[] subscriptions) throws Exception {
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        for (String subscriptionId : subscriptions) {
            try {
                subscriptionAdminClient.deleteSubscription(SubscriptionName.create(projectId, subscriptionId));
                System.out.println("Subscription deleted : " + subscriptionId);
            } catch (Exception e) {
            //do nothing catch clause
            }
        }
    }
}
Also used : SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)

Example 2 with SubscriptionAdminClient

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

the class CreateSubscriptionAndConsumeMessages method main.

public static void main(String... args) throws Exception {
    TopicName topic = TopicName.create("my-project-id", "my-topic-id");
    SubscriptionName subscription = SubscriptionName.create("my-project-id", "my-topic-id");
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        subscriptionAdminClient.createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 0);
    }
    MessageReceiver receiver = new MessageReceiver() {

        @Override
        public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
            System.out.println("Received message: " + message.getData().toStringUtf8());
            consumer.ack();
        }
    };
    Subscriber subscriber = null;
    try {
        subscriber = Subscriber.defaultBuilder(subscription, receiver).build();
        subscriber.addListener(new Subscriber.Listener() {

            @Override
            public void failed(Subscriber.State from, Throwable failure) {
                // Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down.
                System.err.println(failure);
            }
        }, MoreExecutors.directExecutor());
        subscriber.startAsync().awaitRunning();
        Thread.sleep(60000);
    } finally {
        if (subscriber != null) {
            subscriber.stopAsync();
        }
    }
}
Also used : MessageReceiver(com.google.cloud.pubsub.spi.v1.MessageReceiver) Subscriber(com.google.cloud.pubsub.spi.v1.Subscriber) SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient) SubscriptionName(com.google.pubsub.v1.SubscriptionName) AckReplyConsumer(com.google.cloud.pubsub.spi.v1.AckReplyConsumer) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName)

Example 3 with SubscriptionAdminClient

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

the class SubscriptionAdminClientSnippets method getSubscriptionPolicy.

/** Example of getting a subscription policy. */
public Policy getSubscriptionPolicy(String subscriptionId) throws Exception {
    // [START pubsub_get_subscription_policy]
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
        Policy policy = subscriptionAdminClient.getIamPolicy(subscriptionName.toString());
        if (policy == null) {
        // subscription was not found
        }
        return policy;
    }
// [END pubsub_get_subscription_policy]
}
Also used : Policy(com.google.iam.v1.Policy) SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient) SubscriptionName(com.google.pubsub.v1.SubscriptionName)

Example 4 with SubscriptionAdminClient

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

the class SubscriptionAdminClientSnippets method listSubscriptions.

/** Example of listing subscriptions. */
public ListSubscriptionsPagedResponse listSubscriptions() throws Exception {
    // [START pubsub_list_subscriptions]
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        ListSubscriptionsRequest listSubscriptionsRequest = ListSubscriptionsRequest.newBuilder().setProjectWithProjectName(ProjectName.create(projectId)).build();
        ListSubscriptionsPagedResponse response = subscriptionAdminClient.listSubscriptions(listSubscriptionsRequest);
        Iterable<Subscription> subscriptions = response.iterateAll();
        for (Subscription subscription : subscriptions) {
        // do something with the subscription
        }
        return response;
    }
// [END pubsub_list_subscriptions]
}
Also used : ListSubscriptionsPagedResponse(com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListSubscriptionsPagedResponse) ListSubscriptionsRequest(com.google.pubsub.v1.ListSubscriptionsRequest) SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient) Subscription(com.google.pubsub.v1.Subscription)

Example 5 with SubscriptionAdminClient

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

the class SubscriptionAdminClientSnippets method replacePushConfig.

/** Example of replacing the push configuration of a subscription, setting the push endpoint. */
public void replacePushConfig(String subscriptionId, String endpoint) throws Exception {
    // [START pubsub_update_push_configuration]
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
        PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build();
        subscriptionAdminClient.modifyPushConfig(subscriptionName, pushConfig);
    }
// [END pubsub_update_push_configuration]
}
Also used : PushConfig(com.google.pubsub.v1.PushConfig) SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient) SubscriptionName(com.google.pubsub.v1.SubscriptionName)

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