Package dev.failsafe

Class ExecutionImpl<R>

    • Field Detail

      • startTime

        private volatile java.time.Instant startTime
      • attempts

        private final java.util.concurrent.atomic.AtomicInteger attempts
      • executions

        private final java.util.concurrent.atomic.AtomicInteger executions
      • latest

        private final java.util.concurrent.atomic.AtomicReference<ExecutionInternal<R>> latest
      • attemptStartTime

        private volatile java.time.Instant attemptStartTime
      • cancelledIndex

        volatile int cancelledIndex
      • preExecuted

        private volatile boolean preExecuted
      • attemptRecorded

        volatile boolean attemptRecorded
      • completed

        volatile boolean completed
    • Constructor Detail

      • ExecutionImpl

        ExecutionImpl​(java.util.List<? extends Policy<R>> policies)
        Creates a new execution for the policies.
      • ExecutionImpl

        ExecutionImpl​(ExecutionImpl<R> execution)
        Create an execution for a new attempt.
      • ExecutionImpl

        ExecutionImpl​(ExecutionResult<R> previousResult)
        Used for testing purposes only
    • Method Detail

      • onCancel

        public void onCancel​(CheckedRunnable cancelCallback)
        Description copied from interface: ExecutionContext
        Sets the cancelCallback to be called if the execution is cancelled, such as by the resulting Call or CompletableFuture, or a Timeout. Any exception thrown by the cancelCallback is ignored.
        Specified by:
        onCancel in interface ExecutionContext<R>
      • isPreExecuted

        public boolean isPreExecuted()
        Description copied from interface: ExecutionInternal
        Returns whether the execution has been pre-executed, indicating the attempt has started.
        Specified by:
        isPreExecuted in interface ExecutionInternal<R>
      • recordAttempt

        public void recordAttempt()
        Description copied from interface: ExecutionInternal
        Records an execution attempt which may correspond with an execution result. Async executions will have results recorded separately.
        Specified by:
        recordAttempt in interface ExecutionInternal<R>
      • postExecute

        ExecutionResult<R> postExecute​(ExecutionResult<R> result)
        Externally called. Records an execution and performs post-execution handling for the result against all configured policy executors. Returns whether the result is complete for all policies.
        Throws:
        java.lang.IllegalStateException - if the execution is already complete
      • cancel

        public boolean cancel()
        Called indirectly by users.
        Specified by:
        cancel in interface ExecutionInternal<R>
        Returns:
        whether cancellation was successful or not. Returns false if the execution was already cancelled or completed.
      • isCancelled

        public boolean isCancelled()
        Description copied from interface: ExecutionContext
        Returns whether the execution has been cancelled. In this case the implementor should attempt to stop execution.
        Specified by:
        isCancelled in interface ExecutionContext<R>
      • getLock

        public java.lang.Object getLock()
        Description copied from interface: ExecutionInternal
        Returns a lock object that is common across all execution attempts. Useful for guarding against races when mutating an execution.
        Specified by:
        getLock in interface ExecutionInternal<R>
      • getAttemptCount

        public int getAttemptCount()
        Description copied from interface: ExecutionContext
        Gets the number of execution attempts so far, including attempts that are blocked before being executed, such as when a CircuitBreaker is open. Will return 0 when the first attempt is in progress or has yet to begin.
        Specified by:
        getAttemptCount in interface ExecutionContext<R>
      • getExecutionCount

        public int getExecutionCount()
        Description copied from interface: ExecutionContext
        Gets the number of completed executions so far. Executions that are blocked, such as when a CircuitBreaker is open, are not counted. Will return 0 when the first attempt is in progress or has yet to begin.
        Specified by:
        getExecutionCount in interface ExecutionContext<R>
      • getLastException

        public <T extends java.lang.Throwable> T getLastException()
        Description copied from interface: ExecutionContext
        Returns the last exception that was recorded else null.
        Specified by:
        getLastException in interface ExecutionContext<R>
      • getLastResult

        public R getLastResult​(R defaultValue)
        Description copied from interface: ExecutionContext
        Returns the last result that was recorded else the defaultValue.
        Specified by:
        getLastResult in interface ExecutionContext<R>
      • isFirstAttempt

        public boolean isFirstAttempt()
        Description copied from interface: ExecutionContext
        Returns true when an execution result has not yet been recorded, meaning this is the first execution attempt.
        Specified by:
        isFirstAttempt in interface ExecutionContext<R>
      • isRetry

        public boolean isRetry()
        Description copied from interface: ExecutionContext
        Returns true when an execution result has already been recorded, meaning the execution is being retried.
        Specified by:
        isRetry in interface ExecutionContext<R>
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object