Search in sources :

Example 6 with Publisher

use of com.google.cloud.pubsub.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.v1.Publisher in project java-docs-samples by GoogleCloudPlatform.

the class PubSubPublish method doPost.

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    Publisher publisher = this.publisher;
    try {
        String topicId = System.getenv("PUBSUB_TOPIC");
        // create a publisher on the topic
        if (publisher == null) {
            publisher = Publisher.newBuilder(ProjectTopicName.of(ServiceOptions.getDefaultProjectId(), topicId)).build();
        }
        // construct a pubsub message from the payload
        final String payload = req.getParameter("payload");
        PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(payload)).build();
        publisher.publish(pubsubMessage);
        // redirect to home page
        resp.sendRedirect("/");
    } catch (Exception e) {
        resp.sendError(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
Also used : Publisher(com.google.cloud.pubsub.v1.Publisher) ByteString(com.google.protobuf.ByteString) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 8 with Publisher

use of com.google.cloud.pubsub.v1.Publisher in project divolte-collector by divolte.

the class GoogleCloudPubSubFlusherTest method testSingleMessageSentToPublisher.

@Test
public void testSingleMessageSentToPublisher() {
    // Process a single message.
    processSingleMessage();
    // Check it was forwarded to the publisher.
    final Publisher publisher = mockPublisher.orElseThrow(IllegalStateException::new);
    verify(publisher).publish(any(PubsubMessage.class));
    verifyNoMoreInteractions(publisher);
}
Also used : Publisher(com.google.cloud.pubsub.v1.Publisher) PubsubMessage(com.google.pubsub.v1.PubsubMessage) Test(org.junit.Test)

Example 9 with Publisher

use of com.google.cloud.pubsub.v1.Publisher in project divolte-collector by divolte.

the class GoogleCloudPubSubFlusherTest method processSingleMessage.

private void processSingleMessage(final Optional<Integer> confluentId) {
    final Publisher publisher = mockPublisher.orElseThrow(IllegalStateException::new);
    // Process a single message.
    final DivolteSchema schema = new DivolteSchema(MINIMAL_SCHEMA, confluentId);
    final GoogleCloudPubSubFlusher flusher = new GoogleCloudPubSubFlusher(publisher, schema);
    if (ItemProcessor.ProcessingDirective.PAUSE == flusher.process(itemFromAvroRecordBuffer(generateMessage()))) {
        flusher.heartbeat();
    }
}
Also used : Publisher(com.google.cloud.pubsub.v1.Publisher) DivolteSchema(io.divolte.server.DivolteSchema)

Example 10 with Publisher

use of com.google.cloud.pubsub.v1.Publisher in project divolte-collector by divolte.

the class GoogleCloudPubSubFlusherTest method testMessagesAreRetriedOnRetriableFailure.

@Test
public void testMessagesAreRetriedOnRetriableFailure() throws IOException {
    // Simulate a failure on the first send that indicates a retry should succeed.
    final Publisher publisher = mockPublisher.orElseThrow(IllegalStateException::new);
    when(publisher.publish(any(PubsubMessage.class))).thenReturn(failedFuture(new ApiException("simulated transient failure", new IOException(), GrpcStatusCode.of(Status.Code.INTERNAL), true))).thenAnswer(invocationOnMock -> completedFuture(String.valueOf(messageIdCounter++)));
    // Here we send the message.
    processSingleMessage();
    // Now we check the invocations…
    verify(publisher, times(2)).publish(any(PubsubMessage.class));
    verifyNoMoreInteractions(publisher);
}
Also used : Publisher(com.google.cloud.pubsub.v1.Publisher) IOException(java.io.IOException) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

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