Search in sources :

Example 21 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName 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)

Example 22 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName 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 23 with SubscriptionName

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

the class Samples method pubSub.

// [END occurrences_for_image]
// [START pubsub]
/**
 * Handle incoming occurrences using a pubsub subscription
 * @param subscriptionId the user-specified identifier for the pubsub subscription
 * @param timeout the amount of time to listen for pubsub messages (in seconds)
 * @param projectId the project's unique identifier
 * @return number of occurrence pubsub messages received
 * @throws Exception on errors with the subscription client
 */
public static int pubSub(String subscriptionId, int timeout, String projectId) throws Exception {
    Subscriber subscriber = null;
    MessageReceiverExample receiver = new MessageReceiverExample();
    try {
        // subscribe to the requested pubsub channel
        ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
        subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
        subscriber.startAsync().awaitRunning();
        // listen to messages for 'listenTimeout' seconds
        for (int i = 0; i < timeout; i++) {
            sleep(1000);
        }
    } finally {
        // stop listening to the channel
        if (subscriber != null) {
            subscriber.stopAsync();
        }
    }
    // print and return the number of pubsub messages received
    System.out.println(receiver.messageCount);
    return receiver.messageCount;
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) Subscriber(com.google.cloud.pubsub.v1.Subscriber)

Example 24 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName 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 25 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName 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)

Aggregations

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