Example usage for org.apache.zookeeper AsyncCallback.VoidCallback AsyncCallback.VoidCallback

List of usage examples for org.apache.zookeeper AsyncCallback.VoidCallback AsyncCallback.VoidCallback

Introduction

In this page you can find the example usage for org.apache.zookeeper AsyncCallback.VoidCallback AsyncCallback.VoidCallback.

Prototype

AsyncCallback.VoidCallback

Source Link

Usage

From source file:com.dw.zk.DefaultZkSessionManager.java

License:Apache License

@Override
public ZooKeeper getZooKeeper() {
    synchronized (DefaultZkSessionManager.class) {
        if (shutdown)
            throw new IllegalStateException("Cannot request a ZooKeeper after the session has been closed!");
        if (zk == null || zk.getState() == ZooKeeper.States.CLOSED) {
            if (logger.isDebugEnabled()) {
                if (zk == null) {
                    logger.debug("Found NULL ZK state.");
                } else {
                    logger.debug("Found CLOSED ZK state.");
                }/*from   w w  w  . ja  va 2s. c  o  m*/
            }

            zk = getNewZookeeperInstance();
        } else {
            // Before calling zk.sync we must ensure ZK is fully connected.
            // If it is in a CONNECTING state and not a CONNECTED state
            // we must wait before sync otherwise problems will occur.
            logger.debug("Preparing to sync a ZK Instance");
            try {
                ensureConnectionEstablishedBeforeProceeding();

                //make sure that your zookeeper instance is synced
                zk.sync("/", new AsyncCallback.VoidCallback() {
                    @Override
                    public void processResult(int rc, String path, Object ctx) {
                        //                    latch.countDown();
                        //do nothing, we're good
                    }
                }, this);
            } catch (KeeperException.SystemErrorException e) {
                zk = getNewZookeeperInstance();
            }
        }
        return zk;
    }
}

From source file:com.glaf.cluster.catalina.session.ZooKeeperClientImpl.java

License:Apache License

private void resetSession() {
    zooKeeper.sync("/", new AsyncCallback.VoidCallback() {
        @Override/*w  ww. jav a  2s.com*/
        public void processResult(int i, String s, Object o) {
            sessionRestartLock.lock();
            try {
                logger.trace("Checking if ZooKeeper session should be restarted");
                if (!connected()) {
                    logger.info("Restarting ZooKeeper discovery");
                    try {
                        logger.trace("Stopping ZooKeeper");
                        doStop();
                    } catch (Exception ex) {
                        logger.error("Error stopping ZooKeeper", ex);
                    }
                    while (!started()) {
                        try {
                            logger.trace("Starting ZooKeeper");
                            doStart();
                            logger.trace("Started ZooKeeper");
                            notifySessionReset();
                            return;
                        } catch (Exception ex) {
                            if (ex.getCause() != null && ex.getCause() instanceof InterruptedException) {
                                logger.info("ZooKeeper startup was interrupted", ex);
                                Thread.currentThread().interrupt();
                                return;
                            }
                            logger.warn("Error starting ZooKeeper ", ex);
                        }
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException ex) {
                            return;
                        }
                    }
                } else {
                    logger.trace("ZooKeeper is already restarted. Ignoring");
                }

            } finally {
                sessionRestartLock.unlock();
            }
        }
    }, null);

}

From source file:com.iquanwai.confucius.biz.util.zk.RobustZooKeeper.java

License:Apache License

public void sync() throws IOException, InterruptedException {
    log.info("Called sync() on client " + this.clientNumber);
    final CountDownLatch waitForSync = new CountDownLatch(1);
    final ZooKeeper c = getClient();
    assert c.getState().isAlive();

    c.sync("/", new AsyncCallback.VoidCallback() {
        @Override/*from  w w w. jav a2s.  co m*/
        public void processResult(int rc, String path, Object ctx) {
            log.info("Sync callback triggered on client " + RobustZooKeeper.this.clientNumber);
            waitForSync.countDown();
        }
    }, null);

    log.info("Waitng for sync callback on client " + this.clientNumber);
    waitForSync.await();

    log.info("sync() finished on " + this.clientNumber);
}

