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>>>
cancelFunctions
private boolean
cancelledWithInterrupt
private java.util.function.BiConsumer<ExecutionResult<R>,ExecutionContext<R>>
completionHandler
private 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 boolean
cancel(boolean mayInterruptIfRunning)
Cancels the future along with any dependencies.void
cancelDependencies(PolicyExecutor<R> cancellingPolicyExecutor, boolean mayInterrupt, ExecutionResult<R> cancelResult)
Applies anycancel functions
with thecancelResult
for PolicyExecutors whose policyIndex is < the policyIndex of thecancellingPolicyExecutor
.boolean
complete(R value)
If not already completed, completes the future with thevalue
, calling the complete and success handlers.boolean
completeExceptionally(java.lang.Throwable exception)
If not already completed, completes the future with theexception
, calling the complete and failure handlers.boolean
completeResult(ExecutionResult<R> result)
Completes the execution with theresult
and calls the completion handler.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.void
setCancelFn(int policyIndex, java.util.function.BiConsumer<java.lang.Boolean,ExecutionResult<R>> cancelFn)
Sets acancelFn
to be called when a PolicyExecutorcancels dependencies
with a policyIndex > the givenpolicyIndex
, or when this future iscancelled
.void
setCancelFn(PolicyExecutor<R> policyExecutor, java.util.function.BiConsumer<java.lang.Boolean,ExecutionResult<R>> cancelFn)
Sets acancelFn
to be called when a PolicyExecutorcancels dependencies
with a policyIndex > the policyIndex of the givenpolicyExecutor
, or when this future iscancelled
.void
setExecution(ExecutionInternal<R> execution)
Sets theexecution
representing 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:
complete
in 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:
completeExceptionally
in 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 theresult
and calls the completion handler.
-
cancelDependencies
public void cancelDependencies(PolicyExecutor<R> cancellingPolicyExecutor, boolean mayInterrupt, ExecutionResult<R> cancelResult)
Applies anycancel functions
with thecancelResult
for 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 theexecution
representing 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 acancelFn
to be called when a PolicyExecutorcancels dependencies
with 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 acancelFn
to be called when a PolicyExecutorcancels dependencies
with 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.
-
-