Class FutureCompletionHandler<T>

  • All Implemented Interfaces:
    Runnable
    Direct Known Subclasses:
    FutureCompletionLogger

    public class FutureCompletionHandler<T>
    extends Object
    implements Runnable
    To be set as a listener on ListenableFutures. This is useful in order to capture their termination results(either result object on success or exception in case of failure), and react accordingly. Specially useful for tracking success/failure on futures which no thread block on (fire and forget scenarios).
    Author:
    Eyal Schneider
    • Constructor Detail

      • FutureCompletionHandler

        protected FutureCompletionHandler​(Consumer<? super T> successListener,
                                          Consumer<Throwable> errorListener,
                                          Future<? extends T> future)
        Protected constructor
        Parameters:
        successListener - The listener to be triggered when the future's data is produced successfully
        errorListener - The listener to be triggered when the future's computation terminated with an error
        future - The future object to test for errors. Should be the same future this object is set as listener for.
    • Method Detail

      • run

        public void run()
        Specified by:
        run in interface Runnable
      • listenTo

        public static <T> void listenTo​(com.google.common.util.concurrent.ListenableFuture<T> future,
                                        Consumer<? super T> successListener,
                                        Consumer<Throwable> errorListener)
        Adds a completion listener to a given ListenableFuture. The listener runs on the producer thread.
        Parameters:
        future - The future to listen to
        successListener - The listener to be triggered when the future's data is produced successfully
        errorListener - The listener to be triggered when the future's computation terminated with an error
      • listenTo

        public static <T> void listenTo​(com.google.common.util.concurrent.ListenableFuture<T> future,
                                        Consumer<? super T> successListener,
                                        Consumer<Throwable> errorListener,
                                        Executor ex)
        Adds a completion listener to a given ListenableFuture
        Parameters:
        future - The future to listen to
        successListener - The listener to be triggered when the future's data is produced successfully
        errorListener - The listener to be triggered when the future's computation terminated with an error
        ex - The executor to run the callbacks on
      • listenToSuccess

        public static <T> void listenToSuccess​(com.google.common.util.concurrent.ListenableFuture<T> future,
                                               Consumer<? super T> successListener)
        Adds a successful completion listener to a given ListenableFuture. The listener runs on the producer thread.
        Parameters:
        future - The future to listen to
        successListener - The listener to be triggered when the future's data is produced successfully
      • listenToSuccess

        public static <T> void listenToSuccess​(com.google.common.util.concurrent.ListenableFuture<T> future,
                                               Consumer<? super T> successListener,
                                               Executor ex)
        Adds a successful completion listener to a given ListenableFuture
        Parameters:
        future - The future to listen to
        successListener - The listener to be triggered when the future's data is produced successfully
        ex - The executor to run the success callback on
      • listenToError

        public static void listenToError​(com.google.common.util.concurrent.ListenableFuture<?> future,
                                         Consumer<Throwable> errorListener)
        Adds an error listener to a given ListenableFuture. The listener runs on the producer thread.
        Parameters:
        future - The future to listen to
        errorListener - The listener to be triggered when the future's computation terminated with an error
      • listenToError

        public static void listenToError​(com.google.common.util.concurrent.ListenableFuture<?> future,
                                         Consumer<Throwable> errorListener,
                                         Executor ex)
        Adds an error listener to a given ListenableFuture
        Parameters:
        future - The future to listen to
        errorListener - The listener to be triggered when the future's computation terminated with an error
        ex - The executor to run the error callback on