Package org.axonframework.common
Class FutureUtils
java.lang.Object
org.axonframework.common.FutureUtils
Utility class containing reusable functionality for interacting with the
CompletableFuture.- Since:
- 5.0.0
- Author:
- Allard Buijze
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> BiConsumer<T, Throwable> alsoComplete(CompletableFuture<T> future) Creates a function that can be passed to aCompletableFuture.whenComplete(BiConsumer)orCompletableFuture.whenCompleteAsync(BiConsumer)invocation, to complete the givenfuturewith the same result as theCompletableFuturethat this function is passed to.static <T> CompletableFuture<T> Creates a completedCompletableFuturewithnullas result.static <T> VoidignoreResult(T toIgnore) Utility method that doesn't do anything.static <T> TjoinAndUnwrap(CompletableFuture<T> future) Joins aCompletableFutureand unwraps anyCompletionExceptionto throw the actual cause, preserving the exact exception type without wrapping checked exceptions.static <T> CompletableFuture<T> runFailing(Supplier<CompletableFuture<T>> fn) Safely catches exceptions thrown by the givenfnand returns aCompletableFuturethat completes.static ThrowableUnwrap givenexceptionfrom the exception-wrappers added byCompletableFuture.
-
Method Details
-
ignoreResult
Utility method that doesn't do anything. Its purpose is to simplify conversion of aCompletableFuturewith any generic type to aCompletableFuture<Void>.Example:
completableFuture.thenApply(FutureUtils::ignoreResult- Type Parameters:
T- The declared result of theCompletableFutureto ignore the result of.- Parameters:
toIgnore- The actual result of theCompletableFuture.- Returns:
null, as that's the only valid value forVoid.
-
emptyCompletedFuture
Creates a completedCompletableFuturewithnullas result.- Type Parameters:
T- The declared type to return in theCompletableFuture.- Returns:
- A
CompletableFuturethat is completed withnull.
-
alsoComplete
Creates a function that can be passed to aCompletableFuture.whenComplete(BiConsumer)orCompletableFuture.whenCompleteAsync(BiConsumer)invocation, to complete the givenfuturewith the same result as theCompletableFuturethat this function is passed to.- Type Parameters:
T- The declared type of result from theCompletableFuture.- Parameters:
future- TheCompletableFutureto also complete.- Returns:
- A function that completes another
futurewith the same results.
-
unwrap
Unwrap givenexceptionfrom the exception-wrappers added byCompletableFuture.More specifically, if the given
exceptionis aCompletionExceptionorExecutionException, it returns the cause. Otherwise, it will return the exception as-is.- Parameters:
exception- The exception to unwrap.- Returns:
- The unwrapped exception if the given
exceptionis of typeCompletionExceptionorExecutionException. Otherwise, it is returned as is.
-
runFailing
@Nonnull public static <T> CompletableFuture<T> runFailing(@Nonnull Supplier<CompletableFuture<T>> fn) Safely catches exceptions thrown by the givenfnand returns aCompletableFuturethat completes.- Type Parameters:
T- Type of the completable future.- Parameters:
fn- A lambda returning aCompletableFuture.- Returns:
- A completable future that completes exceptionally if the given lambda throws an exception.
-
joinAndUnwrap
Joins aCompletableFutureand unwraps anyCompletionExceptionto throw the actual cause, preserving the exact exception type without wrapping checked exceptions.This method uses the "sneaky throw" technique to re-throw checked exceptions without declaring them, which preserves the original exception type completely. Use this when you need precise exception type preservation and are certain about the exception handling contract.
- Type Parameters:
T- The type of the future's result.- Parameters:
future- TheCompletableFutureto join.- Returns:
- The result of the future.
- Throws:
Throwable- the unwrapped cause if the future completed exceptionally (exact type preserved).
-