Package org.apache.hadoop.hbase.util
Class FutureUtils
java.lang.Object
org.apache.hadoop.hbase.util.FutureUtils
Helper class for processing futures.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> void
addListener
(CompletableFuture<T> future, BiConsumer<? super T, ? super Throwable> action) This is method is used when you just want to add a listener to the given future.static <T> void
addListener
(CompletableFuture<T> future, BiConsumer<? super T, ? super Throwable> action, Executor executor) Almost the same withaddListener(CompletableFuture, BiConsumer)
method above, the only exception is that we will callCompletableFuture.whenCompleteAsync(BiConsumer, Executor)
.static <T> CompletableFuture<List<T>>
allOf
(List<CompletableFuture<T>> futures) Returns a new CompletableFuture that is completed when all of the given CompletableFutures complete.static void
consume
(CompletableFuture<?> future) Log the error if the future indicates any failure.static <T> CompletableFuture<T>
Returns a CompletableFuture that is already completed exceptionally with the given exception.static <T> T
A helper class for getting the result of a Future, and convert the error to anIOException
.static <T> T
A helper class for getting the result of a Future, and convert the error to anIOException
.static IOException
If we could propagate the givenerror
directly, we will fill the stack trace with the current thread's stack trace so it is easier to trace where is the exception thrown.private static void
setStackTrace
(Throwable error) static Throwable
Get the cause of theThrowable
if it is aCompletionException
.static <T> CompletableFuture<T>
wrapFuture
(CompletableFuture<T> future, Executor executor) Return aCompletableFuture
which is same with the givenfuture
, but execute all the callbacks in the givenexecutor
.
-
Field Details
-
LOG
-
-
Constructor Details
-
FutureUtils
private FutureUtils()
-
-
Method Details
-
addListener
public static <T> void addListener(CompletableFuture<T> future, BiConsumer<? super T, ? super Throwable> action) This is method is used when you just want to add a listener to the given future. We will callCompletableFuture.whenComplete(BiConsumer)
to register theaction
to thefuture
. Ignoring the return value of a Future is considered as a bad practice as it may suppress exceptions thrown from the code that completes the future, and this method will catch all the exception thrown from theaction
to catch possible code bugs. And the error phone check will always report FutureReturnValueIgnored because every method in theCompletableFuture
class will return a newCompletableFuture
, so you always have one future that has not been checked. So we introduce this method and add a suppress warnings annotation here. -
addListener
public static <T> void addListener(CompletableFuture<T> future, BiConsumer<? super T, ? super Throwable> action, Executor executor) Almost the same withaddListener(CompletableFuture, BiConsumer)
method above, the only exception is that we will callCompletableFuture.whenCompleteAsync(BiConsumer, Executor)
. -
consume
Log the error if the future indicates any failure. -
wrapFuture
Return aCompletableFuture
which is same with the givenfuture
, but execute all the callbacks in the givenexecutor
. -
unwrapCompletionException
Get the cause of theThrowable
if it is aCompletionException
. -
setStackTrace
-
rethrow
If we could propagate the givenerror
directly, we will fill the stack trace with the current thread's stack trace so it is easier to trace where is the exception thrown. If not, we will just create a new IOException and then throw it.- Throws:
IOException
-
get
A helper class for getting the result of a Future, and convert the error to anIOException
.- Throws:
IOException
-
get
A helper class for getting the result of a Future, and convert the error to anIOException
.- Throws:
IOException
-
failedFuture
Returns a CompletableFuture that is already completed exceptionally with the given exception. -
allOf
Returns a new CompletableFuture that is completed when all of the given CompletableFutures complete. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so, with a CompletionException holding this exception as its cause. Otherwise, the results of all given CompletableFutures could be obtained by the new returned CompletableFuture.
-