【发布时间】:2018-08-14 10:34:41
【问题描述】:
我已经编写了在没有观察者的情况下从 zookeeper 节点检索数据的方法。 zookeeper.getData(nodePath, false, null);
上述方法调用将返回节点中的数据。
现在我想从节点获取数据,稍后当节点发生更新时,我希望返回更新的数据。
这是与zookeeper交互的来源
public class ZooKeeperOperations {
public enum ZooKeeperResult {
SUCCESS, NO_NODE_EXISTS, NODE_ALREADY_EXISTS, BAD_VERSION, CONNECTION_LOSS, NODE_NOT_EMPTY, FAILURE;
}
static ZooKeeperOperations instance = new ZooKeeperOperations();
private ZooKeeperOperations() {
}
public static ZooKeeperOperations getInstance() {
return instance;
}
private static ZooKeeper zk;
private static final String HOST = "localhost"; // No I18N
private static final String APPLICATION_GROUP_NAME = "AppName"; // No I18N
public ZooKeeper connect(String host) throws IOException, InterruptedException {
zk = new ZooKeeper(host, 5000,new Watcher() {
public void process(WatchedEvent we) {
}
});
return zk;
}
// Method to disconnect from zookeeper server
public void close() {
try {
zk.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static Stat znode_exists(String path) {
try {
return zk.exists(getAppPath(path), true);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
private static String getAppPath(String path) {
return "/" + APPLICATION_GROUP_NAME + "/" + path;
}
public Object[] getNodeData(String path, boolean watch, Stat stat) {
try {
String nodePath = getAppPath(path);
byte[] propertyData = this.connect(HOST).getData(nodePath, watch, stat);
TBLogger.logMessage(Level.SEVERE, TBoxIAMUtil.getUserAPI().getCurrentUserZuid(), CLASSNAME, "getNodeData",
new Object[] { new String(propertyData) }, null);
return new Object[] { ZooKeeperResult.SUCCESS, propertyData };
} catch (ConnectionLossException e) {
return new Object[] { ZooKeeperResult.CONNECTION_LOSS };
} catch (NoNodeException e) {
return new Object[] { ZooKeeperResult.NO_NODE_EXISTS };
} catch (Exception e) {
return new Object[] { ZooKeeperResult.FAILURE };
} finally {
this.close();
}
}
}
谁能帮我解决这个问题,我对 zookeeper 完全陌生 :(
【问题讨论】:
-
据我了解您的问题,这就是手表的用途。那么问题到底是什么?
标签: apache-zookeeper