Search in sources :

Example 21 with TopicName

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

the class PublisherSnippets method newBuilder.

/** Example of creating a {@code Publisher}. */
// [TARGET newBuilder(TopicName)]
// [VARIABLE "my_project"]
// [VARIABLE "my_topic"]
public static void newBuilder(String projectId, String topicId) throws Exception {
    TopicName topic = TopicName.create(projectId, topicId);
    Publisher publisher = Publisher.defaultBuilder(topic).build();
    try {
    // ...
    } finally {
        // When finished with the publisher, make sure to shutdown to free up resources.
        publisher.shutdown();
    }
}
Also used : Publisher(com.google.cloud.pubsub.spi.v1.Publisher) TopicName(com.google.pubsub.v1.TopicName)

Example 22 with TopicName

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

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

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

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

the class TopicAdminClientTest method listTopicSubscriptionsTest.

@Test
@SuppressWarnings("all")
public void listTopicSubscriptionsTest() {
    String nextPageToken = "";
    SubscriptionName subscriptionsElement = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
    List<SubscriptionName> subscriptions = Arrays.asList(subscriptionsElement);
    ListTopicSubscriptionsResponse expectedResponse = ListTopicSubscriptionsResponse.newBuilder().setNextPageToken(nextPageToken).addAllSubscriptionsWithSubscriptionNameList(subscriptions).build();
    mockPublisher.addResponse(expectedResponse);
    TopicName topic = TopicName.create("[PROJECT]", "[TOPIC]");
    ListTopicSubscriptionsPagedResponse pagedListResponse = client.listTopicSubscriptions(topic);
    List<String> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getSubscriptionsList().get(0), resources.get(0));
    List<SubscriptionName> resourceNames = Lists.newArrayList(pagedListResponse.iterateAllAsSubscriptionName());
    Assert.assertEquals(1, resourceNames.size());
    Assert.assertEquals(expectedResponse.getSubscriptionsListAsSubscriptionNameList().get(0), resourceNames.get(0));
    List<GeneratedMessageV3> actualRequests = mockPublisher.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListTopicSubscriptionsRequest actualRequest = (ListTopicSubscriptionsRequest) actualRequests.get(0);
    Assert.assertEquals(topic, actualRequest.getTopicAsTopicName());
}
Also used : ListTopicSubscriptionsPagedResponse(com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse) ListTopicSubscriptionsRequest(com.google.pubsub.v1.ListTopicSubscriptionsRequest) ByteString(com.google.protobuf.ByteString) SubscriptionName(com.google.pubsub.v1.SubscriptionName) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) ListTopicSubscriptionsResponse(com.google.pubsub.v1.ListTopicSubscriptionsResponse) TopicName(com.google.pubsub.v1.TopicName) Test(org.junit.Test)

Aggregations

TopicName (com.google.pubsub.v1.TopicName)28 Test (org.junit.Test)20 SubscriptionName (com.google.pubsub.v1.SubscriptionName)9 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)7 ByteString (com.google.protobuf.ByteString)7 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)7 ApiException (com.google.api.gax.grpc.ApiException)6 PubsubMessage (com.google.pubsub.v1.PubsubMessage)6 Subscription (com.google.pubsub.v1.Subscription)6 Topic (com.google.pubsub.v1.Topic)6 StatusRuntimeException (io.grpc.StatusRuntimeException)6 Publisher (com.google.cloud.pubsub.spi.v1.Publisher)4 SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)4 PushConfig (com.google.pubsub.v1.PushConfig)3 AckReplyConsumer (com.google.cloud.pubsub.spi.v1.AckReplyConsumer)2 MessageReceiver (com.google.cloud.pubsub.spi.v1.MessageReceiver)2 ListTopicSubscriptionsPagedResponse (com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse)2 Subscriber (com.google.cloud.pubsub.spi.v1.Subscriber)2 Policy (com.google.iam.v1.Policy)2 DeleteTopicRequest (com.google.pubsub.v1.DeleteTopicRequest)2