Package org.apache.hadoop.hbase.util
Class FutureUtils
java.lang.Object
org.apache.hadoop.hbase.util.FutureUtils
Helper class for processing futures.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidaddListener(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> voidaddListener(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 voidconsume(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> TA helper class for getting the result of a Future, and convert the error to anIOException.static <T> TA helper class for getting the result of a Future with timeout, and convert the error to anIOException.static IOExceptionIf we could propagate the givenerrordirectly, 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 voidsetStackTrace(Throwable error) static ThrowableGet the cause of theThrowableif it is aCompletionException.static <T> CompletableFuture<T>wrapFuture(CompletableFuture<T> future, Executor executor) Return aCompletableFuturewhich 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 theactionto 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 theactionto catch possible code bugs. And the error phone check will always report FutureReturnValueIgnored because every method in theCompletableFutureclass 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 aCompletableFuturewhich is same with the givenfuture, but execute all the callbacks in the givenexecutor. -
unwrapCompletionException
Get the cause of theThrowableif it is aCompletionException. -
setStackTrace
-
rethrow
If we could propagate the givenerrordirectly, 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 with timeout, 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.
-