Class RetryExceptionHandler

java.lang.Object
cloud.opencode.base.event.handler.RetryExceptionHandler
All Implemented Interfaces:
EventExceptionHandler

public class RetryExceptionHandler extends Object implements EventExceptionHandler
Retry Exception Handler 重试异常处理器

Exception handler that retries failed listeners with backoff.

使用退避策略重试失败监听器的异常处理器。

Features | 主要功能:

  • Configurable retry count - 可配置的重试次数
  • Delay between retries - 重试之间的延迟
  • Dead letter queue - 死信队列

Usage Examples | 使用示例:

RetryExceptionHandler handler = new RetryExceptionHandler(3, Duration.ofSeconds(1));

// With retry action
handler.setRetryAction(event -> eventBus.publish(event));

// Process dead letters
handler.processDeadLetters(failedEvent -> {
    log.error("Permanently failed: {}", failedEvent);
});

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
Since:
JDK 25, opencode-base-event V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • RetryExceptionHandler

      public RetryExceptionHandler()
      Create retry handler with default settings 使用默认设置创建重试处理器
    • RetryExceptionHandler

      public RetryExceptionHandler(int maxRetries, Duration delay)
      Create retry handler with custom settings 使用自定义设置创建重试处理器
      Parameters:
      maxRetries - max retry attempts | 最大重试次数
      delay - delay between retries | 重试之间的延迟
  • Method Details

    • setRetryAction

      public void setRetryAction(Consumer<Event> retryAction)
      Set the retry action 设置重试动作
      Parameters:
      retryAction - the action to execute on retry | 重试时执行的动作
    • handleException

      public void handleException(Event event, Throwable exception, String listenerName)
      Handle exception with retry logic 使用重试逻辑处理异常
      Specified by:
      handleException in interface EventExceptionHandler
      Parameters:
      event - the event being processed | 正在处理的事件
      exception - the exception that occurred | 发生的异常
      listenerName - the name of the listener that threw | 抛出异常的监听器名称
    • processDeadLetters

      public void processDeadLetters(Consumer<RetryExceptionHandler.FailedEvent> handler)
      Process dead letter queue 处理死信队列
      Parameters:
      handler - the handler for failed events | 失败事件的处理器
    • getDeadLetterQueueSize

      public int getDeadLetterQueueSize()
      Get dead letter queue size 获取死信队列大小
      Returns:
      queue size | 队列大小
    • clearDeadLetterQueue

      public void clearDeadLetterQueue()
      Clear dead letter queue 清除死信队列
    • getMaxRetries

      public int getMaxRetries()
      Get max retries 获取最大重试次数
      Returns:
      max retries | 最大重试次数
    • getDelay

      public Duration getDelay()
      Get retry delay 获取重试延迟
      Returns:
      retry delay | 重试延迟