下面列出了怎么用org.apache.zookeeper.AsyncCallback.VoidCallback的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void sync(String path, VoidCallback cb, Object ctx) {
executor.execute(() -> {
Optional<KeeperException.Code> failure = programmedFailure(Op.SYNC, path);
if (failure.isPresent()) {
cb.processResult(failure.get().intValue(), path, ctx);
return;
} else if (stopped) {
cb.processResult(KeeperException.Code.ConnectionLoss, path, ctx);
return;
}
cb.processResult(0, path, ctx);
});
}
@Override
public void delete(final String path, int version, final VoidCallback cb, final Object ctx) {
Runnable r = () -> {
mutex.lock();
final Set<Watcher> toNotifyDelete = Sets.newHashSet();
toNotifyDelete.addAll(watchers.get(path));
final Set<Watcher> toNotifyParent = Sets.newHashSet();
final String parent = path.substring(0, path.lastIndexOf("/"));
if (!parent.isEmpty()) {
toNotifyParent.addAll(watchers.get(parent));
}
watchers.removeAll(path);
Optional<KeeperException.Code> failure = programmedFailure(Op.DELETE, path);
if (failure.isPresent()) {
mutex.unlock();
cb.processResult(failure.get().intValue(), path, ctx);
} else if (stopped) {
mutex.unlock();
cb.processResult(KeeperException.Code.CONNECTIONLOSS.intValue(), path, ctx);
} else if (!tree.containsKey(path)) {
mutex.unlock();
cb.processResult(KeeperException.Code.NONODE.intValue(), path, ctx);
} else if (hasChildren(path)) {
mutex.unlock();
cb.processResult(KeeperException.Code.NOTEMPTY.intValue(), path, ctx);
} else {
if (version != -1) {
int currentVersion = tree.get(path).getRight();
if (version != currentVersion) {
mutex.unlock();
cb.processResult(KeeperException.Code.BADVERSION.intValue(), path, ctx);
return;
}
}
tree.remove(path);
mutex.unlock();
cb.processResult(0, path, ctx);
toNotifyDelete.forEach(watcher -> watcher
.process(new WatchedEvent(EventType.NodeDeleted, KeeperState.SyncConnected, path)));
toNotifyParent.forEach(watcher -> watcher
.process(new WatchedEvent(EventType.NodeChildrenChanged, KeeperState.SyncConnected, parent)));
}
};
try {
executor.execute(r);
} catch (RejectedExecutionException ree) {
cb.processResult(KeeperException.Code.SESSIONEXPIRED.intValue(), path, ctx);
return;
}
}