Search in sources :

Example 1 with Publisher

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

the class ITPubSubTest method testPublishSubscribe.

@Test
public void testPublishSubscribe() throws Exception {
    TopicName topicName = TopicName.create(projectId, formatForTest("testing-publish-subscribe-topic"));
    SubscriptionName subscriptionName = SubscriptionName.create(projectId, formatForTest("testing-publish-subscribe-subscription"));
    topicAdminClient.createTopic(topicName);
    subscriptionAdminClient.createSubscription(subscriptionName, topicName, PushConfig.newBuilder().build(), 10);
    PubsubMessage message = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8("my message")).build();
    final SettableApiFuture<PubsubMessage> received = SettableApiFuture.create();
    Subscriber subscriber = Subscriber.defaultBuilder(subscriptionName, new MessageReceiver() {

        @Override
        public void receiveMessage(final PubsubMessage message, final AckReplyConsumer consumer) {
            if (received.set(message)) {
                consumer.ack();
            } else {
                consumer.nack();
            }
        }
    }).build();
    subscriber.addListener(new Subscriber.Listener() {

        public void failed(Subscriber.State from, Throwable failure) {
            received.setException(failure);
        }
    }, MoreExecutors.directExecutor());
    subscriber.startAsync();
    Publisher publisher = Publisher.defaultBuilder(topicName).build();
    publisher.publish(message).get();
    publisher.shutdown();
    assertEquals(received.get().getData(), message.getData());
    subscriber.stopAsync().awaitTerminated();
    subscriptionAdminClient.deleteSubscription(subscriptionName);
    topicAdminClient.deleteTopic(topicName);
}
Also used : Subscriber(com.google.cloud.pubsub.spi.v1.Subscriber) MessageReceiver(com.google.cloud.pubsub.spi.v1.MessageReceiver) SubscriptionName(com.google.pubsub.v1.SubscriptionName) Publisher(com.google.cloud.pubsub.spi.v1.Publisher) AckReplyConsumer(com.google.cloud.pubsub.spi.v1.AckReplyConsumer) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName) Test(org.junit.Test)

Example 2 with Publisher

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

the class ITPubSubSnippets method testPublisherSubscriberHelper.

private void testPublisherSubscriberHelper(TopicName topicName, SubscriptionName subscriptionName) throws Exception {
    String messageToPublish = "my-message";
    Publisher publisher = null;
    try {
        publisher = Publisher.defaultBuilder(topicName).build();
        PublisherSnippets snippets = new PublisherSnippets(publisher);
        final SettableApiFuture<Void> done = SettableApiFuture.create();
        ApiFutures.addCallback(snippets.publish(messageToPublish), new ApiFutureCallback<String>() {

            public void onSuccess(String messageId) {
                done.set(null);
            }

            public void onFailure(Throwable t) {
                done.setException(t);
            }
        });
        done.get();
    } finally {
        if (publisher != null) {
            publisher.shutdown();
        }
    }
    final BlockingQueue<PubsubMessage> queue = new ArrayBlockingQueue<>(1);
    final SettableApiFuture<Void> done = SettableApiFuture.create();
    final SettableApiFuture<PubsubMessage> received = SettableApiFuture.create();
    SubscriberSnippets snippets = new SubscriberSnippets(subscriptionName, new MessageReceiverSnippets(queue).messageReceiver(), done, MoreExecutors.directExecutor());
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                received.set(queue.poll(10, TimeUnit.MINUTES));
            } catch (InterruptedException e) {
                received.set(null);
            }
            // signal the subscriber to clean up
            done.set(null);
        }
    }).start();
    // blocks until done is set
    snippets.startAndWait();
    PubsubMessage message = received.get();
    assertNotNull(message);
    assertEquals(message.getData().toStringUtf8(), messageToPublish);
}
Also used : Publisher(com.google.cloud.pubsub.spi.v1.Publisher) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue)

Example 3 with Publisher

use of com.google.cloud.pubsub.spi.v1.Publisher 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 4 with Publisher

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

