Search in sources :

Example 21 with Publisher

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

the class GoogleCloudPubSubFlusherTest method testMessagesAreAbandonedOnNonRetriableFailure.

@Test
public void testMessagesAreAbandonedOnNonRetriableFailure() throws IOException {
    // Simulate a failure on send that indicates a retry isn't allowed.
    final Publisher publisher = mockPublisher.orElseThrow(IllegalStateException::new);
    when(publisher.publish(any(PubsubMessage.class))).thenReturn(failedFuture(new ApiException("simulated permanent failure", new IOException(), GrpcStatusCode.of(Status.Code.NOT_FOUND), false)));
    // Here we send the message.
    processSingleMessage();
    // Now we check the invocations…
    verify(publisher).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)

Example 22 with Publisher

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

the class PubSubPublishTest method servletPublishesPayloadMessage.

@Test
public void servletPublishesPayloadMessage() throws Exception {
    assertNotNull(System.getenv("PUBSUB_TOPIC"));
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getParameter("payload")).thenReturn("test-message");
    HttpServletResponse response = mock(HttpServletResponse.class);
    Publisher publisher = mock(Publisher.class);
    PubsubMessage message = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8("test-message")).build();
    when(publisher.publish(eq(message))).thenReturn(SettableApiFuture.create());
    PubSubPublish pubSubPublish = new PubSubPublish(publisher);
    // verify content of published test message
    pubSubPublish.doPost(request, response);
    verify(publisher, times(1)).publish(eq(message));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Publisher(com.google.cloud.pubsub.v1.Publisher) PubsubMessage(com.google.pubsub.v1.PubsubMessage) Test(org.junit.Test)

Example 23 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 {
    Publisher publisher = this.publisher;
    // construct a pubsub message from the payload
    final String payload = req.getParameter("payload");
    Message message = new Message(null);
    message.setData(payload);
    PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(payload)).putAttributes("sourceLang", req.getParameter("sourceLang")).putAttributes("targetLang", req.getParameter("targetLang")).build();
    String topicId = System.getenv("PUBSUB_TOPIC");
    // create a publisher on the topic
    if (publisher == null) {
        this.publisher = publisher = Publisher.newBuilder(ProjectTopicName.newBuilder().setProject(ServiceOptions.getDefaultProjectId()).setTopic(topicId).build()).build();
    }
    publisher.publish(pubsubMessage);
    // redirect to home page
    resp.sendRedirect("/");
}
Also used : PubsubMessage(com.google.pubsub.v1.PubsubMessage) Publisher(com.google.cloud.pubsub.v1.Publisher) ByteString(com.google.protobuf.ByteString) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Example 24 with Publisher

use of com.google.cloud.pubsub.v1.Publisher in project spring-cloud-gcp by spring-cloud.

the class DefaultPublisherFactoryTests method testGetPublisher.

@Test
public void testGetPublisher() {
    DefaultPublisherFactory factory = new DefaultPublisherFactory(() -> "projectId");
    factory.setCredentialsProvider(this.credentialsProvider);
    Publisher publisher = factory.createPublisher("testTopic");
    assertEquals(factory.getCache().size(), 1);
    assertEquals(publisher, factory.getCache().get("testTopic"));
    assertEquals("testTopic", ((ProjectTopicName) publisher.getTopicName()).getTopic());
    assertEquals("projectId", ((ProjectTopicName) publisher.getTopicName()).getProject());
}
Also used : Publisher(com.google.cloud.pubsub.v1.Publisher) 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