Search in sources :

Example 31 with TopicAdminClient

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

the class CreateTopicExample method main.

/**
 * Create a topic.
 *
 * @param args topicId
 * @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];
    // Create a new topic
    ProjectTopicName topic = ProjectTopicName.of(projectId, topicId);
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        topicAdminClient.createTopic(topic);
    } catch (ApiException e) {
        // example : code = ALREADY_EXISTS(409) implies topic already exists
        System.out.print(e.getStatusCode().getCode());
        System.out.print(e.isRetryable());
    }
    System.out.printf("Topic %s:%s created.\n", topic.getProject(), topic.getTopic());
}
Also used : TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) ApiException(com.google.api.gax.rpc.ApiException)

Aggregations

TopicAdminClient (com.google.cloud.pubsub.v1.TopicAdminClient)19 Test (org.junit.Test)18 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)12 TopicName (com.google.pubsub.v1.TopicName)8 Topic (com.google.pubsub.v1.Topic)5 Binding (com.google.iam.v1.Binding)2 Policy (com.google.iam.v1.Policy)2 TestIamPermissionsResponse (com.google.iam.v1.TestIamPermissionsResponse)2 SubscriptionName (com.google.pubsub.v1.SubscriptionName)2 LinkedList (java.util.LinkedList)2 ApiException (com.google.api.gax.rpc.ApiException)1 ListTopicSubscriptionsPagedResponse (com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse)1 ListTopicsPagedResponse (com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse)1 SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)1 ListTopicSubscriptionsRequest (com.google.pubsub.v1.ListTopicSubscriptionsRequest)1 ListTopicsRequest (com.google.pubsub.v1.ListTopicsRequest)1 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)1 CallOptions (io.grpc.CallOptions)1 Channel (io.grpc.Channel)1 ClientInterceptor (io.grpc.ClientInterceptor)1