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