Class SimpleRetryer


  • public class SimpleRetryer
    extends Object
    A simple class to retry an operation up to a maximum number of attempts. Uses the same set of initial values for maximum number of attempts, delay between attempts, etc. Consider using this rather than SimpleRetries directly.

    You can construct a SimpleRetryer using the builder obtained via SimpleRetryer.builder().

    Available configuration options for SimpleRetryer:
    Name Default Description
    environment a DefaultEnvironment instance mainly useful for testing, e.g. to supply a mock
    maxAttempts DEFAULT_MAX_ATTEMPTS the maximum number of attempts to make before giving up
    retryDelayTime DEFAULT_RETRY_DELAY_TIME the time value to wait between attempts
    retryDelayUnit DEFAULT_RETRY_DELAY_UNIT the time unit for retryDelayTime
    commonType DEFAULT_TYPE use this to specify a common type/description that retryer will get (used in log messages)
    logLevelForSubsequentAttempts Level.TRACE the log level at which retries will be logged (the first attempt is always logged at TRACE)
    Implementation Note:
    This is basically an instance wrapper around SimpleRetries to allow for specification of common configuration, and thus avoid calling methods with many parameters. It also facilitates easy mocking in tests as opposed to the static methods in SimpleRetries.
    • Field Detail

      • DEFAULT_MAX_ATTEMPTS

        public static final int DEFAULT_MAX_ATTEMPTS
        Default maximum attempts.
        See Also:
        Constant Field Values
      • DEFAULT_RETRY_DELAY_TIME

        public static final long DEFAULT_RETRY_DELAY_TIME
        Default retry delay time. This is a static value, i.e. there is no fancy exponential or linear backoff.
        See Also:
        Constant Field Values
      • DEFAULT_RETRY_DELAY_UNIT

        public static final TimeUnit DEFAULT_RETRY_DELAY_UNIT
        Default retry delay time unit.
      • DEFAULT_TYPE

        public static final String DEFAULT_TYPE
        Default value to include in attempt log messages.
        See Also:
        Constant Field Values
    • Constructor Detail

      • SimpleRetryer

        public SimpleRetryer()
    • Method Detail

      • tryGetObject

        public <T> Optional<T> tryGetObject​(Supplier<T> supplier)
        Try to get an object.
        Type Parameters:
        T - the type of object
        Parameters:
        supplier - on success return the object; return null or throw exception if attempt failed
        Returns:
        an Optional which either contains a value, or is empty if all attempts failed
      • tryGetObject

        public <T> Optional<T> tryGetObject​(Class<T> type,
                                            Supplier<T> supplier)
        Try to get an object.
        Type Parameters:
        T - the type of object
        Parameters:
        type - the type of object to return, used only in logging messages
        supplier - on success return the object; return null or throw exception if attempt failed
        Returns:
        an Optional which either contains a value, or is empty if all attempts failed
      • tryGetObject

        public <T> Optional<T> tryGetObject​(String type,
                                            Supplier<T> supplier)
        Try to get an object.
        Type Parameters:
        T - the type of object
        Parameters:
        type - the type of object to return, used only in logging messages
        supplier - on success return the object; return null or throw exception if attempt failed
        Returns:
        an Optional which either contains a value, or is empty if all attempts failed
      • tryGetObjectCollectingErrors

        public <T> RetryResult<T> tryGetObjectCollectingErrors​(Supplier<T> supplier)
        Try to get an object.
        Type Parameters:
        T - the type of object
        Parameters:
        supplier - on success return the object; return null or throw exception if attempt failed
        Returns:
        a RetryResult
      • tryGetObjectCollectingErrors

        public <T> RetryResult<T> tryGetObjectCollectingErrors​(Class<T> type,
                                                               Supplier<T> supplier)
        Try to get an object.
        Type Parameters:
        T - the type of object
        Parameters:
        type - the type of object to return, used only in logging messages
        supplier - on success return the object; return null or throw exception if attempt failed
        Returns:
        a RetryResult
      • tryGetObjectCollectingErrors

        public <T> RetryResult<T> tryGetObjectCollectingErrors​(String type,
                                                               Supplier<T> supplier)
        Try to get an object.
        Type Parameters:
        T - the type of object
        Parameters:
        type - the type of object to return, used only in logging messages
        supplier - on success return the object; return null or throw exception if attempt failed
        Returns:
        a RetryResult