public class FutureUtils
extends java.lang.Object
Generating already done futures:
Tools for blocking:
blockTillAllComplete(Iterable)
blockTillAllComplete(Iterable, long)
blockTillAllCompleteOrFirstError(Iterable)
blockTillAllCompleteOrFirstError(Iterable, long)
Tools for manipulating collections of futures:
cancelIncompleteFutures(Iterable, boolean)
cancelIncompleteFuturesIfAnyFail(boolean, Iterable, boolean)
countFuturesWithResult(Iterable, Object)
countFuturesWithResult(Iterable, Object, long)
makeCompleteFuture(Iterable)
makeCompleteFuture(Iterable, Object)
makeFailurePropagatingCompleteFuture(Iterable)
makeFailurePropagatingCompleteFuture(Iterable, Object)
makeCompleteListFuture(Iterable)
makeFailureListFuture(Iterable)
makeResultListFuture(Iterable, boolean)
makeSuccessListFuture(Iterable)
Retry operation and return final result in future:
scheduleWhile(SubmitterScheduler, long, boolean, Callable, Predicate)
scheduleWhile(SubmitterScheduler, long, boolean, Callable, Predicate, long, boolean)
scheduleWhile(SubmitterScheduler, long, ListenableFuture, Callable, Predicate)
scheduleWhile(SubmitterScheduler, long, ListenableFuture, Callable, Predicate, long, boolean)
scheduleWhile(SubmitterScheduler, long, boolean, Runnable, Supplier)
scheduleWhile(SubmitterScheduler, long, boolean, Runnable, Supplier, long)
executeWhile(Callable, Predicate)
executeWhile(Callable, Predicate, long, boolean)
executeWhile(ListenableFuture, Callable, Predicate)
executeWhile(ListenableFuture, Callable, Predicate, long, boolean)
Constructor and Description |
---|
FutureUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
blockTillAllComplete(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures)
This call blocks till all futures in the list have completed.
|
static void |
blockTillAllComplete(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures,
long timeoutInMillis)
This call blocks till all futures in the list have completed.
|
static void |
blockTillAllCompleteOrFirstError(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures)
This call blocks till all futures in the list have completed.
|
static void |
blockTillAllCompleteOrFirstError(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures,
long timeoutInMillis)
This call blocks till all futures in the list have completed.
|
static void |
cancelIncompleteFutures(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures,
boolean interruptThread)
Invoked
Future.cancel(boolean) for every future in this collection. |
static void |
cancelIncompleteFuturesIfAnyFail(boolean copy,
java.lang.Iterable<? extends ListenableFuture<?>> futures,
boolean interruptThread)
Provide a group of futures and cancel all of them if any of them are canceled or fail.
|
static <T> int |
countFuturesWithResult(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures,
T comparisonResult)
Counts how many futures provided completed with a result that matches the one provided here.
|
static <T> int |
countFuturesWithResult(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures,
T comparisonResult,
long timeoutInMillis)
Counts how many futures provided completed with a result that matches the one provided here.
|
static <T> ListenableFuture<T> |
executeWhile(java.util.concurrent.Callable<? extends ListenableFuture<? extends T>> asyncTask,
java.util.function.Predicate<? super T> loopTest)
Similar to
scheduleWhile(SubmitterScheduler, long, boolean, Callable, Predicate)
except that no executor is needed because the callable instead will return a future from the
provided async submission (which may be a scheduled task or otherwise). |
static <T> ListenableFuture<T> |
executeWhile(java.util.concurrent.Callable<? extends ListenableFuture<? extends T>> asyncTask,
java.util.function.Predicate<? super T> loopTest,
long timeoutMillis,
boolean timeoutProvideLastValue)
Similar to
scheduleWhile(SubmitterScheduler, long, boolean, Callable, Predicate, long, boolean)
except that no executor is needed because the callable instead will return a future from the
provided async submission (which may be a scheduled task or otherwise). |
static <T> ListenableFuture<T> |
executeWhile(ListenableFuture<? extends T> startingFuture,
java.util.concurrent.Callable<? extends ListenableFuture<? extends T>> asyncTask,
java.util.function.Predicate<? super T> loopTest)
Similar to
scheduleWhile(SubmitterScheduler, long, ListenableFuture, Callable, Predicate)
except that no executor is needed because the callable instead will return a future from the
provided async submission (which may be a scheduled task or otherwise). |
static <T> ListenableFuture<T> |
executeWhile(ListenableFuture<? extends T> startingFuture,
java.util.concurrent.Callable<? extends ListenableFuture<? extends T>> asyncTask,
java.util.function.Predicate<? super T> loopTest,
long timeoutMillis,
boolean timeoutProvideLastValue)
Similar to
scheduleWhile(SubmitterScheduler, long, ListenableFuture, Callable, Predicate, long, boolean)
except that no executor is needed because the callable instead will return a future from the
provided async submission (which may be a scheduled task or otherwise). |
static <T> ListenableFuture<T> |
immediateFailureFuture(java.lang.Throwable failure)
Constructs a
ListenableFuture that has failed with the given failure. |
static <T> ListenableFuture<T> |
immediateResultFuture(T result)
Constructs a
ListenableFuture that has already had the provided result given to it. |
static ListenableFuture<?> |
makeCompleteFuture(java.lang.Iterable<? extends ListenableFuture<?>> futures)
An alternative to
blockTillAllComplete(Iterable) , this provides the ability to know
when all futures are complete without blocking. |
static <T> ListenableFuture<T> |
makeCompleteFuture(java.lang.Iterable<? extends ListenableFuture<?>> futures,
T result)
An alternative to
blockTillAllComplete(Iterable) , this provides the ability to know
when all futures are complete without blocking. |
static ListenableFuture<?> |
makeCompleteFuture(java.util.List<? extends ListenableFuture<?>> futures)
An alternative to
blockTillAllComplete(Iterable) , this provides the ability to know
when all futures are complete without blocking. |
static <T> ListenableFuture<java.util.List<ListenableFuture<? extends T>>> |
makeCompleteListFuture(java.lang.Iterable<? extends ListenableFuture<? extends T>> futures)
This call is similar to
makeCompleteFuture(Iterable) in that it will immediately
provide a future that will not be satisfied till all provided futures complete. |
static <T> ListenableFuture<java.util.List<ListenableFuture<? extends T>>> |
makeFailureListFuture(java.lang.Iterable<? extends ListenableFuture<? extends T>> futures)
This call is similar to
makeCompleteFuture(Iterable) in that it will immediately
provide a future that will not be satisfied till all provided futures complete. |
static ListenableFuture<?> |
makeFailurePropagatingCompleteFuture(java.lang.Iterable<? extends ListenableFuture<?>> futures)
Similar to
makeCompleteFuture(Iterable) in that the returned future wont complete
until all the provided futures complete. |
static <T> ListenableFuture<T> |
makeFailurePropagatingCompleteFuture(java.lang.Iterable<? extends ListenableFuture<?>> futures,
T result)
Similar to
makeCompleteFuture(Iterable, Object) in that the returned future wont
complete until all the provided futures complete. |
static <T> ListenableFuture<java.util.List<T>> |
makeResultListFuture(java.lang.Iterable<? extends ListenableFuture<? extends T>> futures,
boolean ignoreFailedFutures)
This returns a future which provides the results of all the provided futures.
|
static <T> ListenableFuture<java.util.List<ListenableFuture<? extends T>>> |
makeSuccessListFuture(java.lang.Iterable<? extends ListenableFuture<? extends T>> futures)
This call is similar to
makeCompleteFuture(Iterable) in that it will immediately
provide a future that will not be satisfied till all provided futures complete. |
static <T> ListenableFuture<T> |
scheduleWhile(SubmitterScheduler scheduler,
long scheduleDelayMillis,
boolean firstRunAsync,
java.util.concurrent.Callable<? extends T> task,
java.util.function.Predicate<? super T> loopTest)
Executes a task, checking the result from the task to see if it needs to reschedule the task
again.
|
static <T> ListenableFuture<T> |
scheduleWhile(SubmitterScheduler scheduler,
long scheduleDelayMillis,
boolean firstRunAsync,
java.util.concurrent.Callable<? extends T> task,
java.util.function.Predicate<? super T> loopTest,
long timeoutMillis,
boolean timeoutProvideLastValue)
Executes a task, checking the result from the task to see if it needs to reschedule the task
again.
|
static ListenableFuture<?> |
scheduleWhile(SubmitterScheduler scheduler,
long scheduleDelayMillis,
boolean firstRunAsync,
java.lang.Runnable task,
java.util.function.Supplier<java.lang.Boolean> loopTest)
Executes a task until the provided supplier returns
false . |
static ListenableFuture<?> |
scheduleWhile(SubmitterScheduler scheduler,
long scheduleDelayMillis,
boolean firstRunAsync,
java.lang.Runnable task,
java.util.function.Supplier<java.lang.Boolean> loopTest,
long timeoutMillis)
Executes a task, checking the result from the task to see if it needs to reschedule the task
again.
|
static <T> ListenableFuture<T> |
scheduleWhile(SubmitterScheduler scheduler,
long scheduleDelayMillis,
ListenableFuture<? extends T> startingFuture,
java.util.concurrent.Callable<? extends T> task,
java.util.function.Predicate<? super T> loopTest)
Executes a task, checking the result from the task to see if it needs to reschedule the task
again.
|
static <T> ListenableFuture<T> |
scheduleWhile(SubmitterScheduler scheduler,
long scheduleDelayMillis,
ListenableFuture<? extends T> startingFuture,
java.util.concurrent.Callable<? extends T> task,
java.util.function.Predicate<? super T> loopTest,
long timeoutMillis,
boolean timeoutProvideLastValue)
Executes a task, checking the result from the task to see if it needs to reschedule the task
again.
|
static <T> ListenableFuture<T> |
scheduleWhileTaskResultNull(SubmitterScheduler scheduler,
long scheduleDelayMillis,
boolean firstRunAsync,
java.util.concurrent.Callable<? extends T> task)
Deprecated.
|
static <T> ListenableFuture<T> |
scheduleWhileTaskResultNull(SubmitterScheduler scheduler,
long scheduleDelayMillis,
boolean firstRunAsync,
java.util.concurrent.Callable<? extends T> task,
long timeoutMillis)
Deprecated.
Please use with a simple null checking Predicate
scheduleWhile(SubmitterScheduler, long, boolean, Callable, Predicate, long, boolean) |
public static void blockTillAllComplete(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures) throws java.lang.InterruptedException
ExecutionException
is swallowed. Meaning that this does not attempt to
verify that all futures completed successfully. If you need to know if any failed, please
use blockTillAllCompleteOrFirstError(Iterable)
.
If you need to specify a timeout to control how long to block, consider using
blockTillAllComplete(Iterable, long)
.
futures
- Structure of futures to iterate overjava.lang.InterruptedException
- Thrown if thread is interrupted while waiting on futurepublic static void blockTillAllComplete(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures, long timeoutInMillis) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException
ExecutionException
is swallowed. Meaning that this does not attempt to
verify that all futures completed successfully. If you need to know if any failed, please
use blockTillAllCompleteOrFirstError(Iterable, long)
.futures
- Structure of futures to iterate overtimeoutInMillis
- timeout to wait for futures to complete in millisecondsjava.lang.InterruptedException
- Thrown if thread is interrupted while waiting on futurejava.util.concurrent.TimeoutException
- Thrown if the timeout elapsed while waiting on futures to completepublic static void blockTillAllCompleteOrFirstError(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
ExecutionException
is thrown. If this exception is thrown, all futures
may or may not be completed, the exception is thrown as soon as it is hit. There also may be
additional futures that errored (but were not hit yet).
If you need to specify a timeout to control how long to block, consider using
blockTillAllCompleteOrFirstError(Iterable, long)
.
futures
- Structure of futures to iterate overjava.lang.InterruptedException
- Thrown if thread is interrupted while waiting on futurejava.util.concurrent.ExecutionException
- Thrown if future throws exception on .get() callpublic static void blockTillAllCompleteOrFirstError(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures, long timeoutInMillis) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException, java.util.concurrent.ExecutionException
ExecutionException
is thrown. If this exception is thrown, all futures
may or may not be completed, the exception is thrown as soon as it is hit. There also may be
additional futures that errored (but were not hit yet).futures
- Structure of futures to iterate overtimeoutInMillis
- timeout to wait for futures to complete in millisecondsjava.lang.InterruptedException
- Thrown if thread is interrupted while waiting on futurejava.util.concurrent.TimeoutException
- Thrown if the timeout elapsed while waiting on futures to completejava.util.concurrent.ExecutionException
- Thrown if future throws exception on .get() callpublic static <T> int countFuturesWithResult(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures, T comparisonResult) throws java.lang.InterruptedException
ExecutionException
. For example assume an API return's Future<Boolean>
and a
false
represents a failure, this can be used to look for those types of error
results.
Just like blockTillAllComplete(Iterable)
, this will block until all futures have
completed (so we can verify if their result matches or not).
If you need to specify a timeout to control how long to block, consider using
countFuturesWithResult(Iterable, Object, long)
.
T
- type of result futures provide to compare againstfutures
- Structure of futures to iterate overcomparisonResult
- Object to compare future results against to look for matchObject.equals(Object)
comparisonjava.lang.InterruptedException
- Thrown if thread is interrupted while waiting on future's resultpublic static <T> int countFuturesWithResult(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures, T comparisonResult, long timeoutInMillis) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException
ExecutionException
. For example assume an API return's Future<Boolean>
and a
false
represents a failure, this can be used to look for those types of error
results.
Just like blockTillAllComplete(Iterable)
, this will block until all futures have
completed (so we can verify if their result matches or not).
T
- type of result futures provide to compare againstfutures
- Structure of futures to iterate overcomparisonResult
- Object to compare future results against to look for matchtimeoutInMillis
- timeout to wait for futures to complete in millisecondsObject.equals(Object)
comparisonjava.lang.InterruptedException
- Thrown if thread is interrupted while waiting on future's resultjava.util.concurrent.TimeoutException
- Thrown if the timeout elapsed while waiting on futures to completepublic static ListenableFuture<?> makeCompleteFuture(java.util.List<? extends ListenableFuture<?>> futures)
blockTillAllComplete(Iterable)
, this provides the ability to know
when all futures are complete without blocking. Unlike
blockTillAllComplete(Iterable)
, this requires that you provide a collection of
ListenableFuture
's. But will return immediately, providing a new
ListenableFuture
that will be called once all the provided futures have finished.
The future returned will provide a null
result, it is the responsibility of the
caller to get the actual results from the provided futures. This is designed to just be an
indicator as to when they have finished. If you need the results from the provided futures,
consider using makeCompleteListFuture(Iterable)
. You should also consider using
makeFailurePropagatingCompleteFuture(Iterable)
, it has the same semantics as this one
except it will put the returned future into an error state if any of the provided futures error.
futures
- Collection of futures that must finish before returned future is satisfiedpublic static ListenableFuture<?> makeCompleteFuture(java.lang.Iterable<? extends ListenableFuture<?>> futures)
blockTillAllComplete(Iterable)
, this provides the ability to know
when all futures are complete without blocking. Unlike
blockTillAllComplete(Iterable)
, this requires that you provide a collection of
ListenableFuture
's. But will return immediately, providing a new
ListenableFuture
that will be called once all the provided futures have finished.
The future returned will provide a null
result, it is the responsibility of the
caller to get the actual results from the provided futures. This is designed to just be an
indicator as to when they have finished. If you need the results from the provided futures,
consider using makeCompleteListFuture(Iterable)
. You should also consider using
makeFailurePropagatingCompleteFuture(Iterable)
, it has the same semantics as this one
except it will put the returned future into an error state if any of the provided futures error.
futures
- Collection of futures that must finish before returned future is satisfiedpublic static <T> ListenableFuture<T> makeCompleteFuture(java.lang.Iterable<? extends ListenableFuture<?>> futures, T result)
blockTillAllComplete(Iterable)
, this provides the ability to know
when all futures are complete without blocking. Unlike
blockTillAllComplete(Iterable)
, this requires that you provide a collection of
ListenableFuture
's. But will return immediately, providing a new
ListenableFuture
that will be called once all the provided futures have finished.
The future returned will provide the result object once all provided futures have completed.
If any failures occured, they will not be represented in the returned future. If that is
desired you should consider using
makeFailurePropagatingCompleteFuture(Iterable, Object)
, it has the same semantics as
this one except it will put the returned future into an error state if any of the provided
futures error.
T
- type of result returned from the futurefutures
- Collection of futures that must finish before returned future is satisfiedresult
- Result to provide returned future once all futures completepublic static ListenableFuture<?> makeFailurePropagatingCompleteFuture(java.lang.Iterable<? extends ListenableFuture<?>> futures)
makeCompleteFuture(Iterable)
in that the returned future wont complete
until all the provided futures complete. However this implementation will check if any
futures failed or were canceled once all have completed. If any did not complete normally
then the returned futures state will match the state of one of the futures that did not
normally (randomly chosen).
Since the returned future wont complete until all futures complete, you may want to consider
using cancelIncompleteFuturesIfAnyFail(boolean, Iterable, boolean)
in addition to
this so that the future will resolve as soon as any failures occur.
futures
- Collection of futures that must finish before returned future is satisfiedpublic static <T> ListenableFuture<T> makeFailurePropagatingCompleteFuture(java.lang.Iterable<? extends ListenableFuture<?>> futures, T result)
makeCompleteFuture(Iterable, Object)
in that the returned future wont
complete until all the provided futures complete. However this implementation will check if
any futures failed or were canceled once all have completed. If any did not complete normally
then the returned futures state will match the state of one of the futures that did not
normally (randomly chosen).
Since the returned future wont complete until all futures complete, you may want to consider
using cancelIncompleteFuturesIfAnyFail(boolean, Iterable, boolean)
in addition to
this so that the future will resolve as soon as any failures occur.
T
- type of result returned from the futurefutures
- Collection of futures that must finish before returned future is satisfiedresult
- Result to provide returned future once all futures completepublic static <T> ListenableFuture<java.util.List<ListenableFuture<? extends T>>> makeCompleteListFuture(java.lang.Iterable<? extends ListenableFuture<? extends T>> futures)
makeCompleteFuture(Iterable)
in that it will immediately
provide a future that will not be satisfied till all provided futures complete.
This future provides a list of the completed futures as the result. The order of this list is NOT deterministic.
If Future.cancel(boolean)
is invoked on the returned future, all provided
futures will attempt to be canceled in the same way.
T
- The result object type returned from the futuresfutures
- Structure of futures to iterate overpublic static <T> ListenableFuture<java.util.List<ListenableFuture<? extends T>>> makeSuccessListFuture(java.lang.Iterable<? extends ListenableFuture<? extends T>> futures)
makeCompleteFuture(Iterable)
in that it will immediately
provide a future that will not be satisfied till all provided futures complete.
This future provides a list of the futures that completed without throwing an exception nor were canceled. The order of the resulting list is NOT deterministic.
If Future.cancel(boolean)
is invoked on the returned future, all provided
futures will attempt to be canceled in the same way.
T
- The result object type returned from the futuresfutures
- Structure of futures to iterate overpublic static <T> ListenableFuture<java.util.List<ListenableFuture<? extends T>>> makeFailureListFuture(java.lang.Iterable<? extends ListenableFuture<? extends T>> futures)
makeCompleteFuture(Iterable)
in that it will immediately
provide a future that will not be satisfied till all provided futures complete.
This future provides a list of the futures that failed by either throwing an exception or were canceled. The order of the resulting list is NOT deterministic.
If Future.cancel(boolean)
is invoked on the returned future, all provided
futures will attempt to be canceled in the same way.
T
- The result object type returned from the futuresfutures
- Structure of futures to iterate overpublic static <T> ListenableFuture<java.util.List<T>> makeResultListFuture(java.lang.Iterable<? extends ListenableFuture<? extends T>> futures, boolean ignoreFailedFutures)
The order of the result list is NOT deterministic.
If called with true
for ignoreFailedFutures
, even if some of the provided
futures finished in error, they will be ignored and just the successful results will be
provided. If called with false
then if any futures complete in error, then the
returned future will throw a ExecutionException
with the error as the cause when
Future.get()
is invoked. In addition if called with false
and any of the
provided futures are canceled, then the returned future will also be canceled, resulting in a
CancellationException
being thrown when Future.get()
is invoked. In the case
where there is canceled and failed exceptions in the collection, this will prefer to throw the
failure as an ExecutionException
rather than obscure it with a
CancellationException
. In other words CancellationException
will be thrown
ONLY if there was canceled tasks, but NO tasks which finished in error.
T
- The result object type returned from the futuresfutures
- Structure of futures to iterate over and extract results fromignoreFailedFutures
- true
to ignore any failed or canceled futuresListenableFuture
which will provide a list of the results from the provided futurespublic static void cancelIncompleteFutures(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures, boolean interruptThread)
Future.cancel(boolean)
for every future in this collection. Thus if there
are any futures which have not already completed, they will now be marked as canceled.futures
- Collection of futures to iterate through and cancelinterruptThread
- Valued passed in to interrupt thread when calling Future.cancel(boolean)
public static void cancelIncompleteFuturesIfAnyFail(boolean copy, java.lang.Iterable<? extends ListenableFuture<?>> futures, boolean interruptThread)
If false
is provided for copy
parameter, then futures
will be
iterated over twice, once during this invocation, and again when needing to cancel the
futures. Because of that it is critical the Iterable
provided returns the exact same
future contents at the time of invoking this call. If that guarantee can not be provided,
you must specify true
for the copy
parameter.
copy
- true
to copy provided futures to avoidfutures
- Futures to be monitored and canceled on errorinterruptThread
- Valued passed in to interrupt thread when calling Future.cancel(boolean)
public static <T> ListenableFuture<T> immediateResultFuture(T result)
ListenableFuture
that has already had the provided result given to it.
Thus the resulting future can not error, block, or be canceled.
If null
is provided here the static instance of
ImmediateResultListenableFuture.NULL_RESULT
will be returned to reduce GC overhead.
T
- The result object type returned by the returned futureresult
- result to be provided in .get() callpublic static <T> ListenableFuture<T> immediateFailureFuture(java.lang.Throwable failure)
ListenableFuture
that has failed with the given failure. Thus the
resulting future can not block, or be canceled. Calls to Future.get()
will
immediately throw an ExecutionException
.T
- The result object type returned by the returned futurefailure
- to provide as cause for ExecutionException thrown from .get() call@Deprecated public static <T> ListenableFuture<T> scheduleWhileTaskResultNull(SubmitterScheduler scheduler, long scheduleDelayMillis, boolean firstRunAsync, java.util.concurrent.Callable<? extends T> task)
scheduleWhile(SubmitterScheduler, long, boolean, Callable, Predicate)
null
result. This can be a good way to implement retry logic where a result ultimately needs to be
communicated through a future.
The returned future will only complete with a result once the provided task returns a non-null result. Canceling the returned future will prevent future executions from being attempted. Canceling with an interrupt will transmit the interrupt to the running task if it is currently running.
The first execution will either be immediately executed in thread or submitted for immediate
execution on the provided scheduler (depending on firstRunAsync
parameter). Once
this execution completes, if the result is null
then the task will be rescheduled for
execution. If non-null then the result will be able to be retrieved from the returned
ListenableFuture
.
If you want to ensure this does not reschedule forever consider using
scheduleWhileTaskResultNull(SubmitterScheduler, long, boolean, Callable, long)
.
T
- The result object type returned by the task and provided by the futurescheduler
- Scheduler to schedule out task executionsscheduleDelayMillis
- Delay in milliseconds to schedule out future attemptsfirstRunAsync
- False
to run first try on invoking thread, true
to submit on schedulertask
- Task which will provide result, or null
to reschedule itself again@Deprecated public static <T> ListenableFuture<T> scheduleWhileTaskResultNull(SubmitterScheduler scheduler, long scheduleDelayMillis, boolean firstRunAsync, java.util.concurrent.Callable<? extends T> task, long timeoutMillis)
scheduleWhile(SubmitterScheduler, long, boolean, Callable, Predicate, long, boolean)
null
result. This can be a good way to implement retry logic where a result ultimately needs to be
communicated through a future.
The returned future will only complete with a result once the provided task returns a non-null
result, or until the provided timeout is reached. If the timeout is reached then the task
wont be rescheduled. Instead the future will be resolved with a null
result. Even if
only 1 millisecond before timeout, the entire rescheduleDelayMillis
will be provided
for the next attempt's scheduled delay. Canceling the returned future will prevent future
executions from being attempted. Canceling with an interrupt will transmit the interrupt to
the running task if it is currently running.
The first execution will either be immediately executed in thread or submitted for immediate
execution on the provided scheduler (depending on firstRunAsync
parameter). Once
this execution completes, if the result is null
then the task will be rescheduled for
execution. If non-null then the result will be able to be retrieved from the returned
ListenableFuture
.
T
- The result object type returned by the task and provided by the futurescheduler
- Scheduler to schedule out task executionsscheduleDelayMillis
- Delay in milliseconds to schedule out future attemptsfirstRunAsync
- False
to run first try on invoking thread, true
to submit on schedulertask
- Task which will provide result, or null
to reschedule itself againtimeoutMillis
- Timeout in milliseconds task wont be rescheduled and instead just finish with null
public static ListenableFuture<?> scheduleWhile(SubmitterScheduler scheduler, long scheduleDelayMillis, boolean firstRunAsync, java.lang.Runnable task, java.util.function.Supplier<java.lang.Boolean> loopTest)
false
. This can be a good way to
implement retry logic where there completion (but not result) needs to be communicated. If
a result is needed please see
scheduleWhile(SubmitterScheduler, long, boolean, Callable, Predicate)
.
The returned future will only provide a result once the looping of the task has completed. Canceling the returned future will prevent future executions from being attempted. Canceling with an interrupt will transmit the interrupt to the running task if it is currently running.
The first execution will either be immediately executed in thread or submitted for immediate
execution on the provided scheduler (depending on firstRunAsync
parameter). Once
this execution completes the result will be provided to the Supplier
to determine if
another schedule should occur to re-run the task.
If you want to ensure this does not reschedule forever consider using
scheduleWhile(SubmitterScheduler, long, boolean, Runnable, Supplier, long)
.
scheduler
- Scheduler to schedule out task executionsscheduleDelayMillis
- Delay after predicate indicating to loop again before re-executedfirstRunAsync
- False
to run first try on invoking thread, true
to submit on schedulertask
- Task to execute as long as test returns true
loopTest
- Test to see if scheduled loop should continueSupplier
returns false
public static ListenableFuture<?> scheduleWhile(SubmitterScheduler scheduler, long scheduleDelayMillis, boolean firstRunAsync, java.lang.Runnable task, java.util.function.Supplier<java.lang.Boolean> loopTest, long timeoutMillis)
The returned future will only provide a result once the looping of the task has completed, or
until the provided timeout is reached. If the timeout is reached then the task wont be
rescheduled. Even if only 1 millisecond before timeout, the entire
rescheduleDelayMillis
will be provided for the next attempt's scheduled delay. On a
timeout, if true
was provided for timeoutProvideLastValue
then the future
will be resolved with the last result provided. If false
was provided then the
future will complete in an error state, the cause of which being a TimeoutException
.
Canceling the returned future will prevent future executions from being attempted. Canceling
with an interrupt will transmit the interrupt to the running task if it is currently running.
The first execution will either be immediately executed in thread or submitted for immediate
execution on the provided scheduler (depending on firstRunAsync
parameter). Once
this execution completes the result will be provided to the Supplier
to determine if
another schedule should occur to re-run the task.
scheduler
- Scheduler to schedule out task executionsscheduleDelayMillis
- Delay after predicate indicating to loop again before re-executedfirstRunAsync
- False
to run first try on invoking thread, true
to submit on schedulertask
- Task to execute as long as test returns true
loopTest
- Test to see if scheduled loop should continuetimeoutMillis
- If greater than zero, wont reschedule and instead will just return the last resultSupplier
returns false
public static <T> ListenableFuture<T> scheduleWhile(SubmitterScheduler scheduler, long scheduleDelayMillis, boolean firstRunAsync, java.util.concurrent.Callable<? extends T> task, java.util.function.Predicate<? super T> loopTest)
The returned future will only provide a result once the looping of the task has completed. Canceling the returned future will prevent future executions from being attempted. Canceling with an interrupt will transmit the interrupt to the running task if it is currently running.
The first execution will either be immediately executed in thread or submitted for immediate
execution on the provided scheduler (depending on firstRunAsync
parameter). Once
this execution completes the result will be provided to the Predicate
to determine if
another schedule should occur to re-run the task.
If you want to ensure this does not reschedule forever consider using
scheduleWhile(SubmitterScheduler, long, boolean, Callable, Predicate, long, boolean)
.
T
- The result object type returned by the task and provided by the futurescheduler
- Scheduler to schedule out task executionsscheduleDelayMillis
- Delay after predicate indicating to loop again before re-executedfirstRunAsync
- False
to run first try on invoking thread, true
to submit on schedulertask
- Task which will provide result to compare in provided Predicate
loopTest
- Test for result to see if scheduled loop should continuePredicate
returns false
public static <T> ListenableFuture<T> scheduleWhile(SubmitterScheduler scheduler, long scheduleDelayMillis, boolean firstRunAsync, java.util.concurrent.Callable<? extends T> task, java.util.function.Predicate<? super T> loopTest, long timeoutMillis, boolean timeoutProvideLastValue)
The returned future will only provide a result once the looping of the task has completed, or
until the provided timeout is reached. If the timeout is reached then the task wont be
rescheduled. Even if only 1 millisecond before timeout, the entire
rescheduleDelayMillis
will be provided for the next attempt's scheduled delay. On a
timeout, if true
was provided for timeoutProvideLastValue
then the future
will be resolved with the last result provided. If false
was provided then the
future will complete in an error state, the cause of which being a TimeoutException
.
Canceling the returned future will prevent future executions from being attempted. Canceling
with an interrupt will transmit the interrupt to the running task if it is currently running.
The first execution will either be immediately executed in thread or submitted for immediate
execution on the provided scheduler (depending on firstRunAsync
parameter). Once
this execution completes the result will be provided to the Predicate
to determine if
another schedule should occur to re-run the task.
T
- The result object type returned by the task and provided by the futurescheduler
- Scheduler to schedule out task executionsscheduleDelayMillis
- Delay after predicate indicating to loop again before re-executedfirstRunAsync
- False
to run first try on invoking thread, true
to submit on schedulertask
- Task which will provide result to compare in provided Predicate
loopTest
- Test for result to see if scheduled loop should continuetimeoutMillis
- If greater than zero, wont reschedule and instead will just return the last resulttimeoutProvideLastValue
- On timeout false
will complete with a TimeoutException,
true
completes with the last resultPredicate
returns false
or timeout is reachedpublic static <T> ListenableFuture<T> scheduleWhile(SubmitterScheduler scheduler, long scheduleDelayMillis, ListenableFuture<? extends T> startingFuture, java.util.concurrent.Callable<? extends T> task, java.util.function.Predicate<? super T> loopTest)
The returned future will only provide a result once the looping of the task has completed. Canceling the returned future will prevent future executions from being attempted. Canceling with an interrupt will transmit the interrupt to the running task if it is currently running.
The first execution will happen as soon as the provided startingFuture
completes.
If you want to ensure this does not reschedule forever consider using
scheduleWhile(SubmitterScheduler, long, ListenableFuture, Callable, Predicate, long, boolean)
.
T
- The result object type returned by the task and provided by the futurescheduler
- Scheduler to schedule out task executionsscheduleDelayMillis
- Delay after predicate indicating to loop again before re-executedstartingFuture
- Future to use for first result to test for looptask
- Task which will provide result to compare in provided Predicate
loopTest
- Test for result to see if scheduled loop should continuePredicate
returns false
public static <T> ListenableFuture<T> scheduleWhile(SubmitterScheduler scheduler, long scheduleDelayMillis, ListenableFuture<? extends T> startingFuture, java.util.concurrent.Callable<? extends T> task, java.util.function.Predicate<? super T> loopTest, long timeoutMillis, boolean timeoutProvideLastValue)
The returned future will only provide a result once the looping of the task has completed, or
until the provided timeout is reached. If the timeout is reached then the task wont be
rescheduled. Even if only 1 millisecond before timeout, the entire
rescheduleDelayMillis
will be provided for the next attempt's scheduled delay. On a
timeout, if true
was provided for timeoutProvideLastValue
then the future
will be resolved with the last result provided. If false
was provided then the
future will complete in an error state, the cause of which being a TimeoutException
.
Canceling the returned future will prevent future executions from being attempted. Canceling
with an interrupt will transmit the interrupt to the running task if it is currently running.
The first execution will happen as soon as the provided startingFuture
completes.
T
- The result object type returned by the task and provided by the futurescheduler
- Scheduler to schedule out task executionsscheduleDelayMillis
- Delay after predicate indicating to loop again before re-executedstartingFuture
- Future to use for first result to test for looptask
- Task which will provide result to compare in provided Predicate
loopTest
- Test for result to see if scheduled loop should continuetimeoutMillis
- If greater than zero, wont reschedule and instead will just return the last resulttimeoutProvideLastValue
- On timeout false
will complete with a TimeoutException,
true
completes with the last resultPredicate
returns false
public static <T> ListenableFuture<T> executeWhile(java.util.concurrent.Callable<? extends ListenableFuture<? extends T>> asyncTask, java.util.function.Predicate<? super T> loopTest)
scheduleWhile(SubmitterScheduler, long, boolean, Callable, Predicate)
except that no executor is needed because the callable instead will return a future from the
provided async submission (which may be a scheduled task or otherwise).
In the end this is just another way to have a loop of async actions, checking results as they are provided but not resolving the returned future till the async action completes.
T
- The result object type returned by the task and provided by the futureasyncTask
- Callable to produce a ListenableFuture
for when a result is readyloopTest
- The test to check the ready result to see if we need to keep loopingPredicate
returns false
public static <T> ListenableFuture<T> executeWhile(java.util.concurrent.Callable<? extends ListenableFuture<? extends T>> asyncTask, java.util.function.Predicate<? super T> loopTest, long timeoutMillis, boolean timeoutProvideLastValue)
scheduleWhile(SubmitterScheduler, long, boolean, Callable, Predicate, long, boolean)
except that no executor is needed because the callable instead will return a future from the
provided async submission (which may be a scheduled task or otherwise).
In the end this is just another way to have a loop of async actions, checking results as they
are provided but not resolving the returned future till the async action completes. On a
timeout, if true
was provided for timeoutProvideLastValue
then the future
will be resolved with the last result provided. If false
was provided then the
future will complete in an error state, the cause of which being a TimeoutException
.
Canceling the returned future will prevent future executions from being attempted. Canceling
with an interrupt will transmit the interrupt to the running task if it is currently running.
T
- The result object type returned by the task and provided by the futureasyncTask
- Callable to produce a ListenableFuture
for when a result is readyloopTest
- The test to check the ready result to see if we need to keep loopingtimeoutMillis
- If greater than zero, wont reschedule and instead will just return the last resulttimeoutProvideLastValue
- On timeout false
will complete with a TimeoutException,
true
completes with the last resultPredicate
returns false
public static <T> ListenableFuture<T> executeWhile(ListenableFuture<? extends T> startingFuture, java.util.concurrent.Callable<? extends ListenableFuture<? extends T>> asyncTask, java.util.function.Predicate<? super T> loopTest)
scheduleWhile(SubmitterScheduler, long, ListenableFuture, Callable, Predicate)
except that no executor is needed because the callable instead will return a future from the
provided async submission (which may be a scheduled task or otherwise).
In the end this is just another way to have a loop of async actions, checking results as they are provided but not resolving the returned future till the async action completes.
T
- The result object type returned by the task and provided by the futurestartingFuture
- Future to use for first result to test for loopasyncTask
- Callable to produce a ListenableFuture
for when a result is readyloopTest
- The test to check the ready result to see if we need to keep loopingPredicate
returns false
public static <T> ListenableFuture<T> executeWhile(ListenableFuture<? extends T> startingFuture, java.util.concurrent.Callable<? extends ListenableFuture<? extends T>> asyncTask, java.util.function.Predicate<? super T> loopTest, long timeoutMillis, boolean timeoutProvideLastValue)
scheduleWhile(SubmitterScheduler, long, ListenableFuture, Callable, Predicate, long, boolean)
except that no executor is needed because the callable instead will return a future from the
provided async submission (which may be a scheduled task or otherwise).
In the end this is just another way to have a loop of async actions, checking results as they
are provided but not resolving the returned future till the async action completes. On a
timeout, if true
was provided for timeoutProvideLastValue
then the future
will be resolved with the last result provided. If false
was provided then the
future will complete in an error state, the cause of which being a TimeoutException
.
Canceling the returned future will prevent future executions from being attempted. Canceling
with an interrupt will transmit the interrupt to the running task if it is currently running.
T
- The result object type returned by the task and provided by the futurestartingFuture
- Future to use for first result to test for loopasyncTask
- Callable to produce a ListenableFuture
for when a result is readyloopTest
- The test to check the ready result to see if we need to keep loopingtimeoutMillis
- If greater than zero, wont reschedule and instead will just return the last resulttimeoutProvideLastValue
- On timeout false
will complete with a TimeoutException,
true
completes with the last resultPredicate
returns false