Class SimpleRetries


  • public class SimpleRetries
    extends Object
    Static utilities for retrying an operation. The Supplier passed to each method must indicate success or failure. Success is indicated by returning a non-null value. Failure is indicated by returning null or by throwing an exception. Each time a failure occurs, the code sleeps for the specified delay time, and then another attempt will be made unless the maximum number of attempts has been reached.

    While you can use this directly, consider using SimpleRetryer, which is more flexible because (1) you can easily mock it in tests, and (2) it accepts common configuration options and makes the method calls simpler because there are many fewer arguments.

    • Constructor Detail

      • SimpleRetries

        public SimpleRetries()
    • Method Detail

      • tryGetObject

        public static <T> Optional<T> tryGetObject​(int maxAttempts,
                                                   long retryDelay,
                                                   TimeUnit retryDelayUnit,
                                                   Supplier<T> supplier)
        Try to get an object, making up to maxAttempts attempts. Logs first attempt and retries at TRACE level.
        Type Parameters:
        T - the type of object
        Parameters:
        maxAttempts - the maximum number of attempts to make before giving up
        retryDelay - constant delay time between attempts
        retryDelayUnit - delay time unit between attempts
        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 static <T> Optional<T> tryGetObject​(int maxAttempts,
                                                   long retryDelay,
                                                   TimeUnit retryDelayUnit,
                                                   KiwiEnvironment environment,
                                                   Supplier<T> supplier)
        Try to get an object, making up to maxAttempts attempts. Logs first attempt and retries at TRACE level using "object" as the description.
        Type Parameters:
        T - the type of object
        Parameters:
        maxAttempts - the maximum number of attempts to make before giving up
        retryDelay - constant delay time between attempts
        retryDelayUnit - delay time unit between attempts
        environment - the KiwiEnvironment to use when sleeping between attempts
        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 static <T> Optional<T> tryGetObject​(int maxAttempts,
                                                   long retryDelay,
                                                   TimeUnit retryDelayUnit,
                                                   Class<T> type,
                                                   Supplier<T> supplier)
        Try to get an object, making up to maxAttempts attempts. Logs first attempt and retries at TRACE level using the given type as the description.
        Type Parameters:
        T - the type of object
        Parameters:
        maxAttempts - the maximum number of attempts to make before giving up
        retryDelay - constant delay time between attempts
        retryDelayUnit - delay time unit between attempts
        type - the type of object we are attempting to return, used when logging attempts
        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 static <T> Optional<T> tryGetObject​(int maxAttempts,
                                                   long retryDelay,
                                                   TimeUnit retryDelayUnit,
                                                   KiwiEnvironment environment,
                                                   Class<T> type,
                                                   Supplier<T> supplier)
        Try to get an object, making up to maxAttempts attempts. Logs first attempt and retries at TRACE level, using the given type as the description.
        Type Parameters:
        T - the type of object
        Parameters:
        maxAttempts - the maximum number of attempts to make before giving up
        retryDelay - constant delay time between attempts
        retryDelayUnit - delay time unit between attempts
        environment - the KiwiEnvironment to use when sleeping between attempts
        type - the type of object we are attempting to return, used when logging attempts
        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 static <T> Optional<T> tryGetObject​(int maxAttempts,
                                                   long retryDelay,
                                                   TimeUnit retryDelayUnit,
                                                   KiwiEnvironment environment,
                                                   String type,
                                                   org.slf4j.event.Level level,
                                                   Supplier<T> supplier)
        Try to get an object, making up to maxAttempts attempts. Logs first attempt at TRACE level, and logs retries at the given level, always using type as the description.
        Type Parameters:
        T - the type of object
        Parameters:
        maxAttempts - the maximum number of attempts to make before giving up
        retryDelay - constant delay time between attempts
        retryDelayUnit - delay time unit between attempts
        environment - the KiwiEnvironment to use when sleeping between attempts
        type - the type of object we are attempting to return, used when logging attempts
        level - the SLF4J log Level at which to log retries
        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 static <T> RetryResult<T> tryGetObjectCollectingErrors​(int maxAttempts,
                                                                      long retryDelay,
                                                                      TimeUnit retryDelayUnit,
                                                                      Supplier<T> supplier)
        Try to get an object, making up to maxAttempts attempts. Logs first attempt and retries at TRACE level using "object" as the description.
        Type Parameters:
        T - the type of object
        Parameters:
        maxAttempts - the maximum number of attempts to make before giving up
        retryDelay - constant delay time between attempts
        retryDelayUnit - delay time unit between attempts
        supplier - on success return the object; return null or throw exception if attempt failed
        Returns:
        a RetryResult
      • tryGetObjectCollectingErrors

        public static <T> RetryResult<T> tryGetObjectCollectingErrors​(int maxAttempts,
                                                                      long retryDelay,
                                                                      TimeUnit retryDelayUnit,
                                                                      Class<T> type,
                                                                      Supplier<T> supplier)
        Try to get an object, making up to maxAttempts attempts. Logs first attempt and retries at TRACE level using the given type as the description.
        Type Parameters:
        T - the type of object
        Parameters:
        maxAttempts - the maximum number of attempts to make before giving up
        retryDelay - constant delay time between attempts
        retryDelayUnit - delay time unit between attempts
        type - the type of object we are attempting to return, used when logging attempts
        supplier - on success return the object; return null or throw exception if attempt failed
        Returns:
        a RetryResult
      • tryGetObjectCollectingErrors

        public static <T> RetryResult<T> tryGetObjectCollectingErrors​(int maxAttempts,
                                                                      long retryDelay,
                                                                      TimeUnit retryDelayUnit,
                                                                      KiwiEnvironment environment,
                                                                      Class<T> type,
                                                                      Supplier<T> supplier)
        Try to get an object, making up to maxAttempts attempts. Logs first attempt and retries at TRACE level using the given type as the description.
        Type Parameters:
        T - the type of object
        Parameters:
        maxAttempts - the maximum number of attempts to make before giving up
        retryDelay - constant delay time between attempts
        retryDelayUnit - delay time unit between attempts
        environment - the KiwiEnvironment to use when sleeping between attempts
        type - the type of object we are attempting to return, used when logging attempts
        supplier - on success return the object; return null or throw exception if attempt failed
        Returns:
        a RetryResult
      • tryGetObjectCollectingErrors

        public static <T> RetryResult<T> tryGetObjectCollectingErrors​(int maxAttempts,
                                                                      long retryDelay,
                                                                      TimeUnit retryDelayUnit,
                                                                      KiwiEnvironment environment,
                                                                      String type,
                                                                      Supplier<T> supplier)
        Try to get an object, making up to maxAttempts attempts. Logs first attempt and retries at TRACE level using the given type as the description.
        Type Parameters:
        T - the type of object
        Parameters:
        maxAttempts - the maximum number of attempts to make before giving up
        retryDelay - constant delay time between attempts
        retryDelayUnit - delay time unit between attempts
        environment - the KiwiEnvironment to use when sleeping between attempts
        type - the type of object we are attempting to return, used when logging attempts
        supplier - on success return the object; return null or throw exception if attempt failed
        Returns:
        a RetryResult
      • tryGetObjectCollectingErrors

        public static <T> RetryResult<T> tryGetObjectCollectingErrors​(int maxAttempts,
                                                                      long retryDelay,
                                                                      TimeUnit retryDelayUnit,
                                                                      KiwiEnvironment environment,
                                                                      String type,
                                                                      org.slf4j.event.Level level,
                                                                      Supplier<T> supplier)
        Try to get an object, making up to maxAttempts attempts. Logs first attempt at TRACE level and logs retries at the given level, always using type as the description.
        Type Parameters:
        T - the type of object
        Parameters:
        maxAttempts - the maximum number of attempts to make before giving up
        retryDelay - constant delay time between attempts
        retryDelayUnit - delay time unit between attempts
        environment - the KiwiEnvironment to use when sleeping between attempts
        type - the type of object we are attempting to return, used when logging attempts
        level - the SLF4J log Level at which to log retries
        supplier - on success return the object; return null or throw exception if attempt failed
        Returns:
        a RetryResult