/** Converts an {@see ApiFuture} to a {@see ListenableFuture}. */ public static <T> ListenableFuture<T> apiFutureToListenableFuture(final ApiFuture<T> apiFuture) { SettableFuture<T> settableFuture = SettableFuture.create(); ApiFutures.addCallback( apiFuture, new ApiFutureCallback<T>() { @Override public void onFailure(Throwable t) { settableFuture.setException(t); } @Override public void onSuccess(T result) { settableFuture.set(result); } }); return settableFuture; }
/** * Queues the mutation to be batched and sent. Please note that the order of mutations is not * guaranteed. * * @throws IllegalStateException If this instance has been closed. */ public ApiFuture<Void> add(final RowMutation rowMutation) { Preconditions.checkState(!closed, "BulkMutations has been closed"); numOutstanding.incrementAndGet(); ApiFuture<Void> future = callable.futureCall(rowMutation); ApiFutures.addCallback( future, new ApiFutureCallback<Void>() { @Override public void onFailure(Throwable throwable) { onMutationComplete(false); } @Override public void onSuccess(Void aVoid) { onMutationComplete(true); } }, MoreExecutors.directExecutor()); return future; }
@Override public void onSuccess(Void ignored) { ApiFutures.addCallback( invokeUserCallback(), new ApiFutureCallback<T>() {
/** Example of publishing a message. */ // [TARGET publish(PubsubMessage)] // [VARIABLE "my_message"] public ApiFuture<String> publish(String message) { ByteString data = ByteString.copyFromUtf8(message); PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage); ApiFutures.addCallback( messageIdFuture, new ApiFutureCallback<String>() { public void onSuccess(String messageId) { System.out.println("published with message id: " + messageId); } public void onFailure(Throwable t) { System.out.println("failed to publish: " + t); } }, Executors.newSingleThreadExecutor()); return messageIdFuture; }
default: final ApiFuture<Void> writeFuture = writeAsync(logEntries, writeOptions); ApiFutures.addCallback( writeFuture, new ApiFutureCallback<Void>() {
ApiFutures.addCallback(response, ackHandler, MoreExecutors.directExecutor()); executor.execute( new Runnable() {
@Override public void sendAsync(RowMap r, AbstractAsyncProducer.CallbackCompleter cc) throws Exception { String message = r.toJSON(outputConfig); ByteString data = ByteString.copyFromUtf8(message); PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); if ( r instanceof DDLMap ) { ApiFuture<String> apiFuture = ddlPubsub.publish(pubsubMessage); PubsubCallback callback = new PubsubCallback(cc, r.getNextPosition(), message, this.succeededMessageCount, this.failedMessageCount, this.succeededMessageMeter, this.failedMessageMeter, this.context); ApiFutures.addCallback(apiFuture, callback); } else { ApiFuture<String> apiFuture = pubsub.publish(pubsubMessage); PubsubCallback callback = new PubsubCallback(cc, r.getNextPosition(), message, this.succeededMessageCount, this.failedMessageCount, this.succeededMessageMeter, this.failedMessageMeter, this.context); ApiFutures.addCallback(apiFuture, callback); } }
ApiFutures.addCallback( future, new ApiFutureCallback<String>() {
private void rejectTransaction(final Throwable throwable) { if (throwable instanceof ApiException) { span.setStatus(TraceUtil.statusFromApiException((ApiException) throwable)); } span.end(); if (transaction.isPending()) { ApiFutures.addCallback( transaction.rollback(), new ApiFutureCallback<Void>() { @Override public void onFailure(Throwable throwable) { resultFuture.setException(throwable); } @Override public void onSuccess(Void ignored) { resultFuture.setException(throwable); } }); } else { resultFuture.setException(throwable); } } });
ApiFutures.addCallback( errorFuture, new ApiFutureCallback<Void>() {
@Test public void readSingleNonexistentAsyncCallback() throws Exception { ApiFuture<Row> future = testEnvRule .env() .getDataClient() .readRowAsync(testEnvRule.env().getTableId(), "somenonexistentkey"); final AtomicBoolean found = new AtomicBoolean(); final CountDownLatch latch = new CountDownLatch(1); ApiFutures.addCallback( future, new ApiFutureCallback<Row>() { @Override public void onFailure(Throwable t) { latch.countDown(); } @Override public void onSuccess(Row result) { found.set(true); latch.countDown(); } }, MoreExecutors.directExecutor()); latch.await(1, TimeUnit.MINUTES); assertThat(found.get()).isTrue(); }
ApiFutures.addCallback( publisherStub.publishCallable().futureCall(publishRequest.build()), new ApiFutureCallback<PublishResponse>() {
.setAckDeadlineSeconds(modack.deadlineExtensionSeconds) .build()); ApiFutures.addCallback(future, loggingCallback); .addAllAckIds(idChunk) .build()); ApiFutures.addCallback(future, loggingCallback);
ImmutableMap.of("attemptsRemaining", AttributeValue.longAttributeValue(attemptsRemaining))); ApiFutures.addCallback( transaction.begin(), new ApiFutureCallback<Void>() {
@Override public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext inputContext) { GrpcCallContext context = GrpcCallContext.createDefault().nullToSelf(inputContext); ApiFuture<ResponseT> innerCallFuture = callable.futureCall(request, context); ExceptionTransformingFuture transformingFuture = new ExceptionTransformingFuture(innerCallFuture); ApiFutures.addCallback(innerCallFuture, transformingFuture, directExecutor()); return transformingFuture; }
@Override public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext inputContext) { GrpcCallContext context = GrpcCallContext.createDefault().nullToSelf(inputContext); ApiFuture<ResponseT> innerCallFuture = callable.futureCall(request, context); ExceptionTransformingFuture transformingFuture = new ExceptionTransformingFuture(innerCallFuture); ApiFutures.addCallback(innerCallFuture, transformingFuture, directExecutor()); return transformingFuture; }
@Override public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext inputContext) { HttpJsonCallContext context = HttpJsonCallContext.createDefault().nullToSelf(inputContext); ApiFuture<ResponseT> innerCallFuture = callable.futureCall(request, context); ExceptionTransformingFuture transformingFuture = new ExceptionTransformingFuture(innerCallFuture); ApiFutures.addCallback(innerCallFuture, transformingFuture, directExecutor()); return transformingFuture; }
private void publishPubSub(ByteString bytes) { final ApiFuture<String> publish = publisher.publish( PubsubMessage.newBuilder().setData(bytes).build()); ApiFutures.addCallback(publish, new ApiFutureCallback<String>() { @Override public void onFailure(Throwable t) { log.error("Failed sending metrics {}", t.getMessage()); } @Override public void onSuccess(String messageId) { } }, executorService); }
@Test public void testSuccess() { ApiFuture<String> future = ApiFutures.immediateFuture("result"); ApiFutures.addCallback( future, new TraceFinisher<String>(mockTracer), MoreExecutors.directExecutor()); verify(mockTracer, times(1)).operationSucceeded(); }
@Override public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext inputContext) { FakeCallContext context = FakeCallContext.createDefault().nullToSelf(inputContext); ApiFuture<ResponseT> innerCallFuture = callable.futureCall(request, context); ExceptionTransformingFuture transformingFuture = new ExceptionTransformingFuture(innerCallFuture); ApiFutures.addCallback(innerCallFuture, transformingFuture, directExecutor()); return transformingFuture; }