Package dev.failsafe.spi
Class FailsafeFuture<R>
- java.lang.Object
-
- java.util.concurrent.CompletableFuture<R>
-
- dev.failsafe.spi.FailsafeFuture<R>
-
- Type Parameters:
R- result type
- All Implemented Interfaces:
java.util.concurrent.CompletionStage<R>,java.util.concurrent.Future<R>
public class FailsafeFuture<R> extends java.util.concurrent.CompletableFuture<R>A CompletableFuture implementation that propagates cancellations and calls completion handlers.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.Integer,java.util.function.BiConsumer<java.lang.Boolean,ExecutionResult<R>>>cancelFunctionsprivate booleancancelledWithInterruptprivate java.util.function.BiConsumer<ExecutionResult<R>,ExecutionContext<R>>completionHandlerprivate ExecutionInternal<R>newestExecution
-
Constructor Summary
Constructors Constructor Description FailsafeFuture(java.util.function.BiConsumer<ExecutionResult<R>,ExecutionContext<R>> completionHandler)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancancel(boolean mayInterruptIfRunning)Cancels the future along with any dependencies.voidcancelDependencies(PolicyExecutor<R> cancellingPolicyExecutor, boolean mayInterrupt, ExecutionResult<R> cancelResult)Applies anycancel functionswith thecancelResultfor PolicyExecutors whose policyIndex is < the policyIndex of thecancellingPolicyExecutor.booleancomplete(R value)If not already completed, completes the future with thevalue, calling the complete and success handlers.booleancompleteExceptionally(java.lang.Throwable exception)If not already completed, completes the future with theexception, calling the complete and failure handlers.booleancompleteResult(ExecutionResult<R> result)Completes the execution with theresultand calls the completion handler.voidpropagateCancellation(java.util.concurrent.Future<R> future)Propogates any previous cancellation to thefuture, either by cancelling it immediately or setting a cancel function to cancel it later.voidsetCancelFn(int policyIndex, java.util.function.BiConsumer<java.lang.Boolean,ExecutionResult<R>> cancelFn)Sets acancelFnto be called when a PolicyExecutorcancels dependencieswith a policyIndex > the givenpolicyIndex, or when this future iscancelled.voidsetCancelFn(PolicyExecutor<R> policyExecutor, java.util.function.BiConsumer<java.lang.Boolean,ExecutionResult<R>> cancelFn)Sets acancelFnto be called when a PolicyExecutorcancels dependencieswith a policyIndex > the policyIndex of the givenpolicyExecutor, or when this future iscancelled.voidsetExecution(ExecutionInternal<R> execution)Sets theexecutionrepresenting the most recent attempt, which will be cancelled if this future is cancelled.-
Methods inherited from class java.util.concurrent.CompletableFuture
acceptEither, acceptEitherAsync, acceptEitherAsync, allOf, anyOf, applyToEither, applyToEitherAsync, applyToEitherAsync, completedFuture, exceptionally, get, get, getNow, getNumberOfDependents, handle, handleAsync, handleAsync, isCancelled, isCompletedExceptionally, isDone, join, obtrudeException, obtrudeValue, runAfterBoth, runAfterBothAsync, runAfterBothAsync, runAfterEither, runAfterEitherAsync, runAfterEitherAsync, runAsync, runAsync, supplyAsync, supplyAsync, thenAccept, thenAcceptAsync, thenAcceptAsync, thenAcceptBoth, thenAcceptBothAsync, thenAcceptBothAsync, thenApply, thenApplyAsync, thenApplyAsync, thenCombine, thenCombineAsync, thenCombineAsync, thenCompose, thenComposeAsync, thenComposeAsync, thenRun, thenRunAsync, thenRunAsync, toCompletableFuture, toString, whenComplete, whenCompleteAsync, whenCompleteAsync
-
-
-
-
Field Detail
-
completionHandler
private final java.util.function.BiConsumer<ExecutionResult<R>,ExecutionContext<R>> completionHandler
-
newestExecution
private ExecutionInternal<R> newestExecution
-
cancelFunctions
private java.util.Map<java.lang.Integer,java.util.function.BiConsumer<java.lang.Boolean,ExecutionResult<R>>> cancelFunctions
-
cancelledWithInterrupt
private boolean cancelledWithInterrupt
-
-
Constructor Detail
-
FailsafeFuture
public FailsafeFuture(java.util.function.BiConsumer<ExecutionResult<R>,ExecutionContext<R>> completionHandler)
-
-
Method Detail
-
complete
public boolean complete(R value)
If not already completed, completes the future with thevalue, calling the complete and success handlers.- Overrides:
completein classjava.util.concurrent.CompletableFuture<R>
-
completeExceptionally
public boolean completeExceptionally(java.lang.Throwable exception)
If not already completed, completes the future with theexception, calling the complete and failure handlers.- Overrides:
completeExceptionallyin classjava.util.concurrent.CompletableFuture<R>
-
cancel
public boolean cancel(boolean mayInterruptIfRunning)
Cancels the future along with any dependencies.
-
completeResult
public boolean completeResult(ExecutionResult<R> result)
Completes the execution with theresultand calls the completion handler.
-
cancelDependencies
public void cancelDependencies(PolicyExecutor<R> cancellingPolicyExecutor, boolean mayInterrupt, ExecutionResult<R> cancelResult)
Applies anycancel functionswith thecancelResultfor PolicyExecutors whose policyIndex is < the policyIndex of thecancellingPolicyExecutor.- Parameters:
cancellingPolicyExecutor- the PolicyExecutor that is requesting the cancellation of inner policy executors
-
setExecution
public void setExecution(ExecutionInternal<R> execution)
Sets theexecutionrepresenting the most recent attempt, which will be cancelled if this future is cancelled.
-
setCancelFn
public void setCancelFn(int policyIndex, java.util.function.BiConsumer<java.lang.Boolean,ExecutionResult<R>> cancelFn)Sets acancelFnto be called when a PolicyExecutorcancels dependencieswith a policyIndex > the givenpolicyIndex, or when this future iscancelled.
-
setCancelFn
public void setCancelFn(PolicyExecutor<R> policyExecutor, java.util.function.BiConsumer<java.lang.Boolean,ExecutionResult<R>> cancelFn)
Sets acancelFnto be called when a PolicyExecutorcancels dependencieswith a policyIndex > the policyIndex of the givenpolicyExecutor, or when this future iscancelled.
-
propagateCancellation
public void propagateCancellation(java.util.concurrent.Future<R> future)
Propogates any previous cancellation to thefuture, either by cancelling it immediately or setting a cancel function to cancel it later.
-
-