/** * Verifies that the specified lockId is the owner of the lock. */ public static boolean ownsLock(final ZooKeeperItf zk, final String lockId) throws ZkLockException { if (zk.isCurrentThreadEventThread()) { throw new RuntimeException("ZkLock should not be used from within the ZooKeeper event thread."); } try { int lastSlashPos = lockId.lastIndexOf('/'); final String lockPath = lockId.substring(0, lastSlashPos); String lockName = lockId.substring(lastSlashPos + 1); List<String> children = zk.retryOperation(new ZooKeeperOperation<List<String>>() { @Override public List<String> execute() throws KeeperException, InterruptedException { return zk.getChildren(lockPath, null); } }); if (children.isEmpty()) return false; SortedSet<String> sortedChildren = new TreeSet<String>(children); return sortedChildren.first().equals(lockName); } catch (Throwable t) { throw new ZkLockException("Error checking lock, path: " + lockId, t); } }
/** * Verifies that the specified lockId is the owner of the lock. */ public static boolean ownsLock(final ZooKeeperItf zk, final String lockId) throws ZkLockException { if (zk.isCurrentThreadEventThread()) { throw new RuntimeException("ZkLock should not be used from within the ZooKeeper event thread."); } try { int lastSlashPos = lockId.lastIndexOf('/'); final String lockPath = lockId.substring(0, lastSlashPos); String lockName = lockId.substring(lastSlashPos + 1); List<String> children = zk.retryOperation(new ZooKeeperOperation<List<String>>() { @Override public List<String> execute() throws KeeperException, InterruptedException { return zk.getChildren(lockPath, null); } }); if (children.isEmpty()) return false; SortedSet<String> sortedChildren = new TreeSet<String>(children); return sortedChildren.first().equals(lockName); } catch (Throwable t) { throw new ZkLockException("Error checking lock, path: " + lockId, t); } }
/** * Releases a lock. * * @param lockId the string returned by {@link ZkLock#lock}. * @param ignoreMissing if true, do not throw an exception if the lock does not exist */ public static void unlock(final ZooKeeperItf zk, final String lockId, boolean ignoreMissing) throws ZkLockException { if (zk.isCurrentThreadEventThread()) { throw new RuntimeException("ZkLock should not be used from within the ZooKeeper event thread."); } try { zk.retryOperation(new ZooKeeperOperation<Object>() { @Override public Object execute() throws KeeperException, InterruptedException { zk.delete(lockId, -1); return null; } }); } catch (KeeperException.NoNodeException e) { if (!ignoreMissing) { throw new ZkLockException("Error releasing lock: the lock does not exist. Path: " + lockId, e); } } catch (Throwable t) { throw new ZkLockException("Error releasing lock, path: " + lockId, t); } }
/** * Releases a lock. * * @param lockId the string returned by {@link ZkLock#lock}. * @param ignoreMissing if true, do not throw an exception if the lock does not exist */ public static void unlock(final ZooKeeperItf zk, final String lockId, boolean ignoreMissing) throws ZkLockException { if (zk.isCurrentThreadEventThread()) { throw new RuntimeException("ZkLock should not be used from within the ZooKeeper event thread."); } try { zk.retryOperation(new ZooKeeperOperation<Object>() { @Override public Object execute() throws KeeperException, InterruptedException { zk.delete(lockId, -1); return null; } }); } catch (KeeperException.NoNodeException e) { if (!ignoreMissing) { throw new ZkLockException("Error releasing lock: the lock does not exist. Path: " + lockId, e); } } catch (Throwable t) { throw new ZkLockException("Error releasing lock, path: " + lockId, t); } }
if (zk.isCurrentThreadEventThread()) { throw new RuntimeException("ZkLock should not be used from within the ZooKeeper event thread.");
if (zk.isCurrentThreadEventThread()) { throw new RuntimeException("ZkLock should not be used from within the ZooKeeper event thread.");