From source file:com.jbrisbin.vpc.zk.GroovyZooKeeperHelper.java

License:Apache License

/**
 * Asynchronously delete the node at the given path, with the specified version, and execute the given {@link
 * groovy.lang.Closure} as a callback.//from   w ww  .  j  a v a2s.  co m
 *
 * @param path
 * @param version
 * @param callback
 */
public void delete(Object path, int version, final Closure callback) {
    zookeeper.delete(getPathAsString(path), version, new AsyncCallback.VoidCallback() {
        public void processResult(int rc, String path, Object ctx) {
            callback.setProperty("returnCode", rc);
            callback.setProperty("path", path);
            callback.setDelegate(ctx);
            callback.call();
        }
    }, this);
}

From source file:com.sonian.elasticsearch.zookeeper.client.ZooKeeperClientService.java

License:Apache License

private void resetSession() {
    if (lifecycle.started()) {
        zooKeeper.sync("/", new AsyncCallback.VoidCallback() {
            @Override/* www  .j  a v a2s . c  o m*/
            public void processResult(int i, String s, Object o) {
                sessionRestartLock.lock();
                try {
                    logger.trace("Checking if ZooKeeper session should be restarted");
                    if (lifecycle.started()) {
                        if (!connected()) {
                            logger.info("Restarting ZooKeeper discovery");
                            try {
                                logger.trace("Stopping ZooKeeper");
                                doStop();
                            } catch (Exception ex) {
                                logger.error("Error stopping ZooKeeper", ex);
                            }
                            while (lifecycle.started()) {
                                try {
                                    logger.trace("Starting ZooKeeper");
                                    doStart();
                                    logger.trace("Started ZooKeeper");
                                    notifySessionReset();
                                    return;
                                } catch (ZooKeeperClientException ex) {
                                    if (ex.getCause() != null
                                            && ex.getCause() instanceof InterruptedException) {
                                        logger.info("ZooKeeper startup was interrupted", ex);
                                        Thread.currentThread().interrupt();
                                        return;
                                    }
                                    logger.warn("Error starting ZooKeeper ", ex);
                                }
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException ex) {
                                    return;
                                }
                            }
                        } else {
                            logger.trace("ZooKeeper is already restarted. Ignoring");
                        }
                    }
                } finally {
                    sessionRestartLock.unlock();
                }
            }
        }, null);
    }

}

From source file:org.midonet.midolman.state.ZkDirectory.java

License:Apache License

@Override
public void asyncDelete(String relativePath, final DirectoryCallback.Void callback) {
    zk.getZooKeeper().delete(relativePath, -1, new AsyncCallback.VoidCallback() {
        @Override/*ww w  .j  a v  a2  s. com*/
        public void processResult(int rc, String path, Object ctx) {
            if (rc == KeeperException.Code.OK.intValue()) {
                callback.onSuccess(null);
            } else {
                callback.onError(KeeperException.create(KeeperException.Code.get(rc), path));
            }
        }
    }, null);
}

From source file:org.midonet.midolman.state.ZkDirectory.java

License:Apache License

@Override
public void asyncDelete(String relativePath) {
    String absPath = getAbsolutePath(relativePath);
    zk.getZooKeeper().delete(absPath, -1, new AsyncCallback.VoidCallback() {
        @Override/*from  w  w w  .  j av a 2 s .c  o m*/
        public void processResult(int rc, String path, Object ctx) {

        }
    }, null);
}

From source file:org.rioproject.zookeeper.client.GroupManagement.java

License:Apache License

public void delete(final String groupName, final String memberName) {
    if (groupName == null)
        throw new IllegalArgumentException("groupName must not be null");
    if (memberName == null)
        throw new IllegalArgumentException("memberName must not be null");
    String path = String.format("/%s/%s", groupName, memberName);
    zooKeeper.delete(path, -1, new AsyncCallback.VoidCallback() {
        public void processResult(int rc, String path, Object context) {
            if (KeeperException.Code.OK.equals(KeeperException.Code.get(rc))) {
                logger.info("Deleted {}", path);
            }// w  w w .jav  a  2s .  c om
        }
    }, null);
}