@Override public DefaultConnectionFuture<T> exceptionally(Function<Throwable, ? extends T> fn) { return adopt(delegate.exceptionally(fn)); }
/** * Closes the partition. */ CompletableFuture<Void> close() { return closeClient() .exceptionally(v -> null) .thenCompose(v -> closeServer()) .exceptionally(v -> null); }
@Override public CompletableFuture<Void> stop() { if (started.compareAndSet(true, false)) { return primitives.close().exceptionally(e -> null); } return CompletableFuture.completedFuture(null); } }
private void unsubscribeFromEventsStream(String subscriptionId, String unsubscribeMethod) { sendAsync(unsubscribeRequest(subscriptionId, unsubscribeMethod), EthUnsubscribe.class) .thenAccept(ethUnsubscribe -> { log.debug("Successfully unsubscribed from subscription with id {}", subscriptionId); }) .exceptionally(throwable -> { log.error("Failed to unsubscribe from subscription with id {}", subscriptionId); return null; }); }
public PipelinedRedisFuture(Map<?, ? extends CompletionStage<?>> executions, Function<PipelinedRedisFuture<V>, V> converter) { CompletableFuture.allOf(executions.values().toArray(new CompletableFuture<?>[0])) .thenRun(() -> complete(converter.apply(this))).exceptionally(throwable -> { completeExceptionally(throwable); return null; }); }
private static HttpResponse respond(CompletableFuture<HttpResponse> future, Disposable disposable) { final HttpResponse response = HttpResponse.from(future); response.completionFuture().exceptionally(cause -> { disposable.dispose(); return null; }); return response; } }
@Override public CompletableFuture<Void> stop() { return proxy.close() .exceptionally(v -> null) .thenRun(() -> started.set(false)); } }
@Override public void enqueue(Callback responseCallback) { executeHttpRequest() .thenApply(response -> handleResponse(response, responseCallback)) .exceptionally(throwable -> handleException(throwable, responseCallback)); }
@Override public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn) { return ctx.makeContextAware(super.exceptionally(ctx.makeContextAware(fn))); } }
@Override public CompletableFuture<Void> stop() { if (started.compareAndSet(true, false)) { managementService.getMembershipService().removeListener(clusterEventListener); return transactions.close().exceptionally(e -> null); } return CompletableFuture.completedFuture(null); }
@Override void asyncReadEntry(PositionImpl position, AsyncCallbacks.ReadEntryCallback callback, Object ctx) { this.getLedgerHandle(position.getLedgerId()).thenAccept((ledger) -> { asyncReadEntry(ledger, position, callback, ctx); }).exceptionally((ex) -> { log.error("[{}] Error opening ledger for reading at position {} - {}", new Object[]{this.name, position, ex.getMessage()}); callback.readEntryFailed(ManagedLedgerException.getManagedLedgerException(ex.getCause()), ctx); return null; }); }
/** * Tries to shut down the given actor gracefully. * * @param actorRef specifying the actor to shut down * @param timeout for the graceful shut down * @return A future that finishes with {@code true} iff. the actor could be stopped gracefully * or {@code actorRef} was {@code null}. */ private CompletableFuture<Boolean> stopActor(@Nullable final ActorRef actorRef, FiniteDuration timeout) { if (actorRef == null) { return CompletableFuture.completedFuture(true); } return FutureUtils.toJava(Patterns.gracefulStop(actorRef, timeout)) .exceptionally( (Throwable throwable) -> { // The actor did not stop gracefully in time, try to directly stop it actorSystem.stop(actorRef); log.warn("Could not stop actor {} gracefully.", actorRef.path(), throwable); return false; } ); }
private void finishDoStop(CompletableFuture<Void> future) { // TODO(trustin): Add shutdownBlockingTaskExecutorOnStop // TODO(trustin): Count the pending blocking tasks and wait until it goes zero. if (!config.shutdownAccessLogWriterOnStop()) { future.complete(null); return; } config.accessLogWriter().shutdown().exceptionally(cause -> { logger.warn("Failed to close the {}:", AccessLogWriter.class.getSimpleName(), cause); return null; }).thenRunAsync(() -> future.complete(null), config.startStopExecutor()); }
@Override @SuppressWarnings("unchecked") protected CompletableFuture<Void> stopServices() { return primitives.stop() .exceptionally(e -> null) .thenComposeAsync(v -> partitions.stop(), threadContext) .exceptionally(e -> null) .thenComposeAsync(v -> super.stopServices(), threadContext); }
@Override protected void channelRead0(ChannelHandlerContext ctx, HttpRequest httpRequest) { CompletableFuture<HttpRequest> future = CompletableFuture.completedFuture(httpRequest); Executor executor = ctx.executor(); future.thenApplyAsync(req -> buildWebContext(ctx, req), executor) .thenApplyAsync(this::executeLogic, executor) .thenApplyAsync(this::buildResponse, executor) .exceptionally(this::handleException) .thenAcceptAsync(msg -> writeResponse(ctx, future, msg), ctx.channel().eventLoop()); }
@Override protected void channelRead0(ChannelHandlerContext ctx, HttpRequest httpRequest) { CompletableFuture<HttpRequest> future = CompletableFuture.completedFuture(httpRequest); Executor executor = ctx.executor(); future.thenApplyAsync(req -> buildWebContext(ctx, req), executor) .thenApplyAsync(this::executeLogic, executor) .thenApplyAsync(this::buildResponse, executor) .exceptionally(this::handleException) .thenAcceptAsync(msg -> writeResponse(ctx, future, msg), ctx.channel().eventLoop()); }
@Override public Mono<QueryResponseMessage<I>> initialResult() { return MonoWrapper.<QueryResponseMessage<I>>create(monoSinkWrapper -> query(query) .thenAccept(monoSinkWrapper::success) .exceptionally(t -> { monoSinkWrapper.error(t); return null; })).getMono(); }
@Override protected final HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) { return HttpResponse.from(req.aggregate() .thenApply(this::echo) .exceptionally(CompletionActions::log)); }
@Test void testWithinBreaksAsyncExecution() throws ExecutionException, InterruptedException { final CompletableFuture<String> asyncCode = CompletableFuture.supplyAsync(() -> { ThreadUtil.sleep(3000); return "done"; }); String value = Futures.within(asyncCode, Duration.ofSeconds(1)) .exceptionally(throwable -> null) .get(); if (value != null) { fail("Test waited so we failed!"); } }
@Test void testWithinAsyncFaster() throws ExecutionException, InterruptedException { final CompletableFuture<String> asyncCode = CompletableFuture.supplyAsync(() -> { ThreadUtil.sleep(1000); return "done"; }); String value = Futures.within(asyncCode, Duration.ofSeconds(5)) .exceptionally(throwable -> null) .get(); if (value == null) { fail("error"); } }