Class FutureUtils
java.lang.Object
org.apache.bookkeeper.common.concurrent.FutureUtils
Future related utils.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> CompletableFuture<List<T>>collect(List<CompletableFuture<T>> futureList) static <T> voidcomplete(CompletableFuture<T> result, T value) static <T> voidcompleteExceptionally(CompletableFuture<T> result, Throwable cause) static <T> CompletableFuture<T>static <T> CompletableFuture<T>ensure(CompletableFuture<T> future, Runnable ensureBlock) static <T> CompletableFuture<T>static <T> CompletableFuture<Void>ignore(CompletableFuture<T> future) Ignore exception from the future.static <T> CompletableFuture<Void>ignore(CompletableFuture<T> future, String errorMsg) Ignore exception from the future and log errorMsg on exceptions.static <T,R> CompletableFuture<List<R>> processList(List<T> collection, Function<T, CompletableFuture<R>> processFunc, ExecutorService callbackExecutor) Process the list of items one by one using the process function processFunc.static <T> voidproxyTo(CompletableFuture<T> src, CompletableFuture<T> target) static <T> CompletableFuture<T>rescue(CompletableFuture<T> future, Function<Throwable, CompletableFuture<T>> rescueFuc) static <T> Tresult(CompletableFuture<T> future) static <T> Tresult(CompletableFuture<T> future, long timeout, TimeUnit timeUnit) static <T,ExceptionT extends Throwable>
Tresult(CompletableFuture<T> future, Function<Throwable, ExceptionT> exceptionHandler) static <T,ExceptionT extends Throwable>
Tresult(CompletableFuture<T> future, Function<Throwable, ExceptionT> exceptionHandler, long timeout, TimeUnit timeUnit) static <T> CompletableFuture<T>stats(CompletableFuture<T> result, OpStatsLogger opStatsLogger, com.google.common.base.Stopwatch stopwatch) Add a event listener over result for collecting the operation stats.static <T> CompletableFuture<T>value(T value) static CompletableFuture<Void>Void()static <T> CompletableFuture<T>whenCompleteAsync(CompletableFuture<T> future, BiConsumer<? super T, ? super Throwable> action, OrderedScheduler scheduler, Object scheduleKey) Completing thefuturein the thread in the scheduler identified by thescheduleKey.static <T> CompletableFuture<T>within(CompletableFuture<T> promise, long timeout, TimeUnit unit, Throwable cause, OrderedScheduler scheduler, Object key) Raise an exception to the promise within a given timeout period.
-
Method Details
-
Void
-
result
- Throws:
Exception
-
result
public static <T> T result(CompletableFuture<T> future, long timeout, TimeUnit timeUnit) throws Exception - Throws:
Exception
-
result
public static <T,ExceptionT extends Throwable> T result(CompletableFuture<T> future, Function<Throwable, ExceptionT> exceptionHandler) throws ExceptionT- Throws:
ExceptionT extends Throwable
-
result
public static <T,ExceptionT extends Throwable> T result(CompletableFuture<T> future, Function<Throwable, ExceptionT> exceptionHandler, long timeout, TimeUnit timeUnit) throws ExceptionT, TimeoutException- Throws:
ExceptionT extends ThrowableTimeoutException
-
createFuture
-
value
-
exception
-
complete
-
completeExceptionally
-
whenCompleteAsync
public static <T> CompletableFuture<T> whenCompleteAsync(CompletableFuture<T> future, BiConsumer<? super T, ? super Throwable> action, OrderedScheduler scheduler, Object scheduleKey) Completing thefuturein the thread in the scheduler identified by thescheduleKey.- Type Parameters:
T-- Parameters:
future- future to completeaction- action to execute when completescheduler- scheduler to execute the action.scheduleKey- key to choose the thread to execute the action- Returns:
-
collect
-
proxyTo
-
processList
public static <T,R> CompletableFuture<List<R>> processList(List<T> collection, Function<T, CompletableFuture<R>> processFunc, @Nullable ExecutorService callbackExecutor) Process the list of items one by one using the process function processFunc. The process will be stopped immediately if it fails on processing any one.- Parameters:
collection- list of itemsprocessFunc- process functioncallbackExecutor- executor to process the item- Returns:
- future presents the list of processed results
-
within
public static <T> CompletableFuture<T> within(CompletableFuture<T> promise, long timeout, TimeUnit unit, Throwable cause, OrderedScheduler scheduler, Object key) Raise an exception to the promise within a given timeout period. If the promise has been satisfied before raising, it won't change the state of the promise.- Parameters:
promise- promise to raise exceptiontimeout- timeout periodunit- timeout period unitcause- cause to raisescheduler- scheduler to execute raising exceptionkey- the submit key used by the scheduler- Returns:
- the promise applied with the raise logic
-
ignore
Ignore exception from the future.- Parameters:
future- the original future- Returns:
- a transformed future ignores exceptions
-
ignore
Ignore exception from the future and log errorMsg on exceptions.- Parameters:
future- the original futureerrorMsg- the error message to log on exceptions- Returns:
- a transformed future ignores exceptions
-
ensure
-
rescue
public static <T> CompletableFuture<T> rescue(CompletableFuture<T> future, Function<Throwable, CompletableFuture<T>> rescueFuc) -
stats
public static <T> CompletableFuture<T> stats(CompletableFuture<T> result, OpStatsLogger opStatsLogger, com.google.common.base.Stopwatch stopwatch) Add a event listener over result for collecting the operation stats.- Type Parameters:
T-- Parameters:
result- result to listen onopStatsLogger- stats logger to record operations statsstopwatch- stop watch to time operation- Returns:
- result after registered the event listener
-