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