use of com.google.cloud.pubsub.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.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.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();
}
}
use of com.google.cloud.pubsub.v1.Publisher in project divolte-collector by divolte.
the class GoogleCloudPubSubFlusherTest method resetMessages.
@Before
public void resetMessages() {
final long now = System.currentTimeMillis();
partyId = Optional.of(DivolteIdentifier.generate(now));
sessionId = Optional.of(DivolteIdentifier.generate(now));
generatedEventCounter = 0;
messageIdCounter = 0;
final Publisher mockPublisher = mock(Publisher.class);
when(mockPublisher.publish(any(PubsubMessage.class))).thenAnswer(invocationOnMock -> completedFuture(String.valueOf(messageIdCounter++)));
this.mockPublisher = Optional.of(mockPublisher);
}
use of com.google.cloud.pubsub.v1.Publisher in project divolte-collector by divolte.
the class GoogleCloudPubSubFlusherTest method getFirstPublishedMessage.
private PubsubMessage getFirstPublishedMessage() {
final Publisher publisher = mockPublisher.orElseThrow(IllegalStateException::new);
final ArgumentCaptor<PubsubMessage> argumentCaptor = ArgumentCaptor.forClass(PubsubMessage.class);
verify(publisher).publish(argumentCaptor.capture());
return argumentCaptor.getValue();
}
Aggregations