use of com.google.api.core.ApiFuture in project google-cloud-java by GoogleCloudPlatform.
the class CreateTopicAndPublishMessages method publishMessages.
public static void publishMessages() throws Exception {
// [START pubsub_publish]
TopicName topicName = TopicName.create("my-project-id", "my-topic-id");
Publisher publisher = null;
List<ApiFuture<String>> messageIdFutures = new ArrayList<>();
try {
// Create a publisher instance with default settings bound to the topic
publisher = Publisher.defaultBuilder(topicName).build();
List<String> messages = Arrays.asList("first message", "second message");
// schedule publishing one message at a time : messages get automatically batched
for (String message : messages) {
ByteString data = ByteString.copyFromUtf8(message);
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
// Once published, returns a server-assigned message id (unique within the topic)
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
messageIdFutures.add(messageIdFuture);
}
} finally {
// wait on any pending publish requests.
List<String> messageIds = ApiFutures.allAsList(messageIdFutures).get();
for (String messageId : messageIds) {
System.out.println("published with message ID: " + messageId);
}
if (publisher != null) {
// When finished with the publisher, shutdown to free up resources.
publisher.shutdown();
}
}
// [END pubsub_publish]
}
use of com.google.api.core.ApiFuture in project java-docs-samples by GoogleCloudPlatform.
the class QueryDataSnippets method prepareExamples.
/**
* Creates cities collection and add sample documents to test queries.
*
* @return collection reference
*/
void prepareExamples() throws Exception {
// [START fs_query_create_examples]
CollectionReference cities = db.collection("cities");
List<ApiFuture<WriteResult>> futures = new ArrayList<>();
futures.add(cities.document("SF").set(new City("San Francisco", "CA", "USA", false, 860000L)));
futures.add(cities.document("LA").set(new City("Los Angeles", "CA", "USA", false, 3900000L)));
futures.add(cities.document("DC").set(new City("Washington D.C.", null, "USA", true, 680000L)));
futures.add(cities.document("TOK").set(new City("Tokyo", null, "Japan", true, 9000000L)));
futures.add(cities.document("BJ").set(new City("Beijing", null, "China", true, 21500000L)));
// (optional) block on documents successfully added
ApiFutures.allAsList(futures).get();
// [END fs_query_create_examples]
}
use of com.google.api.core.ApiFuture in project divolte-collector by divolte.
the class GoogleCloudPubSubFlusher method sendBatch.
@Override
protected ImmutableList<PubsubMessage> sendBatch(final List<PubsubMessage> batch) throws InterruptedException {
// For Pub/Sub we assume the following:
// - Batching behaviour is set to flush everything ASAP.
// - Retry behaviour will retry indefinitely, so long as it seems likely to succeed.
// First start sending the messages.
// (This will serialize them, determine the partition and then assign them to a per-partition buffer.)
final int batchSize = batch.size();
final List<ApiFuture<String>> sendResults = batch.stream().map(publisher::publish).collect(Collectors.toCollection(() -> new ArrayList<>(batchSize)));
// At this point the messages are in flight, and we assume being flushed.
// When they eventually complete, each message can be in one of several states:
// - Completed.
// - An error occurred, but a retry may succeed.
// - A fatal error occurred.
final ImmutableList.Builder<PubsubMessage> remaining = ImmutableList.builder();
for (int i = 0; i < batchSize; ++i) {
final ApiFuture<String> pendingResult = sendResults.get(i);
try {
final String messageId = pendingResult.get();
if (logger.isDebugEnabled()) {
final PubsubMessage message = batch.get(i);
logger.debug("Finished sending event (partyId={}) to Pub/Sub: messageId = {}", message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID), messageId);
}
} catch (final ExecutionException e) {
final PubsubMessage message = batch.get(i);
// The Pub/Sub publisher internally has a retry policy, but outside that we also
// retry indefinitely unless it's a cause that we don't understand.
final Throwable cause = e.getCause();
if (cause instanceof ApiException) {
final ApiException apiException = (ApiException) cause;
if (apiException.isRetryable()) {
if (logger.isDebugEnabled()) {
logger.debug("Transient error sending event (partyId=" + message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID) + ") to Pub/Sub; retrying.", cause);
}
remaining.add(message);
} else {
logger.warn("Permanent error sending event (partyId=" + message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID) + ") to Pub/Sub; abandoning.", cause);
}
} else {
logger.error("Unknown error sending event (partyId=" + message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID) + ") to Pub/Sub; abandoning.", cause);
}
}
}
return remaining.build();
}
use of com.google.api.core.ApiFuture in project java-docs-samples by GoogleCloudPlatform.
the class RetrieveDataSnippets method prepareExamples.
/**
* Create cities collection and add sample documents.
*/
void prepareExamples() throws Exception {
// [START fs_retrieve_create_examples]
CollectionReference cities = db.collection("cities");
List<ApiFuture<WriteResult>> futures = new ArrayList<>();
futures.add(cities.document("SF").set(new City("San Francisco", "CA", "USA", false, 860000L)));
futures.add(cities.document("LA").set(new City("Los Angeles", "CA", "USA", false, 3900000L)));
futures.add(cities.document("DC").set(new City("Washington D.C.", null, "USA", true, 680000L)));
futures.add(cities.document("TOK").set(new City("Tokyo", null, "Japan", true, 9000000L)));
futures.add(cities.document("BJ").set(new City("Beijing", null, "China", true, 21500000L)));
// (optional) block on operation
ApiFutures.allAsList(futures).get();
// [END fs_retrieve_create_examples]
}
use of com.google.api.core.ApiFuture in project beam by apache.
the class TestPubsub method publish.
/**
* Publish messages to {@link #topicPath()}.
*/
public void publish(List<PubsubMessage> messages) {
Preconditions.checkNotNull(eventsTopicPath);
Publisher eventPublisher;
try {
eventPublisher = Publisher.newBuilder(eventsTopicPath.getPath()).setCredentialsProvider(pipelineOptions::getGcpCredential).setChannelProvider(channelProvider).setEndpoint(pubsubEndpoint).build();
} catch (IOException e) {
throw new RuntimeException("Error creating event publisher", e);
}
List<ApiFuture<String>> futures = messages.stream().map((message) -> {
com.google.pubsub.v1.PubsubMessage.Builder builder = com.google.pubsub.v1.PubsubMessage.newBuilder().setData(ByteString.copyFrom(message.getPayload())).putAllAttributes(message.getAttributeMap());
return eventPublisher.publish(builder.build());
}).collect(Collectors.toList());
try {
ApiFutures.allAsList(futures).get();
} catch (ExecutionException e) {
throw new RuntimeException("Error publishing a test message", e);
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while waiting for messages to publish", e);
}
eventPublisher.shutdown();
}
Aggregations