Search in sources :

Example 6 with ApiException

use of com.google.api.gax.rpc.ApiException 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

ApiException (com.google.api.gax.rpc.ApiException)6 PubsubMessage (com.google.pubsub.v1.PubsubMessage)4 Publisher (com.google.cloud.pubsub.v1.Publisher)3 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)3 ByteString (com.google.protobuf.ByteString)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 ApiFuture (com.google.api.core.ApiFuture)1 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)1 TopicAdminClient (com.google.cloud.pubsub.v1.TopicAdminClient)1 ImmutableList (com.google.common.collect.ImmutableList)1 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)1 Subscription (com.google.pubsub.v1.Subscription)1 ExecutionException (java.util.concurrent.ExecutionException)1