Class RetryUtil
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface for commands that return a boolean result indicating success or failure.static interfaceInterface for commands that can be retried and may throw exceptions. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidretryOnBoolean(long timeoutms, long intervalms, RetryUtil.BooleanRetryCmd cmd) Retries a command until it returnstrueor the timeout is reached.static voidretryOnException(Class<? extends Exception> clazz, long timeoutms, long intervalms, RetryUtil.RetryCmd cmd) Retries a command when a specific exception type occurs, until successful or timeout is reached.static voidretryOnException(Set<Class<? extends Exception>> classes, long timeoutms, long intervalms, RetryUtil.RetryCmd cmd) Retries a command when any of the specified exception types occur, until successful or timeout is reached.static voidretryUntil(String errorMessage, int retries, long pauseTime, TimeUnit pauseUnit, RetryUtil.BooleanRetryCmd cmd) Retries a command until it returnstrueor the maximum number of retries is reached.
-
Constructor Details
-
RetryUtil
public RetryUtil()
-
-
Method Details
-
retryOnException
public static void retryOnException(Class<? extends Exception> clazz, long timeoutms, long intervalms, RetryUtil.RetryCmd cmd) throws Exception Retries a command when a specific exception type occurs, until successful or timeout is reached.This is a convenience method that delegates to
retryOnException(Set, long, long, RetryCmd)with a singleton set containing the specified exception class.- Parameters:
clazz- the exception class to retry ontimeoutms- maximum time to retry in millisecondsintervalms- wait interval between retries in millisecondscmd- the command to execute- Throws:
Exception- if the command fails with a different exception, or if timeout is reachedInterruptedException- if the thread is interrupted while sleeping between retries
-
retryOnException
public static void retryOnException(Set<Class<? extends Exception>> classes, long timeoutms, long intervalms, RetryUtil.RetryCmd cmd) throws Exception Retries a command when any of the specified exception types occur, until successful or timeout is reached.The command is executed repeatedly until it succeeds (completes without throwing an exception) or the timeout is reached. If an exception matching one of the specified classes is thrown and there is time remaining, the method will sleep for
intervalmsmilliseconds before retrying. If a different exception is thrown, or if the timeout is reached, the exception is rethrown.- Parameters:
classes- set of exception classes to retry ontimeoutms- maximum time to retry in millisecondsintervalms- wait interval between retries in millisecondscmd- the command to execute- Throws:
Exception- if the command fails with an exception not in the specified set, or if timeout is reachedInterruptedException- if the thread is interrupted while sleeping between retries
-
retryUntil
public static void retryUntil(String errorMessage, int retries, long pauseTime, TimeUnit pauseUnit, RetryUtil.BooleanRetryCmd cmd) throws InterruptedException Retries a command until it returnstrueor the maximum number of retries is reached.The command is executed up to
retriestimes. After each failed attempt (when the command returnsfalse), the method pauses for the specified duration before retrying. If all retries are exhausted without success, aSolrExceptionis thrown with the provided error message.- Parameters:
errorMessage- the error message to use if all retries are exhaustedretries- maximum number of retry attemptspauseTime- duration to pause between retriespauseUnit- time unit for the pause durationcmd- the command to execute- Throws:
InterruptedException- if the thread is interrupted while sleeping between retriesSolrException- if all retries are exhausted without success
-
retryOnBoolean
Retries a command until it returnstrueor the timeout is reached.The command is executed repeatedly until it returns
trueor the timeout expires. If the command returnsfalseand there is time remaining, the method sleeps forintervalmsmilliseconds before retrying. If the timeout is reached before the command succeeds, aSolrExceptionis thrown.- Parameters:
timeoutms- maximum time to retry in millisecondsintervalms- wait interval between retries in millisecondscmd- the command to execute- Throws:
SolrException- if no success within timeout
-