You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Resilience4j函数式配置时为何未触发重试?

Resilience4j函数式配置重试未触发的原因及解决方法

问题原因

你创建了经过Retry装饰的CheckedSupplier,但没有执行这个装饰后的对象,而是直接调用了原服务方法myService.bar(),导致重试逻辑完全没有被触发。

解决方法

需要调用装饰后的CheckedSupplier的get()方法,才能让重试逻辑生效。修改后的代码如下:

RetryConfig config = RetryConfig.custom()
        .maxAttempts(6)
        .waitDuration(Duration.ofMillis(100))
        .retryExceptions(RuntimeException.class) // 显式指定要重试的异常(可选,默认已包含RuntimeException)
        .build();

RetryRegistry registry = RetryRegistry.of(config);

Retry retry = registry.retry("name1");
// 装饰方法并得到带重试逻辑的Supplier
CheckedSupplier<String> decoratedSupplier = Retry.decorateCheckedSupplier(retry, myService::bar);

try {
    // 执行装饰后的方法,触发重试
    decoratedSupplier.get();
} catch (Exception e) {
    // 处理最终重试失败后的异常
    e.printStackTrace();
}

补充说明

  • Retry.decorateCheckedSupplier只是包装了原方法,返回一个带重试逻辑的新对象,必须调用这个新对象的方法才会触发重试。
  • 虽然默认情况下Resilience4j会对RuntimeException进行重试,但显式通过retryExceptions()指定可以让配置更清晰。
  • 原方法bar()抛出InterruptedException,所以装饰后的get()方法会抛出Exception,需要在调用时捕获或声明抛出。

内容的提问来源于stack exchange,提问作者gstackoverflow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 07:17:04