public static <T> void copyResults(CompletableFuture<T> source, CompletableFuture<T> target) { source.whenComplete((o, throwable) -> { if (throwable != null) { target.completeExceptionally(throwable); return; } target.complete(o); }); }
@Override public void onResponse(Call<R> call, Response<R> response) { if (response.isSuccessful()) { future.complete(response.body()); } else { future.completeExceptionally(new HttpException(response)); } }
private <T> CompletableFuture<T> wrap(CompletableFuture<T> future) { CompletableFuture<T> asyncFuture = new CompletableFuture<>(); future.whenCompleteAsync((r, e) -> { if (e != null) { asyncFuture.completeExceptionally(e); } else { asyncFuture.complete(r); } }, pool); return asyncFuture; }
@Override public CompletableFuture<Void> abort() { TransactionId transactionId = this.transactionId; if (transactionId == null) { return CompletableFuture.completedFuture(null); } CompletableFuture<Void> future = new CompletableFuture<>(); transactionService.complete(transactionId).whenComplete((completeResult, completeError) -> Futures.allOf(participants.stream().map(TransactionParticipant::close).collect(Collectors.toList())) .whenComplete((closeResult, closeError) -> { if (completeError != null) { future.completeExceptionally(completeError); } else if (closeError != null) { future.completeExceptionally(closeError); } else { future.complete(null); } })); return future; }
@Override public CompletableFuture<Boolean> isTableDisabled(TableName tableName) { if (TableName.isMetaTableName(tableName)) { return CompletableFuture.completedFuture(false); } CompletableFuture<Boolean> future = new CompletableFuture<>(); addListener(AsyncMetaTableAccessor.getTableState(metaTable, tableName), (state, error) -> { if (error != null) { future.completeExceptionally(error); return; } if (state.isPresent()) { future.complete(state.get().inStates(TableState.State.DISABLED)); } else { future.completeExceptionally(new TableNotFoundException(tableName)); } }); return future; }
private void executeActionAsync( final CompletableFuture<KvStateResponse> result, final KvStateRequest request, final boolean update) { if (!result.isDone()) { final CompletableFuture<KvStateResponse> operationFuture = getState(request, update); operationFuture.whenCompleteAsync( (t, throwable) -> { if (throwable != null) { if ( throwable.getCause() instanceof UnknownKvStateIdException || throwable.getCause() instanceof UnknownKvStateKeyGroupLocationException || throwable.getCause() instanceof ConnectException ) { // These failures are likely to be caused by out-of-sync // KvStateLocation. Therefore we retry this query and // force look up the location. LOG.debug("Retrying after failing to retrieve state due to: {}.", throwable.getCause().getMessage()); executeActionAsync(result, request, true); } else { result.completeExceptionally(throwable); } } else { result.complete(t); } }, queryExecutor); result.whenComplete( (t, throwable) -> operationFuture.cancel(false)); } }
@SuppressWarnings("unchecked") public static void linkFuturesOnError(CompletableFuture source, CompletableFuture target) { if (source != null && (!source.isDone() || source.isCompletedExceptionally())) { ((CompletableFuture<Object>) source).whenComplete((r, e) -> { if (e != null) { target.completeExceptionally(e); } }); } }
@Override public void onResponse(Call<R> call, Response<R> response) { if (response.isSuccessful()) { future.complete(response.body()); } else { future.completeExceptionally(new HttpException(response)); } }
public static <U> CompletableFuture<List<U>> each(List<CompletableFuture<U>> futures) { CompletableFuture<List<U>> overallResult = new CompletableFuture<>(); CompletableFuture .allOf(futures.toArray(new CompletableFuture[0])) .whenComplete((noUsed, exception) -> { if (exception != null) { overallResult.completeExceptionally(exception); return; } List<U> results = new ArrayList<>(); for (CompletableFuture<U> future : futures) { results.add(future.join()); } overallResult.complete(results); }); return overallResult; }
@Override public CompletableFuture<Boolean> isTableEnabled(TableName tableName) { if (TableName.isMetaTableName(tableName)) { return CompletableFuture.completedFuture(true); } CompletableFuture<Boolean> future = new CompletableFuture<>(); addListener(AsyncMetaTableAccessor.getTableState(metaTable, tableName), (state, error) -> { if (error != null) { future.completeExceptionally(error); return; } if (state.isPresent()) { future.complete(state.get().inStates(TableState.State.ENABLED)); } else { future.completeExceptionally(new TableNotFoundException(tableName)); } }); return future; }
(failForUnknownKeyOrNamespace && throwable.getCause() instanceof UnknownKeyOrNamespaceException) ) { resultFuture.completeExceptionally(throwable.getCause()); } else if (deadline.hasTimeLeft()) { getKvStateIgnoringCertainExceptions( resultFuture.complete(result); resultFuture.whenComplete((result, throwable) -> expected.cancel(false));
private <T> CompletableFuture<T> wrap(CompletableFuture<T> future) { CompletableFuture<T> asyncFuture = new CompletableFuture<>(); future.whenCompleteAsync((r, e) -> { if (e != null) { asyncFuture.completeExceptionally(e); } else { asyncFuture.complete(r); } }, pool); return asyncFuture; }
public static <R, S, P extends Message, Q extends Message, T extends Message> CompletableFuture<R> median(AsyncTable<AdvancedScanResultConsumer> table, ColumnInterpreter<R, S, P, Q, T> ci, Scan scan) { CompletableFuture<R> future = new CompletableFuture<>(); sumByRegion(table, ci, scan).whenComplete((sumByRegion, error) -> { if (error != null) { future.completeExceptionally(error); } else if (sumByRegion.isEmpty()) { future.completeExceptionally(new NoSuchElementException()); } else { findMedian(future, table, ci, ReflectionUtils.newInstance(scan.getClass(), scan), sumByRegion); } }); return future; } }
@Override public void accept(Void unused, Throwable cause) { if (future.isDone()) { return; } if (cause != null && !(cause instanceof CancelledSubscriptionException) && !(cause instanceof AbortedStreamException)) { future.completeExceptionally(cause); } else { future.complete(HttpHeaders.EMPTY_HEADERS); } }
public static CompletableFuture<Void> waitForAll(List<CompletableFuture<Void>> futures) { final CompletableFuture<Void> compositeFuture = new CompletableFuture<>(); final AtomicInteger count = new AtomicInteger(futures.size()); for (CompletableFuture<Void> future : futures) { future.whenComplete((r, ex) -> { if (ex != null) { compositeFuture.completeExceptionally(ex); } else if (count.decrementAndGet() == 0) { // All the pending futures did complete compositeFuture.complete(null); } }); } if (futures.isEmpty()) { compositeFuture.complete(null); } return compositeFuture; } }
private <T> CompletableFuture<T> withTimeout(CompletableFuture<T> future, long timeoutNs, Supplier<String> timeoutMsg) { if (future.isDone() || timeoutNs <= 0) { return future; } Timeout timeoutTask = retryTimer.newTimeout(t -> { if (future.isDone()) { return; } future.completeExceptionally(new TimeoutIOException(timeoutMsg.get())); }, timeoutNs, TimeUnit.NANOSECONDS); return future.whenComplete((loc, error) -> { if (error != null && error.getClass() != TimeoutIOException.class) { // cancel timeout task if we are not completed by it. timeoutTask.cancel(); } }); }
@Override public void accept(Void aVoid, Throwable thrown) { if (thrown != null && !(thrown instanceof CancelledSubscriptionException) && !(thrown instanceof AbortedStreamException)) { future.completeExceptionally(thrown); } else { future.complete(firstNonNull(headers, HttpHeaders.EMPTY_HEADERS)); } } }
private <T> CompletableFuture<T> getAndConvert(String path, Converter<T> converter) { CompletableFuture<T> future = new CompletableFuture<>(); zk.get(path).whenComplete((data, error) -> { if (error != null) { future.completeExceptionally(error); return; } try { future.complete(converter.convert(data)); } catch (Exception e) { future.completeExceptionally(e); } }); return future; }
@Override public void onCompletion(final RecordMetadata metadata, final Exception exception) { if (exception == null) { latency.complete(System.currentTimeMillis() - sendTime); } else { latency.completeExceptionally(exception); } } }
private <R extends LogResponse> CompletableFuture<R> runOnContext(Supplier<CompletableFuture<R>> function) { CompletableFuture<R> future = new CompletableFuture<>(); threadContext.execute(() -> { function.get().whenComplete((response, error) -> { if (error == null) { future.complete(response); } else { future.completeExceptionally(error); } }); }); return future; }