Class LinearBackoffRetryStrategy

java.lang.Object
io.leandev.appfuse.http.retry.LinearBackoffRetryStrategy
All Implemented Interfaces:
RetryStrategy

public class LinearBackoffRetryStrategy extends Object implements RetryStrategy

線性退避重試策略

使用線性增長的延遲時間進行重試:

  • 第 1 次重試:延遲 0 秒
  • 第 2 次重試:延遲 1 秒
  • 第 3 次重試:延遲 2 秒
  • 依此類推...
  • Constructor Details

    • LinearBackoffRetryStrategy

      public LinearBackoffRetryStrategy(long delayMillis)
      建立線性退避重試策略
      Parameters:
      delayMillis - 基礎延遲時間(毫秒),預設 1000ms
    • LinearBackoffRetryStrategy

      public LinearBackoffRetryStrategy(long delayMillis, RetryStrategy.RetryCallback callback)
      建立線性退避重試策略(帶回調)
      Parameters:
      delayMillis - 基礎延遲時間(毫秒)
      callback - 重試回調(可選)
    • LinearBackoffRetryStrategy

      public LinearBackoffRetryStrategy()
      建立使用預設延遲(1000ms)的策略
  • Method Details

    • execute

      public <T> T execute(Callable<T> operation, int maxRetries) throws Exception
      Description copied from interface: RetryStrategy
      執行帶重試機制的操作
      Specified by:
      execute in interface RetryStrategy
      Type Parameters:
      T - 返回值類型
      Parameters:
      operation - 要執行的操作
      maxRetries - 最大重試次數(不包含首次嘗試)
      Returns:
      操作結果
      Throws:
      Exception - 如果所有重試都失敗
    • execute

      public <T> T execute(Callable<T> operation, int maxRetries, RetryStrategy.RetryCallback callback) throws Exception
      Description copied from interface: RetryStrategy
      執行帶重試機制的操作(帶回調)
      Specified by:
      execute in interface RetryStrategy
      Type Parameters:
      T - 返回值類型
      Parameters:
      operation - 要執行的操作
      maxRetries - 最大重試次數(不包含首次嘗試)
      callback - 重試回調
      Returns:
      操作結果
      Throws:
      Exception - 如果所有重試都失敗