Search in sources :

Example 16 with Publisher

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;
}
Also used : RetrySettings(com.google.api.gax.retrying.RetrySettings) Duration(org.threeten.bp.Duration) Publisher(com.google.cloud.pubsub.spi.v1.Publisher)

Example 17 with 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;
}
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 18 with 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();
    }
}
Also used : Publisher(com.google.cloud.pubsub.spi.v1.Publisher) TopicName(com.google.pubsub.v1.TopicName)

Example 19 with Publisher

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);
}
Also used : Publisher(com.google.cloud.pubsub.v1.Publisher) PubsubMessage(com.google.pubsub.v1.PubsubMessage) Before(org.junit.Before)

Example 20 with Publisher

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();
}
Also used : Publisher(com.google.cloud.pubsub.v1.Publisher) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Aggregations

Publisher (com.google.cloud.pubsub.v1.Publisher)14 PubsubMessage (com.google.pubsub.v1.PubsubMessage)14 Publisher (com.google.cloud.pubsub.spi.v1.Publisher)9 Test (org.junit.Test)7 ByteString (com.google.protobuf.ByteString)5 TopicName (com.google.pubsub.v1.TopicName)4 IOException (java.io.IOException)4 ApiException (com.google.api.gax.rpc.ApiException)3 ExecutorProvider (com.google.api.gax.core.ExecutorProvider)2 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)2 DivolteSchema (io.divolte.server.DivolteSchema)2 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 ServletException (javax.servlet.ServletException)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