Package com.ultikits.ultitools.websocket
Class ExponentialBackoffStrategy
java.lang.Object
com.ultikits.ultitools.websocket.ExponentialBackoffStrategy
- All Implemented Interfaces:
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for ExponentialBackoffStrategy. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intstatic final longInitial delay in milliseconds (5 seconds).static final intDefault maximum number of attempts (0 = unlimited).static final longMaximum delay in milliseconds (5 minutes).static final doubleDefault multiplier for exponential growth.private final longprivate final intprivate final longprivate final double -
Constructor Summary
ConstructorsConstructorDescriptionCreates a strategy with default settings.ExponentialBackoffStrategy(long initialDelay, long maxDelay, double multiplier, int maxAttempts) Creates a strategy with custom settings. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Creates a builder for custom configuration.intGets the current attempt count.longGets the delay in milliseconds before the next reconnection attempt.voidreset()Resets the strategy state (called on successful connection).booleanChecks if reconnection should continue.static ExponentialBackoffStrategyCreates a strategy that tries forever with default backoff.static ExponentialBackoffStrategywithMaxAttempts(int maxAttempts) Creates a strategy with limited attempts.
-
Field Details
-
DEFAULT_INITIAL_DELAY
public static final long DEFAULT_INITIAL_DELAYInitial delay in milliseconds (5 seconds).- See Also:
-
DEFAULT_MAX_DELAY
public static final long DEFAULT_MAX_DELAYMaximum delay in milliseconds (5 minutes).- See Also:
-
DEFAULT_MULTIPLIER
public static final double DEFAULT_MULTIPLIERDefault multiplier for exponential growth.- See Also:
-
DEFAULT_MAX_ATTEMPTS
public static final int DEFAULT_MAX_ATTEMPTSDefault 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 millisecondsmaxDelay- maximum delay in millisecondsmultiplier- multiplier for exponential growthmaxAttempts- maximum attempts (0 for unlimited)
-
-
Method Details
-
getNextDelay
public long getNextDelay()Description copied from interface:ReconnectStrategyGets the delay in milliseconds before the next reconnection attempt. 获取下次重连尝试前的延迟(毫秒)。- Specified by:
getNextDelayin interfaceReconnectStrategy- Returns:
- the delay in milliseconds
-
reset
public void reset()Description copied from interface:ReconnectStrategyResets the strategy state (called on successful connection). 重置策略状态(在连接成功时调用)。- Specified by:
resetin interfaceReconnectStrategy
-
getAttemptCount
public int getAttemptCount()Description copied from interface:ReconnectStrategyGets the current attempt count. 获取当前尝试次数。- Specified by:
getAttemptCountin interfaceReconnectStrategy- Returns:
- the number of attempts made
-
shouldContinue
public boolean shouldContinue()Description copied from interface:ReconnectStrategyChecks if reconnection should continue. 检查是否应该继续重连。- Specified by:
shouldContinuein interfaceReconnectStrategy- Returns:
- true if more reconnection attempts should be made
-
unlimited
Creates a strategy that tries forever with default backoff. 创建使用默认退避的无限重试策略。- Returns:
- an unlimited retry strategy
-
withMaxAttempts
Creates a strategy with limited attempts. 创建有限尝试次数的策略。- Parameters:
maxAttempts- the maximum number of attempts- Returns:
- a limited retry strategy
-
builder
Creates a builder for custom configuration. 创建自定义配置的构建器。- Returns:
- a new builder
-