Class FutureUtils

    • Constructor Detail

      • FutureUtils

        public FutureUtils()
    • Method Detail

      • blockTillAllComplete

        public static void blockTillAllComplete​(java.util.concurrent.Future<?>... futures)
                                         throws java.lang.InterruptedException
        This call blocks till all futures in the list have completed. If the future completed with an error, the 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).

        Parameters:
        futures - Futures to block till they complete
        Throws:
        java.lang.InterruptedException - Thrown if thread is interrupted while waiting on future
      • blockTillAllComplete

        public static void blockTillAllComplete​(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures)
                                         throws java.lang.InterruptedException
        This call blocks till all futures in the list have completed. If the future completed with an error, the 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).

        Parameters:
        futures - Structure of futures to iterate over
        Throws:
        java.lang.InterruptedException - Thrown if thread is interrupted while waiting on future
      • blockTillAllComplete

        public static void blockTillAllComplete​(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures,
                                                long timeoutInMillis)
                                         throws java.lang.InterruptedException,
                                                java.util.concurrent.TimeoutException
        This call blocks till all futures in the list have completed. If the future completed with an error, the 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).
        Parameters:
        futures - Structure of futures to iterate over
        timeoutInMillis - timeout to wait for futures to complete in milliseconds
        Throws:
        java.lang.InterruptedException - Thrown if thread is interrupted while waiting on future
        java.util.concurrent.TimeoutException - Thrown if the timeout elapsed while waiting on futures to complete
        Since:
        4.0.0
      • blockTillAllCompleteOrFirstError

        public static void blockTillAllCompleteOrFirstError​(java.util.concurrent.Future<?>... futures)
                                                     throws java.lang.InterruptedException,
                                                            java.util.concurrent.ExecutionException
        This call blocks till all futures in the list have completed. If the future completed with an error an 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).

        Parameters:
        futures - Futures to iterate over
        Throws:
        java.lang.InterruptedException - Thrown if thread is interrupted while waiting on future
        java.util.concurrent.ExecutionException - Thrown if future throws exception on .get() call
      • blockTillAllCompleteOrFirstError

        public static void blockTillAllCompleteOrFirstError​(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures)
                                                     throws java.lang.InterruptedException,
                                                            java.util.concurrent.ExecutionException
        This call blocks till all futures in the list have completed. If the future completed with an error an 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).

        Parameters:
        futures - Structure of futures to iterate over
        Throws:
        java.lang.InterruptedException - Thrown if thread is interrupted while waiting on future
        java.util.concurrent.ExecutionException - Thrown if future throws exception on .get() call
      • blockTillAllCompleteOrFirstError

        public 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
        This call blocks till all futures in the list have completed. If the future completed with an error an 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).
        Parameters:
        futures - Structure of futures to iterate over
        timeoutInMillis - timeout to wait for futures to complete in milliseconds
        Throws:
        java.lang.InterruptedException - Thrown if thread is interrupted while waiting on future
        java.util.concurrent.TimeoutException - Thrown if the timeout elapsed while waiting on futures to complete
        java.util.concurrent.ExecutionException - Thrown if future throws exception on .get() call
        Since:
        4.0.0
      • countFuturesWithResult

        public static <T> int countFuturesWithResult​(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures,
                                                     T comparisonResult)
                                              throws java.lang.InterruptedException
        Counts how many futures provided completed with a result that matches the one provided here. This can be most useful if your looking to know if an error occurred that was not an 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).

        Type Parameters:
        T - type of result futures provide to compare against
        Parameters:
        futures - Structure of futures to iterate over
        comparisonResult - Object to compare future results against to look for match
        Returns:
        Number of futures which match the result using a Object.equals(Object) comparison
        Throws:
        java.lang.InterruptedException - Thrown if thread is interrupted while waiting on future's result
        Since:
        4.0.0
      • countFuturesWithResult

        public 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
        Counts how many futures provided completed with a result that matches the one provided here. This can be most useful if your looking to know if an error occurred that was not an 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).

        Type Parameters:
        T - type of result futures provide to compare against
        Parameters:
        futures - Structure of futures to iterate over
        comparisonResult - Object to compare future results against to look for match
        timeoutInMillis - timeout to wait for futures to complete in milliseconds
        Returns:
        Number of futures which match the result using a Object.equals(Object) comparison
        Throws:
        java.lang.InterruptedException - Thrown if thread is interrupted while waiting on future's result
        java.util.concurrent.TimeoutException - Thrown if the timeout elapsed while waiting on futures to complete
        Since:
        4.0.0
      • invokeAfterAllComplete

        public static void invokeAfterAllComplete​(java.util.Collection<? extends ListenableFuture<?>> futures,
                                                  java.lang.Runnable listener)
        A potentially more performant option than makeCompleteFuture(List) when only a listener invocation is desired after all the futures complete. This is effective an async implementation of blockTillAllComplete(Iterable). If the listener needs to be invoked on another thread than one of the provided futures please use invokeAfterAllComplete(Collection, Runnable, Executor). Please see ListenableFuture.listener(Runnable) for more information on execution without an Executor.

        It is critical that the collection is NOT modified while this is invoked. A change in the futures contained in the collection will lead to unreliable behavior with the exectuion of the listener.

        Parameters:
        futures - Futures that must complete before listener is invoked
        listener - Invoked once all the provided futures have completed
      • invokeAfterAllComplete

        public static void invokeAfterAllComplete​(java.util.Collection<? extends ListenableFuture<?>> futures,
                                                  java.lang.Runnable listener,
                                                  java.util.concurrent.Executor executor)
        A potentially more performant option than makeCompleteFuture(List) when only a listener invocation is desired after all the futures complete. This is effective an async implementation of blockTillAllComplete(Iterable).

        It is critical that the collection is NOT modified while this is invoked. A change in the futures contained in the collection will lead to unreliable behavior with the exectuion of the listener.

        Parameters:
        futures - Futures that must complete before listener is invoked
        listener - Invoked once all the provided futures have completed
        executor - Executor (or null) to invoke listener on, see ListenableFuture.listener(Runnable, Executor)
      • makeFirstResultFuture

        public static <T> ListenableFuture<T> makeFirstResultFuture​(java.util.Collection<? extends ListenableFuture<? extends T>> c,
                                                                    boolean ignoreErrors)
        Converts a collection of ListenableFuture's into a single ListenableFuture where the result will be the first result provided from the collection.

        If ignoreErrors is false the returned future will complete as soon as the first future completes, if it completes in error then the error would be returned. If ignoreErrors is true then the returned future will complete once a result is provided, or once all futures have completed in error. If all futures did complete in error then the last error state will be specified to the resulting ListenableFuture. This minor bookkeeping to ignore errors does incur a slight overhead.

        Type Parameters:
        T - type of result provided in the returned future
        Parameters:
        c - Collection of futures to monitor for result
        ignoreErrors - false to communicate the first completed future state, even if in error
        Returns:
        A future which will be provided the first result from any in the provided Collection
        Since:
        6.6
      • makeFirstResultFuture

        public static <T> ListenableFuture<T> makeFirstResultFuture​(java.util.Collection<? extends ListenableFuture<? extends T>> c,
                                                                    boolean ignoreErrors,
                                                                    boolean interruptOnCancel)
        Converts a collection of ListenableFuture's into a single ListenableFuture where the result will be the first result provided from the collection.

        If ignoreErrors is false the returned future will complete as soon as the first future completes, if it completes in error then the error would be returned. If ignoreErrors is true then the returned future will complete once a result is provided, or once all futures have completed in error. If all futures did complete in error then the last error state will be specified to the resulting ListenableFuture. This minor bookkeeping to ignore errors does incur a slight overhead.

        It is expected that the first result is the only result desired, once it is found this will attempt to cancel all remaining futures. If you may want other results which were in progress, then specifying interruptOnCancel as false will mean that any futures which started can complete. You can then inspect the collection for done futures which might have a result. If there is no concern for other results, then you likely will want to interrupt started futures.

        Type Parameters:
        T - type of result provided in the returned future
        Parameters:
        c - Collection of futures to monitor for result
        ignoreErrors - false to communicate the first completed future state, even if in error
        interruptOnCancel - true to send a interrupt on any running futures after we have a result
        Returns:
        A future which will be provided the first result from any in the provided Collection
        Since:
        5.38
      • makeCompleteFuture

        public 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. 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.

        Parameters:
        futures - Collection of futures that must finish before returned future is satisfied
        Returns:
        ListenableFuture which will be done once all futures provided are done
        Since:
        5.3
      • makeCompleteFuture

        public static ListenableFuture<?> makeCompleteFuture​(java.util.Collection<? extends ListenableFuture<?>> futures)
        An alternative to 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.

        Parameters:
        futures - Collection of futures that must finish before returned future is satisfied
        Returns:
        ListenableFuture which will be done once all futures provided are done
        Since:
        5.3
      • makeCompleteFuture

        public static ListenableFuture<?> makeCompleteFuture​(ListenableFuture<?>... futures)
        An alternative to 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.

        Parameters:
        futures - Futures that must finish before returned future is satisfied
        Returns:
        ListenableFuture which will be done once all futures provided are done
        Since:
        1.2.0
      • makeCompleteFuture

        public 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. 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.

        Parameters:
        futures - Collection of futures that must finish before returned future is satisfied
        Returns:
        ListenableFuture which will be done once all futures provided are done
        Since:
        1.2.0
      • makeCompleteFuture

        public 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. 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.

        Type Parameters:
        T - type of result returned from the future
        Parameters:
        futures - Collection of futures that must finish before returned future is satisfied
        result - Result to provide returned future once all futures complete
        Returns:
        ListenableFuture which will be done once all futures provided are done
        Since:
        3.3.0
      • makeFailurePropagatingCompleteFuture

        public static ListenableFuture<?> makeFailurePropagatingCompleteFuture​(ListenableFuture<?>... futures)
        Similar to 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.

        Parameters:
        futures - Collection of futures that must finish before returned future is satisfied
        Returns:
        ListenableFuture which will be done once all futures provided are done
        Since:
        5.0
      • makeFailurePropagatingCompleteFuture

        public 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. 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.

        Parameters:
        futures - Collection of futures that must finish before returned future is satisfied
        Returns:
        ListenableFuture which will be done once all futures provided are done
        Since:
        5.0
      • makeFailurePropagatingCompleteFuture

        public 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. 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.

        Type Parameters:
        T - type of result returned from the future
        Parameters:
        futures - Collection of futures that must finish before returned future is satisfied
        result - Result to provide returned future once all futures complete
        Returns:
        ListenableFuture which will be done once all futures provided are done
        Since:
        5.0
      • makeCompleteListFuture

        public 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.

        This future provides a list of the completed futures as the result. The order of the result list will match the order returned by the provided Iterable.

        If Future.cancel(boolean) is invoked on the returned future, all provided futures will attempt to be canceled in the same way.

        Type Parameters:
        T - The result object type returned from the futures
        Parameters:
        futures - Structure of futures to iterate over
        Returns:
        ListenableFuture which will be done once all futures provided are done
        Since:
        1.2.0
      • makeSuccessListFuture

        public 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.

        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 order is needed please see makeCompleteListFuture(Iterable) and check for results.

        If Future.cancel(boolean) is invoked on the returned future, all provided futures will attempt to be canceled in the same way.

        Type Parameters:
        T - The result object type returned from the futures
        Parameters:
        futures - Structure of futures to iterate over
        Returns:
        ListenableFuture which will be done once all futures provided are done
        Since:
        1.2.0
      • makeFailureListFuture

        public 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.

        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 order is needed please see makeCompleteListFuture(Iterable) and check for results.

        If Future.cancel(boolean) is invoked on the returned future, all provided futures will attempt to be canceled in the same way.

        Type Parameters:
        T - The result object type returned from the futures
        Parameters:
        futures - Structure of futures to iterate over
        Returns:
        ListenableFuture which will be done once all futures provided are done
        Since:
        1.2.0
      • makeResultListFuture

        public 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. Thus preventing the need to iterate over all the futures and manually extract the results. This call does NOT block, instead it will return a future which will not complete until all the provided futures complete.

        The order of the result list will match the order returned by the provided Iterable.

        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.

        Type Parameters:
        T - The result object type returned from the futures
        Parameters:
        futures - Structure of futures to iterate over and extract results from
        ignoreFailedFutures - true to ignore any failed or canceled futures
        Returns:
        A ListenableFuture which will provide a list of the results from the provided futures
        Since:
        4.0.0
      • cancelIncompleteFutures

        public static void cancelIncompleteFutures​(java.lang.Iterable<? extends java.util.concurrent.Future<?>> futures,
                                                   boolean interruptThread)
        Invoked 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.
        Parameters:
        futures - Collection of futures to iterate through and cancel
        interruptThread - Valued passed in to interrupt thread when calling Future.cancel(boolean)
      • cancelIncompleteFuturesIfAnyFail

        public 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.

        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.

        Parameters:
        copy - true to copy provided futures to avoid
        futures - Futures to be monitored and canceled on error
        interruptThread - Valued passed in to interrupt thread when calling Future.cancel(boolean)
        Since:
        4.7.2
      • immediateResultFuture

        public static <T> ListenableFuture<T> immediateResultFuture​(T result)
        Constructs a 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. This function may additionally try to optimize the references to other common cases (like Boolean results) when performance permits it. Those de-duplications may change based off benchmarking results, so be careful about depending on them. If no de-duplication is desired (ie the Future is used as a key in a Map), then manually construct a new ImmediateResultListenableFuture.

        Type Parameters:
        T - The result object type returned by the returned future
        Parameters:
        result - result to be provided in .get() call
        Returns:
        Already satisfied future
        Since:
        1.2.0
      • immediateFailureFuture

        public static <T> ListenableFuture<T> immediateFailureFuture​(java.lang.Throwable failure)
        Constructs a 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.
        Type Parameters:
        T - The result object type returned by the returned future
        Parameters:
        failure - to provide as cause for ExecutionException thrown from .get() call
        Returns:
        Already satisfied future
        Since:
        1.2.0
      • scheduleWhile

        public 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. 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).

        Parameters:
        scheduler - Scheduler to schedule out task executions
        scheduleDelayMillis - Delay after predicate indicating to loop again before re-executed
        firstRunAsync - False to run first try on invoking thread, true to submit on scheduler
        task - Task to execute as long as test returns true
        loopTest - Test to see if scheduled loop should continue
        Returns:
        Future that will resolve once returned Supplier returns false
        Since:
        5.0
      • scheduleWhile

        public 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. 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 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.

        Parameters:
        scheduler - Scheduler to schedule out task executions
        scheduleDelayMillis - Delay after predicate indicating to loop again before re-executed
        firstRunAsync - False to run first try on invoking thread, true to submit on scheduler
        task - Task to execute as long as test returns true
        loopTest - Test to see if scheduled loop should continue
        timeoutMillis - If greater than zero, wont reschedule and instead will just return the last result
        Returns:
        Future that will resolve once returned Supplier returns false
        Since:
        5.0
      • scheduleWhile

        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)
        Executes a task, checking the result from the task to see if it needs to reschedule the task again. 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 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).

        Type Parameters:
        T - The result object type returned by the task and provided by the future
        Parameters:
        scheduler - Scheduler to schedule out task executions
        scheduleDelayMillis - Delay after predicate indicating to loop again before re-executed
        firstRunAsync - False to run first try on invoking thread, true to submit on scheduler
        task - Task which will provide result to compare in provided Predicate
        loopTest - Test for result to see if scheduled loop should continue
        Returns:
        Future that will resolve once returned Predicate returns false
        Since:
        5.0
      • scheduleWhile

        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)
        Executes a task, checking the result from the task to see if it needs to reschedule the task again. 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 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.

        Type Parameters:
        T - The result object type returned by the task and provided by the future
        Parameters:
        scheduler - Scheduler to schedule out task executions
        scheduleDelayMillis - Delay after predicate indicating to loop again before re-executed
        firstRunAsync - False to run first try on invoking thread, true to submit on scheduler
        task - Task which will provide result to compare in provided Predicate
        loopTest - Test for result to see if scheduled loop should continue
        timeoutMillis - If greater than zero, wont reschedule and instead will just return the last result
        timeoutProvideLastValue - On timeout false will complete with a TimeoutException, true completes with the last result
        Returns:
        Future that will resolve once returned Predicate returns false or timeout is reached
        Since:
        5.0
      • scheduleWhile

        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)
        Executes a task, checking the result from the task to see if it needs to reschedule the task again. 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 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).

        Type Parameters:
        T - The result object type returned by the task and provided by the future
        Parameters:
        scheduler - Scheduler to schedule out task executions
        scheduleDelayMillis - Delay after predicate indicating to loop again before re-executed
        startingFuture - Future to use for first result to test for loop
        task - Task which will provide result to compare in provided Predicate
        loopTest - Test for result to see if scheduled loop should continue
        Returns:
        Future that will resolve once returned Predicate returns false
        Since:
        5.0
      • scheduleWhile

        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)
        Executes a task, checking the result from the task to see if it needs to reschedule the task again. 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 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.

        Type Parameters:
        T - The result object type returned by the task and provided by the future
        Parameters:
        scheduler - Scheduler to schedule out task executions
        scheduleDelayMillis - Delay after predicate indicating to loop again before re-executed
        startingFuture - Future to use for first result to test for loop
        task - Task which will provide result to compare in provided Predicate
        loopTest - Test for result to see if scheduled loop should continue
        timeoutMillis - If greater than zero, wont reschedule and instead will just return the last result
        timeoutProvideLastValue - On timeout false will complete with a TimeoutException, true completes with the last result
        Returns:
        Future that will resolve once returned Predicate returns false
        Since:
        5.0
      • executeWhile

        public 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).

        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.

        Type Parameters:
        T - The result object type returned by the task and provided by the future
        Parameters:
        asyncTask - Callable to produce a ListenableFuture for when a result is ready
        loopTest - The test to check the ready result to see if we need to keep looping
        Returns:
        Future that will resolve once returned Predicate returns false
      • executeWhile

        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)
        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).

        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.

        Type Parameters:
        T - The result object type returned by the task and provided by the future
        Parameters:
        asyncTask - Callable to produce a ListenableFuture for when a result is ready
        loopTest - The test to check the ready result to see if we need to keep looping
        timeoutMillis - If greater than zero, wont reschedule and instead will just return the last result
        timeoutProvideLastValue - On timeout false will complete with a TimeoutException, true completes with the last result
        Returns:
        Future that will resolve once returned Predicate returns false
      • executeWhile

        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)
        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).

        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.

        Type Parameters:
        T - The result object type returned by the task and provided by the future
        Parameters:
        startingFuture - Future to use for first result to test for loop
        asyncTask - Callable to produce a ListenableFuture for when a result is ready
        loopTest - The test to check the ready result to see if we need to keep looping
        Returns:
        Future that will resolve once returned Predicate returns false
      • executeWhile

        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 lastValueOnTimeout)
        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).

        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.

        Type Parameters:
        T - The result object type returned by the task and provided by the future
        Parameters:
        startingFuture - Future to use for first result to test for loop
        asyncTask - Callable to produce a ListenableFuture for when a result is ready
        loopTest - The test to check the ready result to see if we need to keep looping
        timeoutMillis - If greater than zero, wont reschedule and instead will just return the last result
        lastValueOnTimeout - On timeout false will complete with a TimeoutException, true completes with the last result
        Returns:
        Future that will resolve once returned Predicate returns false