Example usage for org.apache.zookeeper.recipes.lock ZooKeeperOperation execute

List of usage examples for org.apache.zookeeper.recipes.lock ZooKeeperOperation execute

Introduction

In this page you can find the example usage for org.apache.zookeeper.recipes.lock ZooKeeperOperation execute.

Prototype

boolean execute() throws KeeperException, InterruptedException;

Source Link

Document

Performs the operation - which may be involved multiple times if the connection to ZooKeeper closes during this operation.

Usage

From source file:com.zookeeper.lyt.lock.ProtocolSupport.java

License:Apache License

/**
 * Perform the given operation, retrying if the connection fails
 * @return object. it needs to be cast to the callee's expected 
 * return type./*from   ww  w .j ava2 s  . c  o  m*/
 */
protected Object retryOperation(ZooKeeperOperation operation) throws KeeperException, InterruptedException {
    KeeperException exception = null;
    for (int i = 0; i < retryCount; i++) {
        try {
            return operation.execute();
        } catch (KeeperException.SessionExpiredException e) {
            LOG.warn("Session expired for: " + zookeeper + " so reconnecting due to: " + e, e);
            throw e;
        } catch (KeeperException.ConnectionLossException e) {
            if (exception == null) {
                exception = e;
            }
            LOG.debug("Attempt " + i + " failed with connection loss so " + "attempting to reconnect: " + e, e);
            retryDelay(i);
        }
    }
    throw exception;
}