use of org.springframework.util.concurrent.SettableListenableFuture in project spring-cloud-gcp by spring-cloud.
the class PubSubPublisherTemplate method publish.
@Override
public ListenableFuture<String> publish(final String topic, PubsubMessage pubsubMessage) {
Assert.hasText(topic, "The topic can't be null or empty.");
Assert.notNull(pubsubMessage, "The pubsubMessage can't be null.");
ApiFuture<String> publishFuture = this.publisherFactory.createPublisher(topic).publish(pubsubMessage);
final SettableListenableFuture<String> settableFuture = new SettableListenableFuture<>();
ApiFutures.addCallback(publishFuture, new ApiFutureCallback<String>() {
@Override
public void onFailure(Throwable throwable) {
String errorMessage = "Publishing to " + topic + " topic failed.";
LOGGER.warn(errorMessage, throwable);
PubSubDeliveryException pubSubDeliveryException = new PubSubDeliveryException(pubsubMessage, errorMessage, throwable);
settableFuture.setException(pubSubDeliveryException);
}
@Override
public void onSuccess(String result) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Publishing to " + topic + " was successful. Message ID: " + result);
}
settableFuture.set(result);
}
});
return settableFuture;
}
use of org.springframework.util.concurrent.SettableListenableFuture in project spring-cloud-gcp by spring-cloud.
the class PubSubSubscriberTemplate method pullNextAsync.
@Override
public ListenableFuture<PubsubMessage> pullNextAsync(String subscription) {
final SettableListenableFuture<PubsubMessage> settableFuture = new SettableListenableFuture<>();
this.pullAndAckAsync(subscription, 1, true).addCallback(messages -> {
PubsubMessage message = messages.isEmpty() ? null : messages.get(0);
settableFuture.set(message);
}, settableFuture::setException);
return settableFuture;
}
use of org.springframework.util.concurrent.SettableListenableFuture in project spring-cloud-gcp by spring-cloud.
the class DocumentOcrTemplate method extractOcrResultFuture.
private ListenableFuture<DocumentOcrResultSet> extractOcrResultFuture(OperationFuture<AsyncBatchAnnotateFilesResponse, OperationMetadata> grpcFuture) {
SettableListenableFuture<DocumentOcrResultSet> result = new SettableListenableFuture<>();
ApiFutures.addCallback(grpcFuture, new ApiFutureCallback<AsyncBatchAnnotateFilesResponse>() {
@Override
public void onFailure(Throwable throwable) {
result.setException(throwable);
}
@Override
public void onSuccess(AsyncBatchAnnotateFilesResponse asyncBatchAnnotateFilesResponse) {
String outputLocationUri = asyncBatchAnnotateFilesResponse.getResponsesList().get(0).getOutputConfig().getGcsDestination().getUri();
GoogleStorageLocation outputFolderLocation = new GoogleStorageLocation(outputLocationUri);
result.set(readOcrOutputFileSet(outputFolderLocation));
}
}, this.executor);
return result;
}
use of org.springframework.util.concurrent.SettableListenableFuture in project spring-cloud-gcp by spring-cloud.
the class BigQueryTemplate method createJobFuture.
private SettableListenableFuture<Job> createJobFuture(Job pendingJob) {
// Prepare the polling task for the ListenableFuture result returned to end-user
SettableListenableFuture<Job> result = new SettableListenableFuture<>();
ScheduledFuture<?> scheduledFuture = taskScheduler.scheduleAtFixedRate(() -> {
try {
Job job = pendingJob.reload();
if (State.DONE.equals(job.getStatus().getState())) {
if (job.getStatus().getError() != null) {
result.setException(new BigQueryException(job.getStatus().getError().getMessage()));
} else {
result.set(job);
}
}
} catch (Exception e) {
result.setException(new BigQueryException(e.getMessage()));
}
}, this.jobPollInterval);
result.addCallback(response -> scheduledFuture.cancel(true), response -> {
pendingJob.cancel();
scheduledFuture.cancel(true);
});
return result;
}
Aggregations