How to Retry on Failures

If an operation fails, it is possible to retry the operation in case the failure was just a temporary glitch.

In case an operation fails due to the target system being overloaded, retrying will likely make the overload situation even worse. Use sparingly, preferably with a concurrency limit or a circuit breaker.

@Retry

The @Retry annotation specifies that a method should be called again in case a previous execution thrown an exception.

@ApplicationScoped
public class MyService {
    @Retry (1)
    public String hello() {
        ...
    }
}
1 Declares that if hello() throws an exception, the exception is swallowed and hello() is called again. Since there is no configuration, at most 3 retry attempts will be done before rethrowing the exception.

It is possible to specify the maximum number of retry attempts, as well as the maximum duration for which retries should be attempted. Further, it is possible to specify some delay between retry attempts, together with random jitter. Finally, it is possible to specify that calls should be retried only for certain exception types or that retrying should be aborted for certain exception types.

For more information, see the Retry reference guide.