- private void buildZK() {
- System.out.println("Build zk client");
- try {
- zk = new ZooKeeper(zookeeperConnectionString, 10000, this);
- Stat s = zk.exists(rootPath, false);
- if (s == null) {
- zk.create(rootPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
- zk.create(rootPath + "/ELECTION", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
- }
- String value = zk.create(rootPath + "/ELECTION/n_", hostAddress, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
- } catch (Exception e) {
- e.printStackTrace();
- System.err.println("Error connect to zoo keeper");
- }
- }
-
-
- public void process(WatchedEvent event) {
- System.out.println(event);
- if (event.getState() == Event.KeeperState.Disconnected || event.getState() == Event.KeeperState.Expired) {
- System.out.println("Zookeeper connection timeout.");
- buildZK();
- }
-
- }
-
|