Package org.pipecraft.infra.concurrent
Class FutureCompletionHandler<T>
- java.lang.Object
-
- org.pipecraft.infra.concurrent.FutureCompletionHandler<T>
-
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
FutureCompletionLogger
public class FutureCompletionHandler<T> extends Object implements Runnable
To be set as a listener onListenableFutures. 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
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> voidlistenTo(com.google.common.util.concurrent.ListenableFuture<T> future, Consumer<? super T> successListener, Consumer<Throwable> errorListener)Adds a completion listener to a givenListenableFuture.static <T> voidlistenTo(com.google.common.util.concurrent.ListenableFuture<T> future, Consumer<? super T> successListener, Consumer<Throwable> errorListener, Executor ex)Adds a completion listener to a givenListenableFuturestatic voidlistenToError(com.google.common.util.concurrent.ListenableFuture<?> future, Consumer<Throwable> errorListener)Adds an error listener to a givenListenableFuture.static voidlistenToError(com.google.common.util.concurrent.ListenableFuture<?> future, Consumer<Throwable> errorListener, Executor ex)Adds an error listener to a givenListenableFuturestatic <T> voidlistenToSuccess(com.google.common.util.concurrent.ListenableFuture<T> future, Consumer<? super T> successListener)Adds a successful completion listener to a givenListenableFuture.static <T> voidlistenToSuccess(com.google.common.util.concurrent.ListenableFuture<T> future, Consumer<? super T> successListener, Executor ex)Adds a successful completion listener to a givenListenableFuturevoidrun()
-
-
-
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 successfullyerrorListener- The listener to be triggered when the future's computation terminated with an errorfuture- The future object to test for errors. Should be the same future this object is set as listener for.
-
-
Method Detail
-
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 givenListenableFuture. The listener runs on the producer thread.- Parameters:
future- The future to listen tosuccessListener- The listener to be triggered when the future's data is produced successfullyerrorListener- 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 givenListenableFuture- Parameters:
future- The future to listen tosuccessListener- The listener to be triggered when the future's data is produced successfullyerrorListener- The listener to be triggered when the future's computation terminated with an errorex- 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 givenListenableFuture. The listener runs on the producer thread.- Parameters:
future- The future to listen tosuccessListener- 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 givenListenableFuture- Parameters:
future- The future to listen tosuccessListener- The listener to be triggered when the future's data is produced successfullyex- 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 givenListenableFuture. The listener runs on the producer thread.- Parameters:
future- The future to listen toerrorListener- 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 givenListenableFuture- Parameters:
future- The future to listen toerrorListener- The listener to be triggered when the future's computation terminated with an errorex- The executor to run the error callback on
-
-