Search in sources :

Example 1 with ProjectSubscriptionName

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

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

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

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

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

the class SubscriberExample method main.

/**
 * Receive messages over a subscription.
 */
public static void main(String... args) throws Exception {
    // set subscriber id, eg. my-sub
    String subscriptionId = args[0];
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(PROJECT_ID, subscriptionId);
    Subscriber subscriber = null;
    try {
        // create a subscriber bound to the asynchronous message receiver
        subscriber = Subscriber.newBuilder(subscriptionName, new MessageReceiverExample()).build();
        subscriber.startAsync().awaitRunning();
        // Continue to listen to messages
        while (true) {
            PubsubMessage message = messages.take();
            System.out.println("Message Id: " + message.getMessageId());
            System.out.println("Data: " + message.getData().toStringUtf8());
        }
    } finally {
        if (subscriber != null) {
            subscriber.stopAsync();
        }
    }
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) Subscriber(com.google.cloud.pubsub.v1.Subscriber) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Aggregations

ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)5 Subscriber (com.google.cloud.pubsub.v1.Subscriber)3 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)3 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)2 Subscription (com.google.pubsub.v1.Subscription)2 ApiException (com.google.api.gax.rpc.ApiException)1 Occurrence (com.google.containeranalysis.v1alpha1.Occurrence)1 PubsubMessage (com.google.pubsub.v1.PubsubMessage)1 PushConfig (com.google.pubsub.v1.PushConfig)1 Test (org.junit.Test)1