Search in sources :

Example 6 with Publisher

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

the class PublisherSnippets method getSingleThreadedPublisher.

public Publisher getSingleThreadedPublisher(TopicName topicName) throws Exception {
    // [START pubsub_publisher_single_threaded]
    // create a publisher with a single threaded executor
    ExecutorProvider executorProvider = InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(1).build();
    Publisher publisher = Publisher.defaultBuilder(topicName).setExecutorProvider(executorProvider).build();
    // [END pubsub_publisher_single_threaded]
    return publisher;
}
Also used : Publisher(com.google.cloud.pubsub.spi.v1.Publisher) ExecutorProvider(com.google.api.gax.grpc.ExecutorProvider) InstantiatingExecutorProvider(com.google.api.gax.grpc.InstantiatingExecutorProvider)

Example 7 with Publisher

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

the class PublisherImplTest method testPublisherGetters.

@Test
public void testPublisherGetters() throws Exception {
    Publisher.Builder builder = Publisher.defaultBuilder(TEST_TOPIC);
    builder.setChannelProvider(TEST_CHANNEL_PROVIDER);
    builder.setExecutorProvider(SINGLE_THREAD_EXECUTOR);
    builder.setBatchingSettings(BatchingSettings.newBuilder().setRequestByteThreshold(10L).setDelayThreshold(Duration.ofMillis(11)).setElementCountThreshold(12L).build());
    builder.setCredentialsProvider(NO_CREDENTIALS_PROVIDER);
    builder.setFlowControlSettings(FlowControlSettings.newBuilder().setMaxOutstandingRequestBytes(13).setMaxOutstandingElementCount(14).setLimitExceededBehavior(LimitExceededBehavior.ThrowException).build());
    Publisher publisher = builder.build();
    assertEquals(TEST_TOPIC, publisher.getTopicName());
    assertEquals(10, (long) publisher.getBatchingSettings().getRequestByteThreshold());
    assertEquals(Duration.ofMillis(11), publisher.getBatchingSettings().getDelayThreshold());
    assertEquals(12, (long) publisher.getBatchingSettings().getElementCountThreshold());
    assertEquals(13, (long) publisher.getFlowControlSettings().getMaxOutstandingRequestBytes());
    assertEquals(14, (long) publisher.getFlowControlSettings().getMaxOutstandingElementCount());
    assertEquals(LimitExceededBehavior.ThrowException, publisher.getFlowControlSettings().getLimitExceededBehavior());
    publisher.shutdown();
}
Also used : Builder(com.google.cloud.pubsub.spi.v1.Publisher.Builder) Test(org.junit.Test)

Example 8 with Publisher

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

the class PublisherSnippets method getPublisherWithCustomRetrySettings.

public Publisher getPublisherWithCustomRetrySettings(TopicName topicName) throws Exception {
    // [START pubsub_publisher_retry_settings]
    // Retry settings control how the publisher handles retryable failures
    // default : 1 ms
    Duration retryDelay = Duration.ofMillis(100);
    // back off for repeated failures
    double retryDelayMultiplier = 2.0;
    // default : 10 seconds
    Duration maxRetryDelay = Duration.ofSeconds(5);
    RetrySettings retrySettings = RetrySettings.newBuilder().setInitialRetryDelay(retryDelay).setRetryDelayMultiplier(retryDelayMultiplier).setMaxRetryDelay(maxRetryDelay).build();
    Publisher publisher = Publisher.defaultBuilder(topicName).setRetrySettings(retrySettings).build();
    // [END pubsub_publisher_retry_settings]
    return publisher;
}
Also used : RetrySettings(com.google.api.gax.retrying.RetrySettings) Duration(org.threeten.bp.Duration) Publisher(com.google.cloud.pubsub.spi.v1.Publisher)

Example 9 with Publisher

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

the class PublisherSnippets method createPublisherWithCustomCredentials.

private Publisher createPublisherWithCustomCredentials(TopicName topicName) throws Exception {
    // [START pubsub_publisher_custom_credentials]
    // read service account credentials from file
    CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream("credentials.json")));
    ChannelProvider channelProvider = TopicAdminSettings.defaultChannelProviderBuilder().setCredentialsProvider(credentialsProvider).build();
    Publisher publisher = Publisher.defaultBuilder(topicName).setChannelProvider(channelProvider).build();
    // [END pubsub_publisher_custom_credentials]
    return publisher;
}
Also used : ChannelProvider(com.google.api.gax.grpc.ChannelProvider) FixedCredentialsProvider(com.google.api.gax.core.FixedCredentialsProvider) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) Publisher(com.google.cloud.pubsub.spi.v1.Publisher) FileInputStream(java.io.FileInputStream)

Example 10 with Publisher

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

the class PublisherSnippets method newBuilder.

/** Example of creating a {@code Publisher}. */
// [TARGET newBuilder(TopicName)]
// [VARIABLE "my_project"]
// [VARIABLE "my_topic"]
public static void newBuilder(String projectId, String topicId) throws Exception {
    TopicName topic = TopicName.create(projectId, topicId);
    Publisher publisher = Publisher.defaultBuilder(topic).build();
    try {
    // ...
    } finally {
        // When finished with the publisher, make sure to shutdown to free up resources.
        publisher.shutdown();
    }
}
Also used : Publisher(com.google.cloud.pubsub.spi.v1.Publisher) TopicName(com.google.pubsub.v1.TopicName)

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