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;
}
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();
}
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;
}
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;
}
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();
}
}
Aggregations