Search in sources :

Example 31 with SubscriptionName

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

the class SubscriptionAdminClientSnippets method createSubscription.

/** Example of creating a pull subscription for a topic. */
public Subscription createSubscription(String topicId, String subscriptionId) throws Exception {
    // [START pubsub_create_pull_subscription]
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        // eg. projectId = "my-test-project", topicId = "my-test-topic"
        TopicName topicName = TopicName.create(projectId, topicId);
        // eg. subscriptionId = "my-test-subscription"
        SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
        // create a pull subscription with default acknowledgement deadline
        Subscription subscription = subscriptionAdminClient.createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
        return subscription;
    }
// [END pubsub_create_pull_subscription]
}
Also used : 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 32 with SubscriptionName

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

the class ITSubscriptionAdminClientSnippets method createSubscriptionWithPushIsSuccessful.

@Test
public void createSubscriptionWithPushIsSuccessful() throws Exception {
    String topicName = topics[0];
    String subscriptionName = subscriptions[0];
    createTopic(topicName);
    String endpoint = "https://" + projectId + ".appspot.com/push";
    Subscription subscription = subscriptionAdminClientSnippets.createSubscriptionWithPushEndpoint(topicName, subscriptionName, endpoint);
    assertNotNull(subscription);
    Subscription retrievedSubscription = subscriptionAdminClientSnippets.getSubscription(subscriptionName);
    assertNotNull(retrievedSubscription);
    assertEquals(subscription.getName(), retrievedSubscription.getName());
    assertEquals(subscription.getPushConfig().getPushEndpoint(), endpoint);
}
Also used : ByteString(com.google.protobuf.ByteString) Subscription(com.google.pubsub.v1.Subscription) Test(org.junit.Test)

Example 33 with SubscriptionName

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

the class ITSubscriptionAdminClientSnippets method replacePushConfigIsSuccessful.

@Test
public void replacePushConfigIsSuccessful() throws Exception {
    String topicName = topics[0];
    String subscriptionName = subscriptions[0];
    createSubscription(topicName, subscriptionName);
    String endpoint = "https://" + projectId + ".appspot.com/push";
    subscriptionAdminClientSnippets.replacePushConfig(subscriptionName, endpoint);
    Subscription subscription = subscriptionAdminClientSnippets.getSubscription(subscriptionName);
    assertNotNull(subscription.getPushConfig());
    assertEquals(subscription.getPushConfig().getPushEndpoint(), endpoint);
}
Also used : ByteString(com.google.protobuf.ByteString) Subscription(com.google.pubsub.v1.Subscription) Test(org.junit.Test)

Example 34 with SubscriptionName

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

the class ITPubSubSnippets method testPublisherSubscriber.

@Test
public void testPublisherSubscriber() throws Exception {
    TopicName topicName = TopicName.create(ServiceOptions.getDefaultProjectId(), formatForTest("test-topic"));
    SubscriptionName subscriptionName = SubscriptionName.create(ServiceOptions.getDefaultProjectId(), formatForTest("test-subscription"));
    try (TopicAdminClient publisherClient = TopicAdminClient.create();
        SubscriptionAdminClient subscriberClient = SubscriptionAdminClient.create()) {
        publisherClient.createTopic(topicName);
        subscriberClient.createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
        testPublisherSubscriberHelper(topicName, subscriptionName);
        subscriberClient.deleteSubscription(subscriptionName);
        publisherClient.deleteTopic(topicName);
    }
}
Also used : TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient) SubscriptionName(com.google.pubsub.v1.SubscriptionName) TopicName(com.google.pubsub.v1.TopicName) Test(org.junit.Test)

Example 35 with SubscriptionName

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

the class SubscriptionAdminClient method acknowledge.

// AUTO-GENERATED DOCUMENTATION AND METHOD
/**
   * Acknowledges the messages associated with the `ack_ids` in the `AcknowledgeRequest`. The
   * Pub/Sub system can remove the relevant messages from the subscription.
   *
   * <p>Acknowledging a message whose ack deadline has expired may succeed, but such a message may
   * be redelivered later. Acknowledging a message more than once will not result in an error.
   *
   * <p>Sample code:
   *
   * <pre><code>
   * try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
   *   SubscriptionName subscription = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
   *   List&lt;String&gt; ackIds = new ArrayList&lt;&gt;();
   *   subscriptionAdminClient.acknowledge(subscription, ackIds);
   * }
   * </code></pre>
   *
   * @param subscription The subscription whose message is being acknowledged. Format is
   *     `projects/{project}/subscriptions/{sub}`.
   * @param ackIds The acknowledgment ID for the messages being acknowledged that was returned by
   *     the Pub/Sub system in the `Pull` response. Must not be empty.
   * @throws com.google.api.gax.grpc.ApiException if the remote call fails
   */
/* package-private */
final void acknowledge(SubscriptionName subscription, List<String> ackIds) {
    AcknowledgeRequest request = AcknowledgeRequest.newBuilder().setSubscriptionWithSubscriptionName(subscription).addAllAckIds(ackIds).build();
    acknowledge(request);
}
Also used : AcknowledgeRequest(com.google.pubsub.v1.AcknowledgeRequest)

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