Package org.apache.curator
Class RetryLoop
- java.lang.Object
-
- org.apache.curator.RetryLoop
-
public class RetryLoop extends java.lang.Object
Mechanism to perform an operation on Zookeeper that is safe against disconnections and "recoverable" errors.
If an exception occurs during the operation, the RetryLoop will process it, check with the current retry policy and either attempt to reconnect or re-throw the exception
Canonical usage:
RetryLoop retryLoop = client.newRetryLoop(); while ( retryLoop.shouldContinue() ) { try { // do your work ZooKeeper zk = client.getZooKeeper(); // it's important to re-get the ZK instance in case there was an error and the instance was re-created retryLoop.markComplete(); } catch ( Exception e ) { retryLoop.takeException(e); } }
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> T
callWithRetry(CuratorZookeeperClient client, java.util.concurrent.Callable<T> proc)
Convenience utility: creates a retry loop calling the given proc and retrying if neededstatic RetrySleeper
getDefaultRetrySleeper()
Returns the default retry sleeperstatic boolean
isRetryException(java.lang.Throwable exception)
Utility - return true if the given exception is retry-ablevoid
markComplete()
Call this when your operation has successfully completedboolean
shouldContinue()
If true is returned, make an attempt at the operationstatic boolean
shouldRetry(int rc)
Utility - return true if the given Zookeeper result code is retry-ablevoid
takeException(java.lang.Exception exception)
Pass any caught exceptions here
-
-
-
Method Detail
-
getDefaultRetrySleeper
public static RetrySleeper getDefaultRetrySleeper()
Returns the default retry sleeper- Returns:
- sleeper
-
callWithRetry
public static <T> T callWithRetry(CuratorZookeeperClient client, java.util.concurrent.Callable<T> proc) throws java.lang.Exception
Convenience utility: creates a retry loop calling the given proc and retrying if needed- Type Parameters:
T
- return type- Parameters:
client
- Zookeeperproc
- procedure to call with retry- Returns:
- procedure result
- Throws:
java.lang.Exception
- any non-retriable errors
-
shouldContinue
public boolean shouldContinue()
If true is returned, make an attempt at the operation- Returns:
- true/false
-
markComplete
public void markComplete()
Call this when your operation has successfully completed
-
shouldRetry
public static boolean shouldRetry(int rc)
Utility - return true if the given Zookeeper result code is retry-able- Parameters:
rc
- result code- Returns:
- true/false
-
isRetryException
public static boolean isRetryException(java.lang.Throwable exception)
Utility - return true if the given exception is retry-able- Parameters:
exception
- exception to check- Returns:
- true/false
-
takeException
public void takeException(java.lang.Exception exception) throws java.lang.Exception
Pass any caught exceptions here- Parameters:
exception
- the exception- Throws:
java.lang.Exception
- if not retry-able or the retry policy returned negative
-
-