javaquarkusmicroprofilefault-tolerancequarkus-rest-client

How to configure the SmallRye Fault Tolerance annotation values at the runtime in the application.yml file such as maxRetries for @Retry?


I am using the Quarkus SmallRye Fault Tolerance annotation such as @Retry within my Quarkus application. I have many methods with Retry logic for which I want to use the SmallRye Fault Tolerance so I changed old code from:

.onFailure()
.retry()
.withBackOff(200)
.withJitter(200)
.atMost(5)
.onFailure()
.recoverWithUni(throwable -> Uni.createFrom().item("failed"));

To this new code with @Retry (The method is a public method with a class annotated with @ApplicationScoped):

@Retry(maxRetries = 5, delay = 200, jitter = 200)
@Fallback(fallbackMethod = "handleFailure")

The above annotations work fine on the failure and can run 5+1 six times during the failure and finally return the default value onFailure from the handleFailure method.

Since I have many methods and classes in my application I don't want to define the values for maxRetries, delay, jitter on each of them rather I want to define them within application.yml properties file.

In future, if I want to change any information I can change it from the properties file and on methods just define the annotation @Retry so the rest of the information will be picked up during the run-time from the application.yml properties file.

Based on the documentation I tried the following:

application.yml file:

Retry/maxRetries=5
Retry/delay=200
Retry/jitter=200

But this does not seem to work at all for me. It's for some reason using the default values, not the ones I provided in my config.

Is it possible to configure the values for SmallRye Fault Tolerance in the application.yml file? I did not find any concrete example anywhere in the documentation or on GitHub. Can someone help me with this issue? What is the right way to define the run-time configuration for SmallRye Fault Tolerance? Please provide a complete YML tree so I can see these values should go under which parent

quarkus:
  # ... other existing configurations ...

  smallrye-fault-tolerance:
    retry:
      max-retries: 5

Update on 25.10 based on a comment from @Ladicek :

I tried to create a simple Quarkus app with SmallRye Fault Tolerance with application.properties and it worked pretty fine with @Retry when making the API calls:

quarkus.http.port=8081
Retry/maxRetries=8
Retry/delay=2
Retry/delayUnit=SECONDS

Following is my CakeResource.java:

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.microprofile.faulttolerance.Fallback;
import org.eclipse.microprofile.faulttolerance.Retry;
import org.jboss.logging.Logger;

import java.util.Random;


@Path("/api/bake")
@ApplicationScoped
public class CakeResource {
    private static final Logger logger = Logger.getLogger(CakeResource.class);
    private final Random r = new Random();

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Retry()
    @Fallback(fallbackMethod = "defaultFallback")
    public String bakeCake() throws Exception {
        logger.info("****** Newly Backing Cake ********");
        final Double randDouble = r.nextDouble();
        logger.info(randDouble);

        if (randDouble < 0.5) {
            logger.info(" No items left sorry .... ");
            throw new Exception();
        }

        if (randDouble < 0.999) {
            logger.info(" oops Cake burned... ");
            throw new Exception();
        }

        logger.info(" Cake prepared ");

        return "Here is your take, Guten Appetit\n";
    }

    public String defaultFallback() {
        return "Sorry unable prepare the cake today \n";
    }
}

Now for the same above CakeResource, I used the application.yml instead of application.properties. I added quarkus-config-yaml to the pom.xml file and changed application.yml to this:

quarkus:
  http:
    port: 8081

Retry:
  maxRetries: 5
  delay: 2
  delayUnit: SECONDS

The @Retry config in application.yml was ignored, defaulting to maxRetries: 3 and delay: 0. Other settings, like port, were applied, so application.yml was recognized, but not for SmallRye Fault Tolerance configs.

I also tried the following:

Approach-1 :

quarkus:
  http:
    port: 8082

smallrye-fault-tolerance:
  retry:
    max-retries: 5
    delay: PT2S

Approach-2 :

quarkus:
  http:
    port: 8083

mp:
  fault-tolerance:
    retry:
      max-retries: 3 
      delay: 500     
      jitter: 100    

Approach-3 :

quarkus:
  http:
    port: 8081

  smallrye-fault-tolerance:
    retry:
      max-retries: 5
      delay: PT2S
      jitter: PT0.2S

Approach-4:

quarkus:
  http:
    port: 8081

mp:
  fault-tolerance:
    retry:
      max-retries: 5
      delay: PT2S
      jitter: PT0.2S

smallrye-fault-tolerance:
  retry:
    max-retries: 5
    delay: PT2S
    jitter: PT0.2S

None of the configurations worked for me in application.yml. I'm unsure if it's a setup issue or if SmallRye Fault Tolerance doesn’t read properties from application.yml. Can you confirm how to use runtime configurations with SmallRye Fault Tolerance via application.yml?


Solution

  • Based on the response provided by @Ladicek:

    There is no support for natively looking Quarkus configuration, so we have to use the raw MicroProfile Fault Tolerance config keys.

    quarkus:
      http:
        port: 8081
    
    Retry/maxRetries: 8
    Retry/delay: 2
    Retry/delayUnit: SECONDS