2

I am using the resilience4j library to retry some code, I have the following code below, I expect it to run 4 times. If I throw IllegalArgumentException it works but if I throw ConnectException it doesn't.

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()
}


I expected that retry to retry on all exceptions.

1 Answers1

0

You are creating decorated supplier which is only catching RuntimeExceptions whilst ConnectException is not a RuntimeException:

    ... decorateSupplier(Retry retry, Supplier<T> supplier) {
        return () -> {
            Retry.Context<T> context = retry.context();
            do try {
               ...
            } catch (RuntimeException runtimeException) {
               ...

Look through the Retry.java and choose one that catches Exception for example decorateCheckedFunction, for example

  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)

which outputs

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

Personally I use softwaremill/retry

Mario Galic
  • 47,285
  • 6
  • 56
  • 98