Search in sources :

Example 16 with TopicName

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

the class CreateTopicAndPublishMessages method publishMessages.

public static void publishMessages() throws Exception {
    // [START pubsub_publish]
    TopicName topicName = TopicName.create("my-project-id", "my-topic-id");
    Publisher publisher = null;
    List<ApiFuture<String>> messageIdFutures = new ArrayList<>();
    try {
        // Create a publisher instance with default settings bound to the topic
        publisher = Publisher.defaultBuilder(topicName).build();
        List<String> messages = Arrays.asList("first message", "second message");
        // schedule publishing one message at a time : messages get automatically batched
        for (String message : messages) {
            ByteString data = ByteString.copyFromUtf8(message);
            PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
            // Once published, returns a server-assigned message id (unique within the topic)
            ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
            messageIdFutures.add(messageIdFuture);
        }
    } finally {
        // wait on any pending publish requests.
        List<String> messageIds = ApiFutures.allAsList(messageIdFutures).get();
        for (String messageId : messageIds) {
            System.out.println("published with message ID: " + messageId);
        }
        if (publisher != null) {
            // When finished with the publisher, shutdown to free up resources.
            publisher.shutdown();
        }
    }
// [END pubsub_publish]
}
Also used : ApiFuture(com.google.api.core.ApiFuture) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) Publisher(com.google.cloud.pubsub.spi.v1.Publisher) ByteString(com.google.protobuf.ByteString) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName)

Example 17 with TopicName

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

the class SubscriptionAdminClientSnippets method createSubscriptionWithPushEndpoint.

/** Example of creating a subscription with a push endpoint. */
public Subscription createSubscriptionWithPushEndpoint(String topicId, String subscriptionId, String endpoint) throws Exception {
    // [START pubsub_create_push_subscription]
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        TopicName topicName = TopicName.create(projectId, topicId);
        SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
        // eg. endpoint = "https://my-test-project.appspot.com/push"
        PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build();
        // acknowledgement deadline in seconds for the message received over the push endpoint
        int ackDeadlineInSeconds = 10;
        Subscription subscription = subscriptionAdminClient.createSubscription(subscriptionName, topicName, pushConfig, ackDeadlineInSeconds);
        return subscription;
    }
// [END pubsub_create_push_subscription]
}
Also used : PushConfig(com.google.pubsub.v1.PushConfig) 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 18 with TopicName

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

the class TopicAdminClientSnippets method getTopic.

/** Example of getting a topic. */
public Topic getTopic(String topicId) throws Exception {
    // [START pubsub_get_topic]
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        TopicName topicName = TopicName.create(projectId, topicId);
        Topic topic = topicAdminClient.getTopic(topicName);
        return topic;
    }
// [END pubsub_get_topic]
}
Also used : TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) Topic(com.google.pubsub.v1.Topic) TopicName(com.google.pubsub.v1.TopicName)

Example 19 with TopicName

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

the class TopicAdminClientSnippets method createTopic.

/** Example of creating a topic. */
public Topic createTopic(String topicId) throws Exception {
    // [START pubsub_create_topic]
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        // projectId <=  unique project identifier, eg. "my-project-id"
        // topicId <= "my-topic-id"
        TopicName topicName = TopicName.create(projectId, topicId);
        Topic topic = topicAdminClient.createTopic(topicName);
        return topic;
    }
// [END pubsub_create_topic]
}
Also used : TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) Topic(com.google.pubsub.v1.Topic) TopicName(com.google.pubsub.v1.TopicName)

Example 20 with TopicName

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

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