Class ExponentialBackoffStrategy

java.lang.Object
com.ultikits.ultitools.websocket.ExponentialBackoffStrategy
All Implemented Interfaces:
ReconnectStrategy

public class ExponentialBackoffStrategy extends Object implements ReconnectStrategy
Exponential backoff reconnection strategy. Delays increase exponentially: 5s, 10s, 20s, 40s... up to max 5 minutes.

指数退避重连策略。 延迟指数增长:5秒、10秒、20秒、40秒...最大5分钟。

Since:
6.2.0
Version:
1.0.0
Author:
wisdomme
  • Field Details

    • DEFAULT_INITIAL_DELAY

      public static final long DEFAULT_INITIAL_DELAY
      Initial delay in milliseconds (5 seconds).
      See Also:
    • DEFAULT_MAX_DELAY

      public static final long DEFAULT_MAX_DELAY
      Maximum delay in milliseconds (5 minutes).
      See Also:
    • DEFAULT_MULTIPLIER

      public static final double DEFAULT_MULTIPLIER
      Default multiplier for exponential growth.
      See Also:
    • DEFAULT_MAX_ATTEMPTS

      public static final int DEFAULT_MAX_ATTEMPTS
      Default maximum number of attempts (0 = unlimited).
      See Also:
    • initialDelay

      private final long initialDelay
    • maxDelay

      private final long maxDelay
    • multiplier

      private final double multiplier
    • maxAttempts

      private final int maxAttempts
    • attemptCount

      private int attemptCount
  • Constructor Details

    • ExponentialBackoffStrategy

      public ExponentialBackoffStrategy()
      Creates a strategy with default settings. 使用默认设置创建策略。
    • ExponentialBackoffStrategy

      public ExponentialBackoffStrategy(long initialDelay, long maxDelay, double multiplier, int maxAttempts)
      Creates a strategy with custom settings. 使用自定义设置创建策略。
      Parameters:
      initialDelay - initial delay in milliseconds
      maxDelay - maximum delay in milliseconds
      multiplier - multiplier for exponential growth
      maxAttempts - maximum attempts (0 for unlimited)
  • Method Details

    • getNextDelay

      public long getNextDelay()
      Description copied from interface: ReconnectStrategy
      Gets the delay in milliseconds before the next reconnection attempt. 获取下次重连尝试前的延迟(毫秒)。
      Specified by:
      getNextDelay in interface ReconnectStrategy
      Returns:
      the delay in milliseconds
    • reset

      public void reset()
      Description copied from interface: ReconnectStrategy
      Resets the strategy state (called on successful connection). 重置策略状态(在连接成功时调用)。
      Specified by:
      reset in interface ReconnectStrategy
    • getAttemptCount

      public int getAttemptCount()
      Description copied from interface: ReconnectStrategy
      Gets the current attempt count. 获取当前尝试次数。
      Specified by:
      getAttemptCount in interface ReconnectStrategy
      Returns:
      the number of attempts made
    • shouldContinue

      public boolean shouldContinue()
      Description copied from interface: ReconnectStrategy
      Checks if reconnection should continue. 检查是否应该继续重连。
      Specified by:
      shouldContinue in interface ReconnectStrategy
      Returns:
      true if more reconnection attempts should be made
    • unlimited

      public static ExponentialBackoffStrategy unlimited()
      Creates a strategy that tries forever with default backoff. 创建使用默认退避的无限重试策略。
      Returns:
      an unlimited retry strategy
    • withMaxAttempts

      public static ExponentialBackoffStrategy withMaxAttempts(int maxAttempts)
      Creates a strategy with limited attempts. 创建有限尝试次数的策略。
      Parameters:
      maxAttempts - the maximum number of attempts
      Returns:
      a limited retry strategy
    • builder

      public static ExponentialBackoffStrategy.Builder builder()
      Creates a builder for custom configuration. 创建自定义配置的构建器。
      Returns:
      a new builder