@InterfaceAudience.Private public final class FutureUtils extends Object
Modifier and Type | Field and Description |
---|---|
private static org.slf4j.Logger |
LOG |
Modifier | Constructor and Description |
---|---|
private |
FutureUtils() |
Modifier and Type | Method and Description |
---|---|
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.
|
static <T> void |
addListener(CompletableFuture<T> future,
BiConsumer<? super T,? super Throwable> action,
Executor executor)
Almost the same with
addListener(CompletableFuture, BiConsumer) method above, the only
exception is that we will call
CompletableFuture.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> |
failedFuture(Throwable e)
Returns a CompletableFuture that is already completed exceptionally with the given exception.
|
static <T> T |
get(Future<T> future)
A helper class for getting the result of a Future, and convert the error to an
IOException . |
static <T> T |
get(Future<T> future,
long timeout,
TimeUnit unit)
A helper class for getting the result of a Future, and convert the error to an
IOException . |
static IOException |
rethrow(Throwable error)
If we could propagate the given
error 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 |
unwrapCompletionException(Throwable error)
Get the cause of the
Throwable if it is a CompletionException . |
static <T> CompletableFuture<T> |
wrapFuture(CompletableFuture<T> future,
Executor executor)
Return a
CompletableFuture which is same with the given future , but execute all
the callbacks in the given executor . |
private static final org.slf4j.Logger LOG
private FutureUtils()
public static <T> void addListener(CompletableFuture<T> future, BiConsumer<? super T,? super Throwable> action)
CompletableFuture.whenComplete(BiConsumer)
to register the action
to the
future
. 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 the action
to catch possible code bugs.
And the error phone check will always report FutureReturnValueIgnored because every method in
the CompletableFuture
class will return a new CompletableFuture
, so you always
have one future that has not been checked. So we introduce this method and add a suppress
warnings annotation here.public static <T> void addListener(CompletableFuture<T> future, BiConsumer<? super T,? super Throwable> action, Executor executor)
addListener(CompletableFuture, BiConsumer)
method above, the only
exception is that we will call
CompletableFuture.whenCompleteAsync(BiConsumer, Executor)
.public static void consume(CompletableFuture<?> future)
public static <T> CompletableFuture<T> wrapFuture(CompletableFuture<T> future, Executor executor)
CompletableFuture
which is same with the given future
, but execute all
the callbacks in the given executor
.public static Throwable unwrapCompletionException(Throwable error)
Throwable
if it is a CompletionException
.private static void setStackTrace(Throwable error)
public static IOException rethrow(Throwable error) throws IOException
error
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.IOException
public static <T> T get(Future<T> future) throws IOException
IOException
.IOException
public static <T> T get(Future<T> future, long timeout, TimeUnit unit) throws IOException
IOException
.IOException
public static <T> CompletableFuture<T> failedFuture(Throwable e)
public static <T> CompletableFuture<List<T>> allOf(List<CompletableFuture<T>> futures)
Copyright © 2007–2020 The Apache Software Foundation. All rights reserved.