Package io.pravega.common.concurrent
Class Futures
- java.lang.Object
-
- io.pravega.common.concurrent.Futures
-
public final class Futures extends java.lang.ObjectExtensions to Future and CompletableFuture.
-
-
Constructor Summary
Constructors Constructor Description Futures()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> java.util.concurrent.CompletableFuture<java.lang.Void>allOf(java.util.Collection<java.util.concurrent.CompletableFuture<T>> futures)Similar implementation to CompletableFuture.allOf(vararg) but that works on a Collection.static <T> java.util.concurrent.CompletableFuture<java.util.List<T>>allOfWithResults(java.util.List<java.util.concurrent.CompletableFuture<T>> futures)Similar implementation to CompletableFuture.allOf(vararg) but that works on a Collection and that returns another Collection which has the results of the given CompletableFutures.static <K,V>
java.util.concurrent.CompletableFuture<java.util.Map<K,V>>allOfWithResults(java.util.Map<K,java.util.concurrent.CompletableFuture<V>> futureMap)Similar to CompletableFuture.allOf(varargs), but that works on a Map and that returns another Map which has the results of the given CompletableFutures, with the same input keys.static <T> booleanawait(java.util.concurrent.CompletableFuture<T> f)Waits for the provided future to be complete, and returns true if it was successful, false otherwise.static <T> booleanawait(java.util.concurrent.CompletableFuture<T> f, long timeout)Waits for the provided future to be complete, and returns true if it was successful, false if it failed or did not complete.static <T> java.util.concurrent.CompletableFuture<T>cancellableFuture(java.util.concurrent.CompletableFuture<T> source, java.util.function.Consumer<T> onCancel)Returns a CompletableFuture that will complete with the same outcome or result as the given source, but when cancelled, will apply a consumer to the eventual result of the original future.static <T> voidcompleteAfter(java.util.function.Supplier<java.util.concurrent.CompletableFuture<? extends T>> futureSupplier, java.util.concurrent.CompletableFuture<T> toComplete)Given a Supplier returning a Future, completes another future either with the result of the first future, in case of normal completion, or exceptionally with the exception of the first future.static <T> java.util.concurrent.CompletableFuture<T>completeOn(java.util.concurrent.CompletableFuture<T> future, java.util.concurrent.Executor executor)Returns a newCompletableFuturethat completes with the same outcome as the given one, but on the givenExecutor.static java.util.concurrent.CompletableFuture<java.lang.Void>delayedFuture(java.time.Duration delay, java.util.concurrent.ScheduledExecutorService executorService)Creates a CompletableFuture that will do nothing and complete after a specified delay, without using a thread during the delay.static <T> java.util.concurrent.CompletableFuture<T>delayedFuture(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> task, long delay, java.util.concurrent.ScheduledExecutorService executorService)Executes the asynchronous task after the specified delay.static <T> java.util.concurrent.CompletableFuture<T>delayedTask(java.util.function.Supplier<T> task, java.time.Duration delay, java.util.concurrent.ScheduledExecutorService executorService)Executes the given task after the specified delay.static <T> java.util.concurrent.CompletableFuture<java.lang.Void>doWhileLoop(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> loopBody, java.util.function.Predicate<T> condition, java.util.concurrent.Executor executor)Executes a code fragment returning a CompletableFutures while a condition on the returned value is satisfied.static <T> java.util.concurrent.CompletableFuture<T>exceptionallyCompose(java.util.concurrent.CompletableFuture<T> future, java.util.function.Function<java.lang.Throwable,java.util.concurrent.CompletableFuture<T>> handler)Same as CompletableFuture.exceptionally(), except that it allows returning a CompletableFuture instead of a single value.static <T> java.util.concurrent.CompletableFuture<T>exceptionallyComposeExpecting(java.util.concurrent.CompletableFuture<T> future, java.util.function.Predicate<java.lang.Throwable> isExpected, java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> exceptionFutureSupplier)Same as exceptionallyExpecting(), except that it allows executing/returning a Future as a result in case of an expected exception.static <T> java.util.concurrent.CompletableFuture<T>exceptionallyExpecting(java.util.concurrent.CompletableFuture<T> future, java.util.function.Predicate<java.lang.Throwable> isExpected, T exceptionValue)Same as CompletableFuture.exceptionally(), except that it allows certain exceptions, as defined by the isExpected parameter.static <T,E extends java.lang.Throwable>
voidexceptionListener(java.util.concurrent.CompletableFuture<T> completableFuture, java.lang.Class<E> exceptionClass, java.util.function.Consumer<E> exceptionListener)Registers an exception listener to the given CompletableFuture for a particular type of exception.static <T> voidexceptionListener(java.util.concurrent.CompletableFuture<T> completableFuture, java.util.function.Consumer<java.lang.Throwable> exceptionListener)Registers an exception listener to the given CompletableFuture.static <T> java.util.concurrent.CompletableFuture<T>failedFuture(java.lang.Throwable exception)Creates a new CompletableFuture that is failed with the given exception.static <T> java.util.concurrent.CompletableFuture<java.util.List<T>>filter(java.util.List<T> input, java.util.function.Function<T,java.util.concurrent.CompletableFuture<java.lang.Boolean>> predicate)Filter that takes a predicate that evaluates in future and returns the filtered list that evaluates in future.static <T> java.util.concurrent.CompletableFuture<T>futureWithTimeout(java.time.Duration timeout, java.lang.String tag, java.util.concurrent.ScheduledExecutorService executorService)Creates a new CompletableFuture that will timeout after the given amount of time.static <T> java.util.concurrent.CompletableFuture<T>futureWithTimeout(java.time.Duration timeout, java.util.concurrent.ScheduledExecutorService executorService)Creates a new CompletableFuture that will timeout after the given amount of time.static <T> java.util.concurrent.CompletableFuture<T>futureWithTimeout(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> futureSupplier, java.time.Duration timeout, java.lang.String tag, java.util.concurrent.ScheduledExecutorService executorService)Creates a new CompletableFuture that either holds the result of future from the futureSupplier or will timeout after the given amount of time.static <ResultT,ExceptionT extends java.lang.Exception>
ResultTgetAndHandleExceptions(java.util.concurrent.Future<ResultT> future, java.util.function.Function<java.lang.Throwable,ExceptionT> exceptionConstructor)Calls get on the provided future, handling interrupted, and transforming the executionException into an exception of the type whose constructor is provided.static <T> java.lang.ThrowablegetException(java.util.concurrent.CompletableFuture<T> future)If the future has failed returns the exception that caused it.static <ResultT,E1 extends java.lang.Exception,E2 extends java.lang.Exception,E3 extends java.lang.Exception>
ResultTgetThrowingException(java.util.concurrent.Future<ResultT> future)Gets a future returning its result, or the exception that caused it to fail.static <T,U>
java.util.concurrent.CompletableFuture<U>handleCompose(java.util.concurrent.CompletableFuture<T> future, java.util.function.BiFunction<T,java.lang.Throwable,java.util.concurrent.CompletableFuture<U>> handler)Same as CompletableFuture.handle(), except that it allows returning a CompletableFuture instead of a single value.static <T> booleanisSuccessful(java.util.concurrent.CompletableFuture<T> f)Returns true if the future is done and successful.static <K,V>
java.util.concurrent.CompletableFuture<java.util.Map<K,V>>keysAllOfWithResults(java.util.Map<java.util.concurrent.CompletableFuture<K>,V> futureMap)Similar to CompletableFuture.allOf(varargs), but that works on a Map and that returns another Map which has the results of the given CompletableFutures, with the same input keys.static <T> java.util.concurrent.CompletableFuture<java.lang.Void>loop(java.lang.Iterable<T> iterable, java.util.function.Function<T,java.util.concurrent.CompletableFuture<java.lang.Boolean>> loopBody, java.util.concurrent.Executor executor)Executes a loop using CompletableFutures over the given Iterable, processing each item in order, without overlap, using the given Executor for task execution.static java.util.concurrent.CompletableFuture<java.lang.Void>loop(java.util.function.Supplier<java.lang.Boolean> condition, java.util.function.Supplier<java.util.concurrent.CompletableFuture<java.lang.Void>> loopBody, java.util.concurrent.Executor executor)Executes a loop using CompletableFutures, without invoking join()/get() on any of them or exclusively hogging a thread.static <T> java.util.concurrent.CompletableFuture<java.lang.Void>loop(java.util.function.Supplier<java.lang.Boolean> condition, java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> loopBody, java.util.function.Consumer<T> resultConsumer, java.util.concurrent.Executor executor)Executes a loop using CompletableFutures, without invoking join()/get() on any of them or exclusively hogging a thread.static <T> voidonTimeout(java.util.concurrent.CompletableFuture<T> future, java.util.function.Consumer<java.util.concurrent.TimeoutException> callback)Attaches the given callback as an exception listener to the given CompletableFuture, which will be invoked when the future times out (fails with a TimeoutException).static <T,R>
RrunOrFail(java.util.concurrent.Callable<R> callable, java.util.concurrent.CompletableFuture<T> future)Runs the provided Callable in the current thread (synchronously) if it throws any Exception or Throwable the exception is propagated, but the supplied future is also failed.static <T> java.util.concurrent.CompletableFuture<java.lang.Void>toVoid(java.util.concurrent.CompletableFuture<T> future)Returns a CompletableFuture that will end when the given future ends, but discards its result.static <T,E extends java.lang.Exception>
java.util.concurrent.CompletableFuture<java.lang.Void>toVoidExpecting(java.util.concurrent.CompletableFuture<T> future, T expectedValue, java.util.function.Supplier<E> exceptionConstructor)Returns a CompletableFuture that will end when the given future ends, expecting a certain result.
-
-
-
Method Detail
-
completeOn
public static <T> java.util.concurrent.CompletableFuture<T> completeOn(java.util.concurrent.CompletableFuture<T> future, java.util.concurrent.Executor executor)Returns a newCompletableFuturethat completes with the same outcome as the given one, but on the givenExecutor. This helps transfer the downstream callback executions on another executor.- Type Parameters:
T- The Type of the future's result.- Parameters:
future- The future whose result is wanted.executor- The executor to transfer callback execution onto.- Returns:
- A new
CompletableFuturethat will complete with the same outcome as the given one, but on the givenExecutor.
-
await
public static <T> boolean await(java.util.concurrent.CompletableFuture<T> f)
Waits for the provided future to be complete, and returns true if it was successful, false otherwise.- Type Parameters:
T- The Type of the future's result.- Parameters:
f- The future to wait for.- Returns:
- True if the provided CompletableFuture is complete and successful.
-
await
public static <T> boolean await(java.util.concurrent.CompletableFuture<T> f, long timeout)Waits for the provided future to be complete, and returns true if it was successful, false if it failed or did not complete.- Type Parameters:
T- The Type of the future's result.- Parameters:
timeout- The maximum number of milliseconds to blockf- The future to wait for.- Returns:
- True if the given CompletableFuture is completed and successful within the given timeout.
-
completeAfter
public static <T> void completeAfter(java.util.function.Supplier<java.util.concurrent.CompletableFuture<? extends T>> futureSupplier, java.util.concurrent.CompletableFuture<T> toComplete)Given a Supplier returning a Future, completes another future either with the result of the first future, in case of normal completion, or exceptionally with the exception of the first future.- Type Parameters:
T- Return type of Future.- Parameters:
futureSupplier- A Supplier returning a Future to listen to.toComplete- A CompletableFuture that has not yet been completed, which will be completed with the result of the Future from futureSupplier.
-
isSuccessful
public static <T> boolean isSuccessful(java.util.concurrent.CompletableFuture<T> f)
Returns true if the future is done and successful.- Type Parameters:
T- The Type of the future's result.- Parameters:
f- The future to inspect.- Returns:
- True if the given CompletableFuture has completed successfully.
-
getException
public static <T> java.lang.Throwable getException(java.util.concurrent.CompletableFuture<T> future)
If the future has failed returns the exception that caused it. Otherwise returns null.- Type Parameters:
T- The Type of the future's result.- Parameters:
future- The future to inspect.- Returns:
- null or the exception that caused the Future to fail.
-
getThrowingException
public static <ResultT,E1 extends java.lang.Exception,E2 extends java.lang.Exception,E3 extends java.lang.Exception> ResultT getThrowingException(java.util.concurrent.Future<ResultT> future) throws E1 extends java.lang.Exception, E2 extends java.lang.Exception, E3 extends java.lang.ExceptionGets a future returning its result, or the exception that caused it to fail. (Unlike a normal future the cause does not need to be extracted.) Because some of these exceptions are checked, and the future does not allow expressing this, the compile time information is lost. To get around this this method is generically typed with up to 3 exception types, that can be used to re-introduce the exception types that could cause the future to fail into to the compiler so they can be tracked. Note that nothing restricts or ensures that the exceptions thrown from the future are of this type. The exception will always be throw as it was set on the future, these types are purely for the benefit of the compiler. It is up to the caller to ensure that this type matches the exception type that can fail the future.- Type Parameters:
ResultT- The result type of the provided futureE1- A type of exception that may cause the future to fail.E2- A type of exception that may cause the future to fail.E3- A type of exception that may cause the future to fail.- Parameters:
future- The future to call get() on.- Returns:
- The result of the provided future.
- Throws:
E1- If exception E1 occurs.E2- If exception E2 occurs.E3- If exception E3 occurs.E1 extends java.lang.Exception
-
getAndHandleExceptions
public static <ResultT,ExceptionT extends java.lang.Exception> ResultT getAndHandleExceptions(java.util.concurrent.Future<ResultT> future, java.util.function.Function<java.lang.Throwable,ExceptionT> exceptionConstructor) throws ExceptionT extends java.lang.ExceptionCalls get on the provided future, handling interrupted, and transforming the executionException into an exception of the type whose constructor is provided.- Type Parameters:
ResultT- Type of the result.ExceptionT- Type of the Exception.- Parameters:
future- The future whose result is wantedexceptionConstructor- This can be any function that either transforms an exception i.e. Passing RuntimeException::new will wrap the exception in a new RuntimeException. If null is returned from the function no exception will be thrown.- Returns:
- The result of calling future.get()
- Throws:
ExceptionT- If thrown by the future.ExceptionT extends java.lang.Exception
-
failedFuture
public static <T> java.util.concurrent.CompletableFuture<T> failedFuture(java.lang.Throwable exception)
Creates a new CompletableFuture that is failed with the given exception.- Type Parameters:
T- The Type of the future's result.- Parameters:
exception- The exception to fail the CompletableFuture.- Returns:
- A CompletableFuture that fails with the given exception.
-
cancellableFuture
public static <T> java.util.concurrent.CompletableFuture<T> cancellableFuture(java.util.concurrent.CompletableFuture<T> source, java.util.function.Consumer<T> onCancel)Returns a CompletableFuture that will complete with the same outcome or result as the given source, but when cancelled, will apply a consumer to the eventual result of the original future.If the returned CompletableFuture is NOT cancelled (
CompletableFuture.cancel(boolean)): - If source completes normally, the result CompletableFuture will complete with the same result. - If source completes exceptionally, the result CompletableFuture will complete with the same result.If the returned CompletableFuture is cancelled (
CompletableFuture.cancel(boolean)): - If the source has already completed, the result CompletableFuture will also be completed with the same outcome. - If the source has not already been completed, if it completes normally, then `onCancel` will be applied to the result when it eventually completes. The source completes exceptionally, nothing will happen.- Type Parameters:
T- Result type.- Parameters:
source- The CompletableFuture to wrap.onCancel- A Consumer to invoke on source's eventual completion result if the result of this method is cancelled.- Returns:
- A CompletableFuture that will complete with the same outcome or result as the given source.
-
exceptionListener
public static <T> void exceptionListener(java.util.concurrent.CompletableFuture<T> completableFuture, java.util.function.Consumer<java.lang.Throwable> exceptionListener)Registers an exception listener to the given CompletableFuture.- Type Parameters:
T- The Type of the future's result.- Parameters:
completableFuture- The Future to register to.exceptionListener- The Listener to register.
-
exceptionListener
public static <T,E extends java.lang.Throwable> void exceptionListener(java.util.concurrent.CompletableFuture<T> completableFuture, java.lang.Class<E> exceptionClass, java.util.function.Consumer<E> exceptionListener)Registers an exception listener to the given CompletableFuture for a particular type of exception.- Type Parameters:
T- The Type of the future's result.E- The Type of the exception.- Parameters:
completableFuture- The Future to register to.exceptionClass- The type of exception to listen to.exceptionListener- The Listener to register.
-
exceptionallyCompose
public static <T> java.util.concurrent.CompletableFuture<T> exceptionallyCompose(java.util.concurrent.CompletableFuture<T> future, java.util.function.Function<java.lang.Throwable,java.util.concurrent.CompletableFuture<T>> handler)Same as CompletableFuture.exceptionally(), except that it allows returning a CompletableFuture instead of a single value.- Type Parameters:
T- Type of the value of the original Future.- Parameters:
future- The original CompletableFuture to attach an Exception Listener.handler- A Function that consumes a Throwable and returns a CompletableFuture of the same type as the original one. This Function will be invoked if the original Future completed exceptionally.- Returns:
- A new CompletableFuture that will be completed either with the result of future (if it completed normally), or with the result of handler when applied to the exception of future, should future complete exceptionally.
-
exceptionallyExpecting
public static <T> java.util.concurrent.CompletableFuture<T> exceptionallyExpecting(java.util.concurrent.CompletableFuture<T> future, java.util.function.Predicate<java.lang.Throwable> isExpected, T exceptionValue)Same as CompletableFuture.exceptionally(), except that it allows certain exceptions, as defined by the isExpected parameter. If such an exception is caught, the given exceptionValue is then returned. All other Exceptions will be re-thrown.- Type Parameters:
T- The Type of the Future's result.- Parameters:
future- The original CompletableFuture to attach to.isExpected- A Predicate that can check whether an Exception is expected or not.exceptionValue- The value to return in case the thrown Exception if of type exceptionClass.- Returns:
- A new CompletableFuture that will complete either: - With the same result as the original Future if that one completed normally - With exceptionValue if the original Future completed with an expected exception. - Exceptionally with the original Future's exception if none of the above are true.
-
exceptionallyComposeExpecting
public static <T> java.util.concurrent.CompletableFuture<T> exceptionallyComposeExpecting(java.util.concurrent.CompletableFuture<T> future, java.util.function.Predicate<java.lang.Throwable> isExpected, java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> exceptionFutureSupplier)Same as exceptionallyExpecting(), except that it allows executing/returning a Future as a result in case of an expected exception. If such an exception is caught, the given exceptionFutureSupplier is invoked and its result is then returned. All other Exceptions will be re-thrown.- Type Parameters:
T- The Type of the Future's result.- Parameters:
future- The original CompletableFuture to attach to.isExpected- A Predicate that can check whether an Exception is expected or not.exceptionFutureSupplier- A Supplier that returns a CompletableFuture which will be invoked in case the thrown Exception if of type exceptionClass.- Returns:
- A new CompletableFuture that will complete either: - With the same result as the original Future if that one completed normally - With exceptionValue if the original Future completed with an expected exception. - Exceptionally with the original Future's exception if none of the above are true.
-
toVoid
public static <T> java.util.concurrent.CompletableFuture<java.lang.Void> toVoid(java.util.concurrent.CompletableFuture<T> future)
Returns a CompletableFuture that will end when the given future ends, but discards its result. If the given future fails, the returned future will fail with the same exception.- Type Parameters:
T- The type of the input's future result.- Parameters:
future- The CompletableFuture to attach to.- Returns:
- A CompletableFuture that will complete when the given future completes. If the given future fails, so will this future.
-
toVoidExpecting
public static <T,E extends java.lang.Exception> java.util.concurrent.CompletableFuture<java.lang.Void> toVoidExpecting(java.util.concurrent.CompletableFuture<T> future, T expectedValue, java.util.function.Supplier<E> exceptionConstructor)Returns a CompletableFuture that will end when the given future ends, expecting a certain result. If the supplied value is not the same (using .equals() comparison) as the result an exception from the supplier will be thrown. If the given future fails, the returned future will fail with the same exception.- Type Parameters:
T- The type of the value expectedE- The type of the exception to be throw if the value is not found.- Parameters:
future- the CompletableFuture to attach to.expectedValue- The value expectedexceptionConstructor- Constructor for an exception in the event there is not a match.- Returns:
- A void completable future.
-
handleCompose
public static <T,U> java.util.concurrent.CompletableFuture<U> handleCompose(java.util.concurrent.CompletableFuture<T> future, java.util.function.BiFunction<T,java.lang.Throwable,java.util.concurrent.CompletableFuture<U>> handler)Same as CompletableFuture.handle(), except that it allows returning a CompletableFuture instead of a single value.- Type Parameters:
T- Type of the value of the original Future.U- Type of the value of the returned Future.- Parameters:
future- The original CompletableFuture to attach a handle callback.handler- A BiFunction that consumes a Throwable or successful result and returns a CompletableFuture of the same type as the original one. This Function will be invoked after the original Future completes both successfully and exceptionally.- Returns:
- A new CompletableFuture that will handle the result/exception and return a new future or throw the exception.
-
allOfWithResults
public static <T> java.util.concurrent.CompletableFuture<java.util.List<T>> allOfWithResults(java.util.List<java.util.concurrent.CompletableFuture<T>> futures)
Similar implementation to CompletableFuture.allOf(vararg) but that works on a Collection and that returns another Collection which has the results of the given CompletableFutures.- Type Parameters:
T- The type of the results items.- Parameters:
futures- A List of CompletableFutures to wait on.- Returns:
- A new CompletableFuture List that is completed when all of the given CompletableFutures complete.
-
allOfWithResults
public static <K,V> java.util.concurrent.CompletableFuture<java.util.Map<K,V>> allOfWithResults(java.util.Map<K,java.util.concurrent.CompletableFuture<V>> futureMap)
Similar to CompletableFuture.allOf(varargs), but that works on a Map and that returns another Map which has the results of the given CompletableFutures, with the same input keys.- Type Parameters:
K- The type of the Keys.V- The Type of the Values.- Parameters:
futureMap- A Map of Keys to CompletableFutures to wait on.- Returns:
- A CompletableFuture that will contain a Map of Keys to Values, where Values are the results of the Futures in the input map.
-
keysAllOfWithResults
public static <K,V> java.util.concurrent.CompletableFuture<java.util.Map<K,V>> keysAllOfWithResults(java.util.Map<java.util.concurrent.CompletableFuture<K>,V> futureMap)
Similar to CompletableFuture.allOf(varargs), but that works on a Map and that returns another Map which has the results of the given CompletableFutures, with the same input keys.- Type Parameters:
K- The type of the Keys.V- The Type of the Values.- Parameters:
futureMap- A Map of Keys to CompletableFutures to wait on.- Returns:
- A CompletableFuture that will contain a Map of Keys to Values, where Values are the results of the Futures in the input map.
-
allOf
public static <T> java.util.concurrent.CompletableFuture<java.lang.Void> allOf(java.util.Collection<java.util.concurrent.CompletableFuture<T>> futures)
Similar implementation to CompletableFuture.allOf(vararg) but that works on a Collection.- Type Parameters:
T- The type of the results items.- Parameters:
futures- A Collection of CompletableFutures to wait on.- Returns:
- A new CompletableFuture that is completed when all of the given CompletableFutures complete.
-
filter
public static <T> java.util.concurrent.CompletableFuture<java.util.List<T>> filter(java.util.List<T> input, java.util.function.Function<T,java.util.concurrent.CompletableFuture<java.lang.Boolean>> predicate)Filter that takes a predicate that evaluates in future and returns the filtered list that evaluates in future.- Type Parameters:
T- Type parameter.- Parameters:
input- Input list.predicate- Predicate that evaluates in the future.- Returns:
- List that evaluates in future.
-
futureWithTimeout
public static <T> java.util.concurrent.CompletableFuture<T> futureWithTimeout(java.time.Duration timeout, java.util.concurrent.ScheduledExecutorService executorService)Creates a new CompletableFuture that will timeout after the given amount of time.- Type Parameters:
T- The Type argument for the CompletableFuture to create.- Parameters:
timeout- The timeout for the future.executorService- An ExecutorService that will be used to invoke the timeout on.- Returns:
- A CompletableFuture with a timeout.
-
futureWithTimeout
public static <T> java.util.concurrent.CompletableFuture<T> futureWithTimeout(java.time.Duration timeout, java.lang.String tag, java.util.concurrent.ScheduledExecutorService executorService)Creates a new CompletableFuture that will timeout after the given amount of time.- Type Parameters:
T- The Type argument for the CompletableFuture to create.- Parameters:
timeout- The timeout for the future.tag- A tag (identifier) to be used as a parameter to the TimeoutException.executorService- An ExecutorService that will be used to invoke the timeout on.- Returns:
- The result.
-
futureWithTimeout
public static <T> java.util.concurrent.CompletableFuture<T> futureWithTimeout(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> futureSupplier, java.time.Duration timeout, java.lang.String tag, java.util.concurrent.ScheduledExecutorService executorService)Creates a new CompletableFuture that either holds the result of future from the futureSupplier or will timeout after the given amount of time.- Type Parameters:
T- The Type argument for the CompletableFuture to create.- Parameters:
futureSupplier- Supplier of the future.timeout- The timeout for the future.tag- A tag (identifier) to be used as a parameter to the TimeoutException.executorService- An ExecutorService that will be used to invoke the timeout on.- Returns:
- A CompletableFuture which is either completed within given timebound or failed with timeout exception.
-
onTimeout
public static <T> void onTimeout(java.util.concurrent.CompletableFuture<T> future, java.util.function.Consumer<java.util.concurrent.TimeoutException> callback)Attaches the given callback as an exception listener to the given CompletableFuture, which will be invoked when the future times out (fails with a TimeoutException).- Type Parameters:
T- The Type of the future's result.- Parameters:
future- The future to attach to.callback- The callback to invoke.
-
delayedFuture
public static java.util.concurrent.CompletableFuture<java.lang.Void> delayedFuture(java.time.Duration delay, java.util.concurrent.ScheduledExecutorService executorService)Creates a CompletableFuture that will do nothing and complete after a specified delay, without using a thread during the delay.- Parameters:
delay- The duration of the delay (how much to wait until completing the Future).executorService- An ExecutorService that will be used to complete the Future on.- Returns:
- A CompletableFuture that will complete after the specified delay.
-
delayedFuture
public static <T> java.util.concurrent.CompletableFuture<T> delayedFuture(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> task, long delay, java.util.concurrent.ScheduledExecutorService executorService)Executes the asynchronous task after the specified delay.- Type Parameters:
T- Type parameter.- Parameters:
task- Asynchronous task.delay- Delay in milliseconds.executorService- Executor on which to execute the task.- Returns:
- A CompletableFuture that will be completed with the result of the given task.
-
delayedTask
public static <T> java.util.concurrent.CompletableFuture<T> delayedTask(java.util.function.Supplier<T> task, java.time.Duration delay, java.util.concurrent.ScheduledExecutorService executorService)Executes the given task after the specified delay.- Type Parameters:
T- Type parameter.- Parameters:
task- Asynchronous task.delay- Delay.executorService- Executor on which to execute the task.- Returns:
- A CompletableFuture that will be completed with the result of the given task, or completed exceptionally if the task fails.
-
runOrFail
public static <T,R> R runOrFail(java.util.concurrent.Callable<R> callable, java.util.concurrent.CompletableFuture<T> future)Runs the provided Callable in the current thread (synchronously) if it throws any Exception or Throwable the exception is propagated, but the supplied future is also failed.- Type Parameters:
T- The type of the future.R- The return type of the callable.- Parameters:
callable- The function to invoke.future- The future to fail if the function fails.- Returns:
- The return value of the function.
-
loop
public static <T> java.util.concurrent.CompletableFuture<java.lang.Void> loop(java.lang.Iterable<T> iterable, java.util.function.Function<T,java.util.concurrent.CompletableFuture<java.lang.Boolean>> loopBody, java.util.concurrent.Executor executor)Executes a loop using CompletableFutures over the given Iterable, processing each item in order, without overlap, using the given Executor for task execution.- Type Parameters:
T- Type of items in the given iterable.- Parameters:
iterable- An Iterable instance to loop over.loopBody- A Function that, when applied to an element in the given iterable, returns a CompletableFuture which will complete when the given element has been processed. This function is invoked every time the loopBody needs to execute.executor- An Executor that is used to execute the condition and the loop support code.- Returns:
- A CompletableFuture that, when completed, indicates the loop terminated without any exception. If the loopBody throws/returns Exceptions, these will be set as the result of this returned Future.
-
loop
public static java.util.concurrent.CompletableFuture<java.lang.Void> loop(java.util.function.Supplier<java.lang.Boolean> condition, java.util.function.Supplier<java.util.concurrent.CompletableFuture<java.lang.Void>> loopBody, java.util.concurrent.Executor executor)Executes a loop using CompletableFutures, without invoking join()/get() on any of them or exclusively hogging a thread.- Parameters:
condition- A Supplier that indicates whether to proceed with the loop or not.loopBody- A Supplier that returns a CompletableFuture which represents the body of the loop. This supplier is invoked every time the loopBody needs to execute.executor- An Executor that is used to execute the condition and the loop support code.- Returns:
- A CompletableFuture that, when completed, indicates the loop terminated without any exception. If either the loopBody or condition throw/return Exceptions, these will be set as the result of this returned Future.
-
loop
public static <T> java.util.concurrent.CompletableFuture<java.lang.Void> loop(java.util.function.Supplier<java.lang.Boolean> condition, java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> loopBody, java.util.function.Consumer<T> resultConsumer, java.util.concurrent.Executor executor)Executes a loop using CompletableFutures, without invoking join()/get() on any of them or exclusively hogging a thread.- Type Parameters:
T- The Type of the future's result.- Parameters:
condition- A Supplier that indicates whether to proceed with the loop or not.loopBody- A Supplier that returns a CompletableFuture which represents the body of the loop. This supplier is invoked every time the loopBody needs to execute.resultConsumer- A Consumer that will be invoked with the result of every call to loopBody.executor- An Executor that is used to execute the condition and the loop support code.- Returns:
- A CompletableFuture that, when completed, indicates the loop terminated without any exception. If either the loopBody or condition throw/return Exceptions, these will be set as the result of this returned Future.
-
doWhileLoop
public static <T> java.util.concurrent.CompletableFuture<java.lang.Void> doWhileLoop(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> loopBody, java.util.function.Predicate<T> condition, java.util.concurrent.Executor executor)Executes a code fragment returning a CompletableFutures while a condition on the returned value is satisfied.- Type Parameters:
T- Return type of the executor.- Parameters:
condition- Predicate that indicates whether to proceed with the loop or not.loopBody- A Supplier that returns a CompletableFuture which represents the body of the loop. This supplier is invoked every time the loopBody needs to execute.executor- An Executor that is used to execute the condition and the loop support code.- Returns:
- A CompletableFuture that, when completed, indicates the loop terminated without any exception. If either the loopBody or condition throw/return Exceptions, these will be set as the result of this returned Future.
-
-