Class AsyncTasks
CompletableFuture or CompletionStage.-
Method Summary
Modifier and TypeMethodDescriptionstatic <V> voidcomplete(V result, Throwable error, CompletableFuture<? super V> future) Completes the passed future exceptionally or normally.static <V> BiConsumer<V,Throwable> completeForwarder(CompletableFuture<? super V> future) Returns aBiConsumerpassable to thewhenCompletemethod ofCompletionStagecompleting the passedCompletableFuture.static <E extends Throwable>
BiConsumer<E,Throwable> errorResultHandler(Consumer<? super Throwable> errorHandler) Returns aBiConsumernotifying the passed error handler if any of the arguments of the returnedBiConsumeris notnull.static <R> RexpectNoError(Throwable error) Logs the argument as aSEVERElevel issue, if it is notnulland does not represent cancellation.static voidhandleErrorResult(Throwable result, Throwable error, Consumer<? super Throwable> errorHandler) Calls the given error handler if any of the exception arguments is notnull.static booleanisCanceled(Throwable error) Returnstrueif the given exception represents a cancellation event.static booleanReturnstrueif the given exception represents an error event.static ThrowableReturns the actual error represented by the given exception.
-
Method Details
-
expectNoError
Logs the argument as aSEVERElevel issue, if it is notnulland does not represent cancellation.This function is expected to be used with
CompletionStage'sexceptionallymethod to log any uncaught error. This is important to do as a last action because otherwise the stack trace of the thrown exception will be lost. That is, the intended use is:CompletionStage<?> future = ...; future.exceptionally(Tasks::expectNoError);This method does not log
OperationCanceledExceptionbecause cancellation is considered as a normal event.- Type Parameters:
R- any type- Parameters:
error- the error to be logged if notnull. This argument can benull, in which case, this method does nothing.- Returns:
- always null
- See Also:
-
unwrap
Returns the actual error represented by the given exception. This method was designed to be used withCompletableFuturein mind. That is,CompletableFutureoften wraps exceptions in aCompletionExceptionand this method unwraps that exception and returns the original cause.- Parameters:
error- the error to be unwrapped. This argument can benull, in which case the return value is alsonull.- Returns:
- the actual exception represented by the given exception. This method
only returns
nullif the given exception was alsonull.
-
isCanceled
Returnstrueif the given exception represents a cancellation event. This method is can be safely used with the error reported byCompletableFuturewhether it is thrown or if the error is provided in a callback.- Parameters:
error- the exception to be checked if it represents a cancellation event. This argument can benull, in which case, the return value isfalse.- Returns:
trueif the given exception represents a cancellation event,falseotherwise
-
isError
Returnstrueif the given exception represents an error event. That is, if the given exception isnullorrepresents cancellation.- Parameters:
error- the exception to be checked if it represents an error event. This argument can benull, in which case, the return value isfalse.- Returns:
trueif the given exception represents an error event,falseotherwise
-
completeForwarder
Returns aBiConsumerpassable to thewhenCompletemethod ofCompletionStagecompleting the passedCompletableFuture. That is, the returnedBiConsumersimply calls thecompletemethod with the arguments passed to the returnedBiConsumer.- Type Parameters:
V- the type of the result of the asynchronous computation- Parameters:
future- the future to be completed by the returnedBiConsumer. This argument cannot benull.- Returns:
- a
BiConsumerpassable to thewhenCompletemethod ofCompletionStagecompleting the passedCompletableFuture. This method never returnsnull.
-
complete
Completes the passed future exceptionally or normally. This method was designed to be called from thewhenCompletemethod ofCompletionStage.If the passed exception is not
null, the passed future is completed exceptionally with the given error. Otherwise, the passed future is completed normally with the givenresult.- Type Parameters:
V- the type of the result of the asynchronous computation- Parameters:
result- the result of the asynchronous computation to complete the passed future normally with. This argument can benull, if the asynchronous computation yieldednullresult, and should benullif the passed error is notnull.error- the exception to complete the passed future with (if notnull. This argument can benull, if the asynchronous computation completed normally. However, if notnull, the result argument will be ignored.future- theCompletableFutureto be completed. This future will always be completed after this method returns. This argument cannot benull.
-
errorResultHandler
public static <E extends Throwable> BiConsumer<E,Throwable> errorResultHandler(Consumer<? super Throwable> errorHandler) Returns aBiConsumernotifying the passed error handler if any of the arguments of the returnedBiConsumeris notnull. SeehandleErrorResultfor further explanation.The returned
BiConsumerwas designed to be passable to thewhenCompletemethod ofCompletionStage.- Type Parameters:
E- the type of the result of the asynchronous computation- Parameters:
errorHandler- the handler to be notified in case of an error. This argument cannot benull.- Returns:
- a
BiConsumernotifying the passed error handler if any of the arguments of the returnedBiConsumeris notnull. This method never returnsnull. - See Also:
-
handleErrorResult
public static void handleErrorResult(Throwable result, Throwable error, Consumer<? super Throwable> errorHandler) Calls the given error handler if any of the exception arguments is notnull. If none of the exception arguments isnull, theresultargument is passed and theerrorargument is added as a suppressed exception. If both argument isnull, the handler is not called.This method is useful if an asynchronous computation returns a
Throwableas a result but may also throw an exception.- Parameters:
result- the result exception which takes precedence over the other exception argument. This argument can benull.error- the error which will only be (directly) passed to the handler if theresultargument isnull. This argument can benull.errorHandler- the handler to be notified if any of the exception arguments is notnull. This argument cannot benull.- See Also:
-