Class Retry
- java.lang.Object
-
- io.pravega.common.util.Retry
-
public final class Retry extends java.lang.ObjectA Utility class to support retrying something that can fail with exponential backoff. The class is designed to have a declarative interface for ease of use. It can be used as follows:Retry.withExpBackoff(1, 10, 5) .retryingOn(FooException.class) .throwingOn(RuntimeException.class).run(() -> { //Do stuff here. }The above will retry the code in the block up to 5 times if it throws FooException. If it throws a RuntimeException or returns successfully it will throw or return immediately. The delay following each of the filed attempts would be 1, 10, 100, 1000, and 10000ms respectively. If all retries fail
RetriesExhaustedExceptionwill be thrown.Note that the class is not a builder object, so the methods in the chain must be invoked in order. The intermediate objects in the chain are reusable and threadsafe, so they can be shared between invocations.
In the event that the exception passed to retryingOn() and throwingOn() are related. i.e. In the above example if FooException were to extend RuntimeException. Then the more specific exception is given preference. (In the above case FooException would be retried).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceRetry.Retryable<ReturnT,RetryableET extends java.lang.Exception,NonRetryableET extends java.lang.Exception>static classRetry.RetryAndThrowBase<ThrowsT extends java.lang.Exception>static classRetry.RetryAndThrowConditionallyReturned byRetry.RetryExceptionally.throwingOn(Class)to add the type of exception that should cause the method to throw right away.static classRetry.RetryAndThrowExceptionally<RetryT extends java.lang.Exception,ThrowsT extends java.lang.Exception>Returned byRetry.RetryExceptionally.throwingOn(Class)to add the type of exception that should cause the method to throw right away.static classRetry.RetryExceptionally<RetryT extends java.lang.Exception>Returned byRetry.RetryWithBackoff.retryingOn(Class)to add the type of exception that should result in a retry.static classRetry.RetryUnconditionallyReturned byindefinitelyWithExpBackoff(long, int, long, Consumer)(Class)} to retry indefinitely.static classRetry.RetryWithBackoffReturned bywithExpBackoff(long, int, int)to set the retry schedule.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Retry.RetryUnconditionallyindefinitelyWithExpBackoff(long initialMillis, int multiplier, long maxDelay, java.util.function.Consumer<java.lang.Throwable> consumer)static Retry.RetryUnconditionallyindefinitelyWithExpBackoff(java.lang.String failureMessage)static Retry.RetryWithBackoffwithExpBackoff(long initialMillis, int multiplier, int attempts)static Retry.RetryWithBackoffwithExpBackoff(long initialMillis, int multiplier, int attempts, long maxDelay)
-
-
-
Method Detail
-
withExpBackoff
public static Retry.RetryWithBackoff withExpBackoff(long initialMillis, int multiplier, int attempts)
-
withExpBackoff
public static Retry.RetryWithBackoff withExpBackoff(long initialMillis, int multiplier, int attempts, long maxDelay)
-
indefinitelyWithExpBackoff
public static Retry.RetryUnconditionally indefinitelyWithExpBackoff(long initialMillis, int multiplier, long maxDelay, java.util.function.Consumer<java.lang.Throwable> consumer)
-
indefinitelyWithExpBackoff
public static Retry.RetryUnconditionally indefinitelyWithExpBackoff(java.lang.String failureMessage)
-
-