the class PublisherSnippets method getPublisherWithCustomBatchSettings.

public Publisher getPublisherWithCustomBatchSettings(TopicName topicName) throws Exception {
    // [START pubsub_publisher_batch_settings]
    // Batch settings control how the publisher batches messages
    // default : 1kb
    long requestBytesThreshold = 5000L;
    // default : 100
    long messageCountBatchSize = 10L;
    // default : 1 ms
    Duration publishDelayThreshold = Duration.ofMillis(100);
    // Publish request get triggered based on request size, messages count & time since last publish
    BatchingSettings batchingSettings = BatchingSettings.newBuilder().setElementCountThreshold(messageCountBatchSize).setRequestByteThreshold(requestBytesThreshold).setDelayThreshold(publishDelayThreshold).build();
    Publisher publisher = Publisher.defaultBuilder(topicName).setBatchingSettings(batchingSettings).build();
    // [END pubsub_publisher_batch_settings]
    return publisher;
}
Also used : Duration(org.threeten.bp.Duration) Publisher(com.google.cloud.pubsub.spi.v1.Publisher) BatchingSettings(com.google.api.gax.batching.BatchingSettings)

Example 5 with Publisher

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

the class PublisherSnippets method getPublisherWithCustomFlowControlSettings.

public Publisher getPublisherWithCustomFlowControlSettings(TopicName topicName) throws Exception {
    // [START pubsub_publisher_flow_settings]
    // Flow control settings restrict the number of outstanding publish requests
    int maxOutstandingBatches = 20;
    int maxOutstandingRequestBytes = 500000;
    // override behavior on limits exceeded if needed, default behavior is to block
    LimitExceededBehavior limitExceededBehavior = LimitExceededBehavior.ThrowException;
    FlowControlSettings flowControlSettings = FlowControlSettings.newBuilder().setMaxOutstandingElementCount(maxOutstandingBatches).setMaxOutstandingRequestBytes(maxOutstandingRequestBytes).setLimitExceededBehavior(limitExceededBehavior).build();
    Publisher publisher = Publisher.defaultBuilder(topicName).setFlowControlSettings(flowControlSettings).build();
    // [END pubsub_publisher_flow_settings]
    return publisher;
}
Also used : FlowControlSettings(com.google.api.gax.batching.FlowControlSettings) Publisher(com.google.cloud.pubsub.spi.v1.Publisher) LimitExceededBehavior(com.google.api.gax.batching.FlowController.LimitExceededBehavior)

Aggregations

Publisher (com.google.cloud.pubsub.spi.v1.Publisher)9 PubsubMessage (com.google.pubsub.v1.PubsubMessage)3 TopicName (com.google.pubsub.v1.TopicName)3 Test (org.junit.Test)2 Duration (org.threeten.bp.Duration)2 ApiFuture (com.google.api.core.ApiFuture)1 BatchingSettings (com.google.api.gax.batching.BatchingSettings)1 FlowControlSettings (com.google.api.gax.batching.FlowControlSettings)1 LimitExceededBehavior (com.google.api.gax.batching.FlowController.LimitExceededBehavior)1 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)1 FixedCredentialsProvider (com.google.api.gax.core.FixedCredentialsProvider)1 ChannelProvider (com.google.api.gax.grpc.ChannelProvider)1 ExecutorProvider (com.google.api.gax.grpc.ExecutorProvider)1 InstantiatingExecutorProvider (com.google.api.gax.grpc.InstantiatingExecutorProvider)1 RetrySettings (com.google.api.gax.retrying.RetrySettings)1 AckReplyConsumer (com.google.cloud.pubsub.spi.v1.AckReplyConsumer)1 MessageReceiver (com.google.cloud.pubsub.spi.v1.MessageReceiver)1 Builder (com.google.cloud.pubsub.spi.v1.Publisher.Builder)1 Subscriber (com.google.cloud.pubsub.spi.v1.Subscriber)1 ByteString (com.google.protobuf.ByteString)1