Class CompletableFutures
- java.lang.Object
-
- com.spotify.futures.CompletableFutures
-
public final class CompletableFutures extends java.lang.ObjectA collection of static utility methods that extend theJava completable futureAPI.- Since:
- 0.1.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> java.util.concurrent.CompletableFuture<java.util.List<T>>allAsList(java.util.List<? extends java.util.concurrent.CompletionStage<? extends T>> stages)Returns a newCompletableFuturewhich completes to a list of all values of its input stages, if all succeed.static <U,T>
java.util.concurrent.CompletableFuture<java.util.Map<U,T>>allAsMap(java.util.Map<U,? extends java.util.concurrent.CompletionStage<? extends T>> map)Returns a newCompletableFuturewhich completes to a map of all values of its input stages, if all succeed.static voidcheckCompleted(java.util.concurrent.CompletionStage<?> stage)Checks that a stage is completed.static <R,A,B,C>
java.util.concurrent.CompletionStage<R>combine(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, Function3<A,B,C,R> function)Combines multiple stages by applying a function.static <R,A,B,C,D>
java.util.concurrent.CompletionStage<R>combine(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, java.util.concurrent.CompletionStage<D> d, Function4<A,B,C,D,R> function)Combines multiple stages by applying a function.static <R,A,B,C,D,E>
java.util.concurrent.CompletionStage<R>combine(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, java.util.concurrent.CompletionStage<D> d, java.util.concurrent.CompletionStage<E> e, Function5<A,B,C,D,E,R> function)Combines multiple stages by applying a function.static <R,A,B,C,D,E,F>
java.util.concurrent.CompletionStage<R>combine(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, java.util.concurrent.CompletionStage<D> d, java.util.concurrent.CompletionStage<E> e, java.util.concurrent.CompletionStage<F> f, Function6<A,B,C,D,E,F,R> function)Combines multiple stages by applying a function.static <R,A,B>
java.util.concurrent.CompletionStage<R>combine(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.function.BiFunction<A,B,R> function)Combines multiple stages by applying a function.static <T> java.util.concurrent.CompletionStage<T>combine(java.util.function.Function<CombinedFutures,T> function, java.util.concurrent.CompletionStage<?>... stages)Combines multiple stages by applying a function.static <T> java.util.concurrent.CompletionStage<T>combine(java.util.function.Function<CombinedFutures,T> function, java.util.List<? extends java.util.concurrent.CompletionStage<?>> stages)Combines multiple stages by applying a function.static <R,A,B,C>
java.util.concurrent.CompletionStage<R>combineFutures(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, Function3<A,B,C,java.util.concurrent.CompletionStage<R>> function)Composes multiple stages into another stage using a function.static <R,A,B,C,D>
java.util.concurrent.CompletionStage<R>combineFutures(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, java.util.concurrent.CompletionStage<D> d, Function4<A,B,C,D,java.util.concurrent.CompletionStage<R>> function)Composes multiple stages into another stage using a function.static <R,A,B,C,D,E>
java.util.concurrent.CompletionStage<R>combineFutures(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, java.util.concurrent.CompletionStage<D> d, java.util.concurrent.CompletionStage<E> e, Function5<A,B,C,D,E,java.util.concurrent.CompletionStage<R>> function)Composes multiple stages into another stage using a function.static <R,A,B,C,D,E,F>
java.util.concurrent.CompletionStage<R>combineFutures(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, java.util.concurrent.CompletionStage<D> d, java.util.concurrent.CompletionStage<E> e, java.util.concurrent.CompletionStage<F> f, Function6<A,B,C,D,E,F,java.util.concurrent.CompletionStage<R>> function)Composes multiple stages into another stage using a function.static <R,A,B>
java.util.concurrent.CompletionStage<R>combineFutures(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.function.BiFunction<A,B,java.util.concurrent.CompletionStage<R>> function)Composes multiple stages into another stage using a function.static <T> java.util.concurrent.CompletionStage<T>dereference(java.util.concurrent.CompletionStage<? extends java.util.concurrent.CompletionStage<T>> stage)This takes a stage of a stage of a value and returns a plain stage of a value.static <T> java.util.concurrent.CompletableFuture<T>exceptionallyCompletedFuture(java.lang.Throwable throwable)Returns a newCompletableFuturethat is already exceptionally completed with the given exception.static <T> java.util.concurrent.CompletionStage<T>exceptionallyCompose(java.util.concurrent.CompletionStage<T> stage, java.util.function.Function<java.lang.Throwable,? extends java.util.concurrent.CompletionStage<T>> fn)Returns a new stage that, when this stage completes exceptionally, is executed with this stage's exception as the argument to the supplied function.static <T> TgetCompleted(java.util.concurrent.CompletionStage<T> stage)Gets the value of a completed stage.static <T> java.lang.ThrowablegetException(java.util.concurrent.CompletionStage<T> stage)Gets the exception from an exceptionally completed futurestatic <T,U>
java.util.concurrent.CompletionStage<U>handleCompose(java.util.concurrent.CompletionStage<T> stage, java.util.function.BiFunction<? super T,java.lang.Throwable,? extends java.util.concurrent.CompletionStage<U>> fn)Returns a new stage that, when this stage completes either normally or exceptionally, is executed with this stage's result and exception as arguments to the supplied function.static <T,S extends java.util.concurrent.CompletionStage<? extends T>>
java.util.stream.Collector<S,?,java.util.concurrent.CompletableFuture<java.util.List<T>>>joinList()Collect a stream ofCompletionStages into a single future holding a list of the joined entities.static <T,K,V>
java.util.stream.Collector<T,?,java.util.concurrent.CompletableFuture<java.util.Map<K,V>>>joinMap(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends java.util.concurrent.CompletionStage<? extends V>> valueFutureMapper)Collect a stream of Objects into a single future holding a Map whose keys are the result of applying the provided mapping function to the input elements, and whose values are the corresponding joined entities.static <T> java.util.concurrent.CompletableFuture<T>poll(java.util.function.Supplier<java.util.Optional<T>> pollingTask, java.time.Duration frequency, java.util.concurrent.ScheduledExecutorService executorService)Polls an external resource periodically until it returns a non-empty result.static <T> java.util.concurrent.CompletableFuture<java.util.List<T>>successfulAsList(java.util.List<? extends java.util.concurrent.CompletionStage<T>> stages, java.util.function.Function<java.lang.Throwable,? extends T> defaultValueMapper)Returns a newCompletableFuturewhich completes to a list of values of those input stages that succeeded.
-
-
-
Method Detail
-
allAsList
public static <T> java.util.concurrent.CompletableFuture<java.util.List<T>> allAsList(java.util.List<? extends java.util.concurrent.CompletionStage<? extends T>> stages)
Returns a newCompletableFuturewhich completes to a list of all values of its input stages, if all succeed. The list of results is in the same order as the input stages.As soon as any of the given stages complete exceptionally, then the returned future also does so, with a
CompletionExceptionholding this exception as its cause.If no stages are provided, returns a future holding an empty list.
- Type Parameters:
T- the common super-type of all of the input stages, that determines the monomorphic type of the output future- Parameters:
stages- the stages to combine- Returns:
- a future that completes to a list of the results of the supplied stages
- Throws:
java.lang.NullPointerException- if the stages list or any of its elements arenull- Since:
- 0.1.0
-
allAsMap
public static <U,T> java.util.concurrent.CompletableFuture<java.util.Map<U,T>> allAsMap(java.util.Map<U,? extends java.util.concurrent.CompletionStage<? extends T>> map)
Returns a newCompletableFuturewhich completes to a map of all values of its input stages, if all succeed.If any of the given stages complete exceptionally, then the returned future also does so, with a
CompletionExceptionholding this exception as its cause.If no stages are provided, returns a future holding an empty map.
- Type Parameters:
U- the common super-type of the keysT- the common super-type of all of the input value-stages, that determines the monomorphic type of the output future- Parameters:
map- the map of stages to combine- Returns:
- a future that completes to a map of the results of the supplied keys and value-stages
- Throws:
java.lang.NullPointerException- if value-stages or any of its elements arenull- Since:
- 0.3.3
-
successfulAsList
public static <T> java.util.concurrent.CompletableFuture<java.util.List<T>> successfulAsList(java.util.List<? extends java.util.concurrent.CompletionStage<T>> stages, java.util.function.Function<java.lang.Throwable,? extends T> defaultValueMapper)Returns a newCompletableFuturewhich completes to a list of values of those input stages that succeeded. The list of results is in the same order as the input stages. For failed stages, the defaultValueMapper will be called, and the value returned from that function will be put in the resulting list.If no stages are provided, returns a future holding an empty list.
- Type Parameters:
T- the common type of all of the input stages, that determines the type of the output future- Parameters:
stages- the stages to combine.defaultValueMapper- a function that will be called when a future completes exceptionally to provide a default value to place in the resulting list- Returns:
- a future that completes to a list of the results of the supplied stages
- Throws:
java.lang.NullPointerException- if the stages list or any of its elements arenull
-
exceptionallyCompletedFuture
public static <T> java.util.concurrent.CompletableFuture<T> exceptionallyCompletedFuture(java.lang.Throwable throwable)
Returns a newCompletableFuturethat is already exceptionally completed with the given exception.- Type Parameters:
T- an arbitrary type for the returned future; can be anything since the future will be exceptionally completed and thus there will never be a value of typeT- Parameters:
throwable- the exception- Returns:
- a future that exceptionally completed with the supplied exception
- Throws:
java.lang.NullPointerException- if the supplied throwable isnull- Since:
- 0.1.0
-
joinList
public static <T,S extends java.util.concurrent.CompletionStage<? extends T>> java.util.stream.Collector<S,?,java.util.concurrent.CompletableFuture<java.util.List<T>>> joinList()
Collect a stream ofCompletionStages into a single future holding a list of the joined entities.Usage:
collection.stream() .map(this::someAsyncFunc) .collect(joinList()) .thenApply(this::consumeList)The generated
CompletableFuturewill complete to a list of all entities, in the order they were encountered in the original stream. Similar toCompletableFuture.allOf(CompletableFuture[]), if any of the input futures complete exceptionally, then the returned CompletableFuture also does so, with aCompletionExceptionholding this exception as its cause.- Type Parameters:
T- the common super-type of all of the input stages, that determines the monomorphic type of the output futureS- the implementation ofCompletionStagethat the stream contains- Returns:
- a new
CompletableFutureaccording to the rules outlined in the method description - Throws:
java.lang.NullPointerException- if any future in the stream isnull- Since:
- 0.1.0
-
joinMap
public static <T,K,V> java.util.stream.Collector<T,?,java.util.concurrent.CompletableFuture<java.util.Map<K,V>>> joinMap(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends java.util.concurrent.CompletionStage<? extends V>> valueFutureMapper)Collect a stream of Objects into a single future holding a Map whose keys are the result of applying the provided mapping function to the input elements, and whose values are the corresponding joined entities.Usage:
collection.stream() .collect(joinMap(this::toKey, this::someAsyncFunc)) .thenApply(this::consumeMap)The generated
CompletableFuturewill complete to a Map of all entities. Similar toCompletableFuture.allOf(CompletableFuture[]), if any of the input futures complete exceptionally, then the returned CompletableFuture also does so, with aCompletionExceptionholding this exception as its cause.- Type Parameters:
T- the type of the input elementsK- the output type of the key mapping functionV- the common super-type of the stages returned by the value future mapping function- Parameters:
keyMapper- a mapping function to produce keysvalueFutureMapper- a mapping function to produce futures that will eventually produce the values- Returns:
- a new
CompletableFutureaccording to the rules outlined in the method description - Throws:
java.lang.NullPointerException- if valueFutureMapper returnsnullfor any input element- Since:
- 0.3.4
-
checkCompleted
public static void checkCompleted(java.util.concurrent.CompletionStage<?> stage)
Checks that a stage is completed.- Parameters:
stage- theCompletionStageto check- Throws:
java.lang.IllegalStateException- if the stage is not completed- Since:
- 0.1.0
-
getCompleted
public static <T> T getCompleted(java.util.concurrent.CompletionStage<T> stage)
Gets the value of a completed stage.- Type Parameters:
T- the type of the value that the stage completes into- Parameters:
stage- a completedCompletionStage- Returns:
- the value of the stage if it has one
- Throws:
java.lang.IllegalStateException- if the stage is not completed- Since:
- 0.1.0
-
getException
public static <T> java.lang.Throwable getException(java.util.concurrent.CompletionStage<T> stage)
Gets the exception from an exceptionally completed future- Type Parameters:
T- the type of the value that the stage completes into- Parameters:
stage- an exceptionally completedCompletionStage- Returns:
- the exception the stage has completed with
- Throws:
java.lang.IllegalStateException- if the stage is not completed exceptionallyjava.util.concurrent.CancellationException- if the stage was cancelledjava.lang.UnsupportedOperationException- if theCompletionStagedoes not support theCompletionStage.toCompletableFuture()operation
-
handleCompose
public static <T,U> java.util.concurrent.CompletionStage<U> handleCompose(java.util.concurrent.CompletionStage<T> stage, java.util.function.BiFunction<? super T,java.lang.Throwable,? extends java.util.concurrent.CompletionStage<U>> fn)Returns a new stage that, when this stage completes either normally or exceptionally, is executed with this stage's result and exception as arguments to the supplied function.When this stage is complete, the given function is invoked with the result (or
nullif none) and the exception (ornullif none) of this stage as arguments, and the function's result is used to complete the returned stage.This differs from
CompletionStage.handle(java.util.function.BiFunction)in that the function should return aCompletionStagerather than the value directly.- Type Parameters:
T- the type of the input stage's value.U- the function's return type- Parameters:
stage- theCompletionStageto composefn- the function to use to compute the value of the returnedCompletionStage- Returns:
- the new
CompletionStage - Since:
- 0.1.0
-
exceptionallyCompose
public static <T> java.util.concurrent.CompletionStage<T> exceptionallyCompose(java.util.concurrent.CompletionStage<T> stage, java.util.function.Function<java.lang.Throwable,? extends java.util.concurrent.CompletionStage<T>> fn)Returns a new stage that, when this stage completes exceptionally, is executed with this stage's exception as the argument to the supplied function. Otherwise, if this stage completes normally, then the returned stage also completes normally with the same value.This differs from
CompletionStage.exceptionally(java.util.function.Function)in that the function should return aCompletionStagerather than the value directly.- Type Parameters:
T- the type of the input stage's value.- Parameters:
stage- theCompletionStageto composefn- the function to use to compute the value of the returnedCompletionStageif this stage completed exceptionally- Returns:
- the new
CompletionStage - Since:
- 0.1.0
-
dereference
public static <T> java.util.concurrent.CompletionStage<T> dereference(java.util.concurrent.CompletionStage<? extends java.util.concurrent.CompletionStage<T>> stage)
This takes a stage of a stage of a value and returns a plain stage of a value.- Type Parameters:
T- the type of the inner stage's value.- Parameters:
stage- aCompletionStageof aCompletionStageof a value- Returns:
- the
CompletionStageof the value - Since:
- 0.1.0
-
combine
public static <R,A,B> java.util.concurrent.CompletionStage<R> combine(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.function.BiFunction<A,B,R> function)Combines multiple stages by applying a function.- Type Parameters:
R- the type of the combining function's return value.A- the type of the first stage's value.B- the type of the second stage's value.- Parameters:
a- the first stage.b- the second stage.function- the combining function.- Returns:
- a stage that completes into the return value of the supplied function.
- Since:
- 0.1.0
-
combine
public static <R,A,B,C> java.util.concurrent.CompletionStage<R> combine(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, Function3<A,B,C,R> function)Combines multiple stages by applying a function.- Type Parameters:
R- the type of the combining function's return value.A- the type of the first stage's value.B- the type of the second stage's value.C- the type of the third stage's value.- Parameters:
a- the first stage.b- the second stage.c- the third stage.function- the combining function.- Returns:
- a stage that completes into the return value of the supplied function.
- Since:
- 0.1.0
-
combine
public static <R,A,B,C,D> java.util.concurrent.CompletionStage<R> combine(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, java.util.concurrent.CompletionStage<D> d, Function4<A,B,C,D,R> function)Combines multiple stages by applying a function.- Type Parameters:
R- the type of the combining function's return value.A- the type of the first stage's value.B- the type of the second stage's value.C- the type of the third stage's value.D- the type of the fourth stage's value.- Parameters:
a- the first stage.b- the second stage.c- the third stage.d- the fourth stage.function- the combining function.- Returns:
- a stage that completes into the return value of the supplied function.
- Since:
- 0.1.0
-
combine
public static <R,A,B,C,D,E> java.util.concurrent.CompletionStage<R> combine(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, java.util.concurrent.CompletionStage<D> d, java.util.concurrent.CompletionStage<E> e, Function5<A,B,C,D,E,R> function)Combines multiple stages by applying a function.- Type Parameters:
R- the type of the combining function's return value.A- the type of the first stage's value.B- the type of the second stage's value.C- the type of the third stage's value.D- the type of the fourth stage's value.E- the type of the fifth stage's value.- Parameters:
a- the first stage.b- the second stage.c- the third stage.d- the fourth stage.e- the fifth stage.function- the combining function.- Returns:
- a stage that completes into the return value of the supplied function.
- Since:
- 0.1.0
-
combine
public static <R,A,B,C,D,E,F> java.util.concurrent.CompletionStage<R> combine(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, java.util.concurrent.CompletionStage<D> d, java.util.concurrent.CompletionStage<E> e, java.util.concurrent.CompletionStage<F> f, Function6<A,B,C,D,E,F,R> function)Combines multiple stages by applying a function.- Type Parameters:
R- the type of the combining function's return value.A- the type of the first stage's value.B- the type of the second stage's value.C- the type of the third stage's value.D- the type of the fourth stage's value.E- the type of the fifth stage's value.F- the type of the sixth stage's value.- Parameters:
a- the first stage.b- the second stage.c- the third stage.d- the fourth stage.e- the fifth stage.f- the sixth stage.function- the combining function.- Returns:
- a stage that completes into the return value of the supplied function.
- Since:
- 0.3.2
-
combine
public static <T> java.util.concurrent.CompletionStage<T> combine(java.util.function.Function<CombinedFutures,T> function, java.util.concurrent.CompletionStage<?>... stages)
Combines multiple stages by applying a function.- Type Parameters:
T- the type of the combining function's return value.- Parameters:
function- the combining function.stages- the stages to combine- Returns:
- a stage that completes into the return value of the supplied function.
- Since:
- 0.4.0
-
combine
public static <T> java.util.concurrent.CompletionStage<T> combine(java.util.function.Function<CombinedFutures,T> function, java.util.List<? extends java.util.concurrent.CompletionStage<?>> stages)
Combines multiple stages by applying a function.- Type Parameters:
T- the type of the combining function's return value.- Parameters:
function- the combining function.stages- the stages to combine- Returns:
- a stage that completes into the return value of the supplied function.
- Since:
- 0.4.0
-
combineFutures
public static <R,A,B> java.util.concurrent.CompletionStage<R> combineFutures(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.function.BiFunction<A,B,java.util.concurrent.CompletionStage<R>> function)Composes multiple stages into another stage using a function.- Type Parameters:
R- the type of the composedCompletionStage.A- the type of the first stage's value.B- the type of the second stage's value.- Parameters:
a- the first stage.b- the second stage.function- the combining function.- Returns:
- a stage that is composed from the input stages using the function.
- Throws:
java.lang.UnsupportedOperationException- if any of theCompletionStages do not interoperate with CompletableFuture
-
combineFutures
public static <R,A,B,C> java.util.concurrent.CompletionStage<R> combineFutures(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, Function3<A,B,C,java.util.concurrent.CompletionStage<R>> function)Composes multiple stages into another stage using a function.- Type Parameters:
R- the type of the composedCompletionStage.A- the type of the first stage's value.B- the type of the second stage's value.C- the type of the third stage's value.- Parameters:
a- the first stage.b- the second stage.c- the third stage.function- the combining function.- Returns:
- a stage that is composed from the input stages using the function.
- Throws:
java.lang.UnsupportedOperationException- if any of theCompletionStages do not interoperate with CompletableFuture
-
combineFutures
public static <R,A,B,C,D> java.util.concurrent.CompletionStage<R> combineFutures(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, java.util.concurrent.CompletionStage<D> d, Function4<A,B,C,D,java.util.concurrent.CompletionStage<R>> function)Composes multiple stages into another stage using a function.- Type Parameters:
R- the type of the composedCompletionStage.A- the type of the first stage's value.B- the type of the second stage's value.C- the type of the third stage's value.D- the type of the fourth stage's value.- Parameters:
a- the first stage.b- the second stage.c- the third stage.d- the fourth stage.function- the combining function.- Returns:
- a stage that is composed from the input stages using the function.
- Throws:
java.lang.UnsupportedOperationException- if any of theCompletionStages do not interoperate with CompletableFuture
-
combineFutures
public static <R,A,B,C,D,E> java.util.concurrent.CompletionStage<R> combineFutures(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, java.util.concurrent.CompletionStage<D> d, java.util.concurrent.CompletionStage<E> e, Function5<A,B,C,D,E,java.util.concurrent.CompletionStage<R>> function)Composes multiple stages into another stage using a function.- Type Parameters:
R- the type of the composedCompletionStage.A- the type of the first stage's value.B- the type of the second stage's value.C- the type of the third stage's value.D- the type of the fourth stage's value.E- the type of the fifth stage's value.- Parameters:
a- the first stage.b- the second stage.c- the third stage.d- the fourth stage.e- the fifth stage.function- the combining function.- Returns:
- a stage that is composed from the input stages using the function.
- Throws:
java.lang.UnsupportedOperationException- if any of theCompletionStages do not interoperate with CompletableFuture
-
combineFutures
public static <R,A,B,C,D,E,F> java.util.concurrent.CompletionStage<R> combineFutures(java.util.concurrent.CompletionStage<A> a, java.util.concurrent.CompletionStage<B> b, java.util.concurrent.CompletionStage<C> c, java.util.concurrent.CompletionStage<D> d, java.util.concurrent.CompletionStage<E> e, java.util.concurrent.CompletionStage<F> f, Function6<A,B,C,D,E,F,java.util.concurrent.CompletionStage<R>> function)Composes multiple stages into another stage using a function.- Type Parameters:
R- the type of the composedCompletionStage.A- the type of the first stage's value.B- the type of the second stage's value.C- the type of the third stage's value.D- the type of the fourth stage's value.E- the type of the fifth stage's value.F- the type of the sixth stage's value.- Parameters:
a- the first stage.b- the second stage.c- the third stage.d- the fourth stage.e- the fifth stage.f- the sixth stage.function- the combining function.- Returns:
- a stage that is composed from the input stages using the function.
- Throws:
java.lang.UnsupportedOperationException- if any of theCompletionStages do not interoperate with CompletableFuture
-
poll
public static <T> java.util.concurrent.CompletableFuture<T> poll(java.util.function.Supplier<java.util.Optional<T>> pollingTask, java.time.Duration frequency, java.util.concurrent.ScheduledExecutorService executorService)Polls an external resource periodically until it returns a non-empty result.The polling task should return
Optional.empty()until it becomes available, and thenOptional.of(result). If the polling task throws an exception or returns null, that will cause the result future to complete exceptionally.Canceling the returned future will cancel the scheduled polling task as well.
Note that on a ScheduledThreadPoolExecutor the polling task might remain allocated for up to
frequencytime after completing or being cancelled. If you have lots of polling operations or a long polling frequency, consider settingremoveOnCancelPolicyto true. SeeScheduledThreadPoolExecutor.setRemoveOnCancelPolicy(boolean).- Type Parameters:
T- the type of the result of the polling task, that will be returned when the task succeeds.- Parameters:
pollingTask- the polling taskfrequency- the frequency to run the polling task atexecutorService- the executor service to schedule the polling task on- Returns:
- a future completing to the result of the polling task once that becomes available
-
-