Search in sources :

Example 21 with SettableListenableFuture

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;
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) PubSubDeliveryException(org.springframework.cloud.gcp.pubsub.core.PubSubDeliveryException)

Example 22 with SettableListenableFuture

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;
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) BasicAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.BasicAcknowledgeablePubsubMessage) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ConvertedAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage) AcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage) ConvertedBasicAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.converter.ConvertedBasicAcknowledgeablePubsubMessage)

Example 23 with SettableListenableFuture

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;
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) AsyncBatchAnnotateFilesResponse(com.google.cloud.vision.v1.AsyncBatchAnnotateFilesResponse) GoogleStorageLocation(org.springframework.cloud.gcp.storage.GoogleStorageLocation)

Example 24 with SettableListenableFuture

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;
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) Job(com.google.cloud.bigquery.Job) IOException(java.io.IOException)

Aggregations

SettableListenableFuture (org.springframework.util.concurrent.SettableListenableFuture)24 Test (org.junit.Test)8 List (java.util.List)3 BeanFactory (org.springframework.beans.factory.BeanFactory)3 MessagingException (org.springframework.messaging.MessagingException)3 PubsubMessage (com.google.pubsub.v1.PubsubMessage)2 URI (java.net.URI)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Transformer (javax.xml.transform.Transformer)2 TransformerFactory (javax.xml.transform.TransformerFactory)2 Log (org.apache.commons.logging.Log)2 AcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage)2 BasicAcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.BasicAcknowledgeablePubsubMessage)2 ConvertedAcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage)2 ConvertedBasicAcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.converter.ConvertedBasicAcknowledgeablePubsubMessage)2 MethodParameter (org.springframework.core.MethodParameter)2 Message (org.springframework.messaging.Message)2 MessageHandlingException (org.springframework.messaging.MessageHandlingException)2 ErrorMessage (org.springframework.messaging.support.ErrorMessage)2