Search in sources :

Example 6 with ProjectTopicName

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

the class PublisherExample method main.

/**
 * Publish messages to a topic.
 * @param args topic name, number of messages
 */
public static void main(String... args) throws Exception {
    // topic id, eg. "my-topic"
    String topicId = args[0];
    int messageCount = Integer.parseInt(args[1]);
    ProjectTopicName topicName = ProjectTopicName.of(PROJECT_ID, topicId);
    Publisher publisher = null;
    try {
        // Create a publisher instance with default settings bound to the topic
        publisher = Publisher.newBuilder(topicName).build();
        for (int i = 0; i < messageCount; i++) {
            String message = "message-" + i;
            // convert message to bytes
            ByteString data = ByteString.copyFromUtf8(message);
            PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
            // schedule a message to be published, messages are automatically batched
            ApiFuture<String> future = publisher.publish(pubsubMessage);
            // add an asynchronous callback to handle success / failure
            ApiFutures.addCallback(future, new ApiFutureCallback<String>() {

                @Override
                public void onFailure(Throwable throwable) {
                    if (throwable instanceof ApiException) {
                        ApiException apiException = ((ApiException) throwable);
                        // details on the API exception
                        System.out.println(apiException.getStatusCode().getCode());
                        System.out.println(apiException.isRetryable());
                    }
                    System.out.println("Error publishing message : " + message);
                }

                @Override
                public void onSuccess(String messageId) {
                    // Once published, returns server-assigned message ids (unique within the topic)
                    System.out.println(messageId);
                }
            });
        }
    } finally {
        if (publisher != null) {
            // When finished with the publisher, shutdown to free up resources.
            publisher.shutdown();
        }
    }
}
Also used : ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) Publisher(com.google.cloud.pubsub.v1.Publisher) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ApiException(com.google.api.gax.rpc.ApiException)

Example 7 with ProjectTopicName

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

use of com.google.pubsub.v1.ProjectTopicName 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

ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)7 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)4 ApiException (com.google.api.gax.rpc.ApiException)3 ByteString (com.google.protobuf.ByteString)3 SettableApiFuture (com.google.api.core.SettableApiFuture)2 ServiceOptions (com.google.cloud.ServiceOptions)2 DlpServiceClient (com.google.cloud.dlp.v2.DlpServiceClient)2 Publisher (com.google.cloud.pubsub.v1.Publisher)2 Subscriber (com.google.cloud.pubsub.v1.Subscriber)2 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)2 Action (com.google.privacy.dlp.v2.Action)2 BigQueryTable (com.google.privacy.dlp.v2.BigQueryTable)2 CreateDlpJobRequest (com.google.privacy.dlp.v2.CreateDlpJobRequest)2 DlpJob (com.google.privacy.dlp.v2.DlpJob)2 GetDlpJobRequest (com.google.privacy.dlp.v2.GetDlpJobRequest)2 InfoType (com.google.privacy.dlp.v2.InfoType)2 ProjectName (com.google.privacy.dlp.v2.ProjectName)2 PubsubMessage (com.google.pubsub.v1.PubsubMessage)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2