Search in sources :

Example 6 with SubscriptionAdminClient

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

the class SubscriptionAdminClientSnippets method replaceSubscriptionPolicy.

/** Example of replacing a subscription policy. */
public Policy replaceSubscriptionPolicy(String subscriptionId) throws Exception {
    // [START pubsub_set_subscription_policy]
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
        Policy policy = subscriptionAdminClient.getIamPolicy(subscriptionName.toString());
        // Create a role => members binding
        Binding binding = Binding.newBuilder().setRole(Role.viewer().toString()).addMembers(Identity.allAuthenticatedUsers().toString()).build();
        //Update policy
        Policy updatedPolicy = policy.toBuilder().addBindings(binding).build();
        updatedPolicy = subscriptionAdminClient.setIamPolicy(subscriptionName.toString(), updatedPolicy);
        return updatedPolicy;
    }
// [END pubsub_set_subscription_policy]
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient) SubscriptionName(com.google.pubsub.v1.SubscriptionName)

Example 7 with SubscriptionAdminClient

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

the class Samples method createOccurrenceSubscription.

/**
 * Creates and returns a pubsub subscription object listening to the occurrence topic
 * @param subscriptionId the identifier you want to associate with the subscription
 * @param projectId the project's unique identifier
 * @throws Exception on errors with the subscription client
 */
public static Subscription createOccurrenceSubscription(String subscriptionId, String projectId) throws Exception {
    String topicId = "resource-notes-occurrences-v1alpha1";
    try (SubscriptionAdminClient client = SubscriptionAdminClient.create()) {
        PushConfig config = PushConfig.getDefaultInstance();
        ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
        ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
        Subscription sub = client.createSubscription(subscriptionName, topicName, config, 0);
        return sub;
    }
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) PushConfig(com.google.pubsub.v1.PushConfig) SubscriptionAdminClient(com.google.cloud.pubsub.v1.SubscriptionAdminClient) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) Subscription(com.google.pubsub.v1.Subscription)

Example 8 with SubscriptionAdminClient

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

the class SamplesTests method testPubSub.

@Test
public void testPubSub() throws Exception {
    int newCount;
    int tries;
    String subscriptionId = "drydockOccurrences";
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(PROJECT_ID, subscriptionId);
    Samples.createOccurrenceSubscription(subscriptionId, PROJECT_ID);
    Subscriber subscriber = null;
    Samples.MessageReceiverExample receiver = new Samples.MessageReceiverExample();
    try {
        subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
        subscriber.startAsync().awaitRunning();
        // sleep so any messages in the queue can go through and be counted before we start the test
        sleep(SLEEP_TIME);
        // set the initial state of our counter
        int startVal = receiver.messageCount + 1;
        // now, we can test adding 3 more occurrences
        int endVal = startVal + 3;
        for (int i = startVal; i <= endVal; i++) {
            Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
            System.out.println("CREATED: " + o.getName());
            tries = 0;
            do {
                newCount = receiver.messageCount;
                sleep(SLEEP_TIME);
                tries += 1;
            } while (newCount != i && tries < TRY_LIMIT);
            System.out.println(receiver.messageCount + " : " + i);
            assertEquals(i, receiver.messageCount);
            Samples.deleteOccurrence(o.getName());
        }
    } catch (Exception e) {
        fail("exception thrown");
    } finally {
        if (subscriber != null) {
            subscriber.stopAsync();
        }
        // delete subscription now that we're done with it
        try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
            subscriptionAdminClient.deleteSubscription(subscriptionName);
        }
    }
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) Subscriber(com.google.cloud.pubsub.v1.Subscriber) SubscriptionAdminClient(com.google.cloud.pubsub.v1.SubscriptionAdminClient) Occurrence(com.google.containeranalysis.v1alpha1.Occurrence) Test(org.junit.Test)

Example 9 with SubscriptionAdminClient

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

the class SubscriptionAdminClientSnippets method createSubscriptionWithPushEndpoint.

/** Example of creating a subscription with a push endpoint. */
public Subscription createSubscriptionWithPushEndpoint(String topicId, String subscriptionId, String endpoint) throws Exception {
    // [START pubsub_create_push_subscription]
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        TopicName topicName = TopicName.create(projectId, topicId);
        SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
        // eg. endpoint = "https://my-test-project.appspot.com/push"
        PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build();
        // acknowledgement deadline in seconds for the message received over the push endpoint
        int ackDeadlineInSeconds = 10;
        Subscription subscription = subscriptionAdminClient.createSubscription(subscriptionName, topicName, pushConfig, ackDeadlineInSeconds);
        return subscription;
    }
// [END pubsub_create_push_subscription]
}
Also used : PushConfig(com.google.pubsub.v1.PushConfig) 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 10 with SubscriptionAdminClient

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

the class SubscriptionAdminClientSnippets method getSubscription.

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

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)4 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)3 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 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 ListSubscriptionsRequest (com.google.pubsub.v1.ListSubscriptionsRequest)1