Class RetryOptions


  • public class RetryOptions
    extends Object
    Configuration options for HTTP request retry mechanism.

    This class allows customization of retry behavior for failed HTTP requests. By default, retries are enabled with exponential backoff strategy.

    Default Configuration:

    • Retry limit: 3 attempts
    • Retry delay: 1000ms (1 second)
    • Backoff strategy: EXPONENTIAL
    • Retryable status codes: 408, 429, 502, 503, 504
    • Retry enabled: true

    Example Usage:

    
     // Custom retry configuration
     RetryOptions options = new RetryOptions()
         .setRetryLimit(5)
         .setRetryDelay(2000)
         .setBackoffStrategy(BackoffStrategy.LINEAR)
         .setRetryableStatusCodes(429, 502, 503);
     
     Config config = new Config();
     config.setRetryOptions(options);
     
     // Disable retries
     RetryOptions noRetry = new RetryOptions().setRetryEnabled(false);
     
    Since:
    2.0.0
    • Constructor Detail

      • RetryOptions

        public RetryOptions()
        Creates RetryOptions with default configuration.

        Defaults: 3 retries, 1000ms delay, exponential backoff, retries on [408, 429, 502, 503, 504], enabled.

    • Method Detail

      • setRetryLimit

        public RetryOptions setRetryLimit​(int limit)
        Sets the maximum number of retry attempts.
        Parameters:
        limit - maximum retry attempts (0-10)
        Returns:
        this RetryOptions instance for method chaining
        Throws:
        IllegalArgumentException - if limit is negative or exceeds maximum
      • setRetryDelay

        public RetryOptions setRetryDelay​(long delayMs)
        Sets the base delay between retry attempts in milliseconds.
        Parameters:
        delayMs - base delay in milliseconds (must be non-negative)
        Returns:
        this RetryOptions instance for method chaining
        Throws:
        IllegalArgumentException - if delay is negative
      • setBackoffStrategy

        public RetryOptions setBackoffStrategy​(RetryOptions.BackoffStrategy strategy)
        Sets the backoff strategy for calculating retry delays.
        Parameters:
        strategy - backoff strategy (FIXED, LINEAR, or EXPONENTIAL)
        Returns:
        this RetryOptions instance for method chaining
        Throws:
        NullPointerException - if strategy is null
      • setRetryableStatusCodes

        public RetryOptions setRetryableStatusCodes​(int... codes)
        Sets the HTTP status codes that should trigger a retry.

        Only requests that fail with these status codes will be retried. Other status codes will fail immediately.

        If null or empty array is provided, no status code-based retries will occur.

        Parameters:
        codes - HTTP status codes to retry (e.g., 429, 502, 503)
        Returns:
        this RetryOptions instance for method chaining
        Throws:
        IllegalArgumentException - if any status code is outside valid HTTP range (100-599)
      • setRetryEnabled

        public RetryOptions setRetryEnabled​(boolean enabled)
        Enables or disables the retry mechanism.

        When disabled, no retries will occur regardless of other settings.

        Parameters:
        enabled - true to enable retries, false to disable
        Returns:
        this RetryOptions instance for method chaining
      • getCustomBackoffStrategy

        public CustomBackoffStrategy getCustomBackoffStrategy()
        Returns the custom backoff strategy.
      • hasCustomBackoff

        public boolean hasCustomBackoff()
        Checks if custom backoff is configured.
      • getRetryLimit

        public int getRetryLimit()
        Returns the maximum number of retry attempts.
        Returns:
        retry limit (0-10)
      • getRetryDelay

        public long getRetryDelay()
        Returns the base delay in milliseconds between retry attempts.
        Returns:
        retry delay in milliseconds
      • getBackoffStrategy

        public RetryOptions.BackoffStrategy getBackoffStrategy()
        Returns the backoff strategy used for calculating retry delays.
        Returns:
        backoff strategy (FIXED, LINEAR, or EXPONENTIAL)
      • getRetryableStatusCodes

        public int[] getRetryableStatusCodes()
        Returns the HTTP status codes that trigger retries.

        Returns a copy to prevent external modification.

        Returns:
        array of retryable HTTP status codes
      • isRetryEnabled

        public boolean isRetryEnabled()
        Returns whether the retry mechanism is enabled.
        Returns:
        true if retry is enabled, false otherwise
      • toString

        public String toString()
        Returns a string representation of the retry configuration. Useful for debugging and logging.
        Overrides:
        toString in class Object
        Returns:
        string representation of this configuration