使用Resilience4j重试在某些例外情况下不起作用

时间:2019-06-25 11:56:09

标签: java scala resilience4j

我正在使用resilience4j库重试一些代码,下面有以下代码,我希望它可以运行4次。如果我抛出IllegalArgumentException,它将起作用,但如果抛出ConnectException,则不会。

object Test extends App {

  val retryConf = RetryConfig.custom()
    .maxAttempts(4)
    .retryOnException(_ => true)
    //.retryExceptions(classOf[ConnectException])
    .build
  val retryRegistry = RetryRegistry.of(retryConf)
  val retryConfig = retryRegistry.retry("test", retryConf)
  val supplier: Supplier[Unit] = () => {
    println("Run")
    throw new IllegalArgumentException("Test")
    //throw new ConnectException("Test")
  }

  val decoratedSupplier = Decorators.ofSupplier(supplier).withRetry(retryConfig).get()
}


我希望可以重试所有异常。

1 个答案:

答案 0 :(得分:1)

您创建的decorated supplier仅捕获ConnectException,而RuntimeException不是 ... decorateSupplier(Retry retry, Supplier<T> supplier) { return () -> { Retry.Context<T> context = retry.context(); do try { ... } catch (RuntimeException runtimeException) { ...

Exception

浏览Retry.java,然后选择一个捕获 val registry = RetryRegistry.of(RetryConfig.custom().maxAttempts(4).build()) val retry = registry.retry("my") Retry.decorateCheckedFunction(retry, (x: Int) => { println(s"woohoo $x") throw new ConnectException("Test") 42 }).apply(1) 的对象,例如decorateCheckedFunction

woohoo 1
woohoo 1
woohoo 1
woohoo 1
Exception in thread "main" java.rmi.ConnectException: Test

输出

industry_id

我个人使用softwaremill/retry